Iron Python

Inte^ate PytW

#4: Automated testing techniques

And, no, before you ask, this has nothing to do with program-testing robots automatically testing your code for you (which, we agree, would be nice).

Automated testing does have everything to do with trying to make sure, as far as possible, that your code is working properly.

But, how is this possible?

The truth is, it's very hard to know for sure that your code is working perfectly 100% of the time. Programs and software systems are complex beasts. Over time, they grow, and it gets increasingly hard to know for sure if they are working quite the way you want them to.

To help, programmers test their code. When they are done, they test, test, then test some more. And just to be sure, they test again. In fact, programmers often test to destruction, which refers to attempting everything they know to try and break the program and make it misbehave. If testing finds a problem, the code is then fixed so that whatever it was isn't a problem anymore.

To help with the grunt work that testing sometimes is, Python comes with a handy built-in module called unittest. This module's sole purpose in life is to allow you to write code that tests your code. Trust us, this isn't as strange as it sounds. The unittest module provides a framework within which you can exercise your code to ensure it's working the way you want it to.

The idea is simple enough: as you write your code, you write a little extra bit of code based on unittest to check that your new code is working properly. If the test works (that is, it successfully demonstrates that your code is OK), you are then in a position to automate the testing of your code by reusing your unittest code.

For more details, check out the description of unittest in your favorite Python book or online in the official Python documentation.

And now that you know about unittest, you have no excuse not to use it to test your code.

exterminating bugs

#5: Debugging

This is not as disgusting as it sounds.

Debugging is related to testing. It is the process of dealing with errors that aren't picked up during testing but, instead, blow up in your face when you run your code or—worse—blow up in the face of your user!

Tracking down where a problem is can sometimes feel like a bit of an art form, but there are some tried and true techniques that can make things easier for you. Experience helps a lot.

One of the most important things you can know about when it comes to debugging code is a thing called the debugger. This is a software tool that can be used by programmers to run code step by step or line by line. As the program runs, the debugger lets you watch what's going on and then potentially see when something goes wrong. If you can work out where in your code the problem lies, it makes it easier to fix, and the debugger is designed to help you do just that.

It is a rare programming technology indeed that comes without a debugger. Python's is called pdb and it can be accessed from within IDLE and within the Python Shell. Check the Python online documentation for more information on pdb.

The problem is not with the hardware; it's with your program. You'll have to DEBUG your code.

As mentioned at the top of the page: debugging is related to testing. Typically, you debug your code after it is up and running and (possibly) delivered to your user. You test your code as you write it and before you give your program to your users. The idea is that you only ever give a user your program when you're happy it works the way you want it to.

To be honest, though, there are no hard and fast rules here, and a lot of programmers blur the line between debugging and testing, treating it all as one activity.

#6: Command-line execution

Throughout the 10% chapters of this book, you've consistently executed your code from within the IDLE programming environment. This has been great while developing and working on your code, but it's probably not how you want your users to run your programs. It turns out there's more than one way to do it when it comes to running your code. The method available to you (and your users) will vary depending on the operating system you're using.

Your operating system might have a file association already in place that allows you to double-click on a Python program and have it execute for you. If this is the case, feel free to double-click to your heart's content. If not, here's how to start Python from the command line on the "big 3" operating systems. Let's assume the program you're running is called coffee pos .py:

On Windows, enter the "C:\Pyth on3l\python.exe" Command together with the name of the program you want to execute.

Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\hfprog> C:\Python31\python.exe coffee_pos.py

1. DONUT

2. LATTE

3. FILTER

4. MUFFIN

5. Quit

Choose an option: |

File Edit Window Help Mac OS X and Linux

Jth the name ^oofram you

tommand W"

$ python3 coffee_pos.py

1. DONUT

2. LATTE

3. FILTER

4. MUFFIN

5. Quit

Choose an option: |

down with oop

#7: Ooops... we could've covered more OOP

Chapter 10 introduced the important concept of classes, and throughout the book we've touched on objects in lots of different places. Doing justice to all the concepts of object oriented programming (OOP) would easily take an entire book all on its own.

The bit of OOP covered in Chapter 10 relates to the concept of encapsulation. This is the process of bundling data with methods into prebuilt templates that can be used to create functionally identical objects of a certain type.

Now... if your eyes glazed over reading that last line, don't worry; you are as normal as the rest of us. OOP is full of terminology like this. As well as encapsulation, there's inheritance and polymorphism, too.

Discussing all the ins and outs of OOP is something that takes a little time and it is not something that we are going to try and do on just one page!

That said, OOP really comes into its own when your programs get very large and turn into software systems. When systems start to scale (get really, really big), the importance of proper design takes center stage, and OOP can help here—big time. Again, there's help from those lovely people at Head First Labs.

This book assumes you already know a little bit about Java, so consider reading Head First Java -first

This book assumes you already know a little bit about Java, so consider reading Head First Java -first

#8: Algorithms

There are plenty of great books that believe it's impossible to learn about programming without also learning about algorithms.

The word "algorithm" is used to describe a precise and established way of solving a particular problem in any programming language. It's often useful to think of an algorithm as a recipe.

In the good ol' days, it was certainly true that every programmer had to have a good grasp of algorithms in order to get anything of any worth done. But, luckily for you, this is no longer the case.

Nowadays, with programming technologies such as Python (and Ruby, Perl, Java, and C#), this is less of an issue because modern programming languages just do so much for you.

For instance, if you need to sort something in Python, you either call the sort () method on a object or use the built-in sorted () function, and the thing you are working with is duly sorted. You are probably less concerned (and rightly so) with how the sort actually occurred—that is, with the details of the algorithm used to sort your data. Was it the quicksort algorithm or the bubblesort? Maybe it was timsort or something else entirely? Maybe you don't care, because calling sort () or using sorted () orders your data in the way you desire and frees you to move onto some other problem that needs solving.

You could take the time to write a really cool sort function and learn lots about algorithms in the process but, let's face it, life is far too short.

If you want to write your own programming language, you'll need to learn lots about algorithms. If all you want to do is use an existing programming language (like Python), you can worry less about algorithms and more about writing code, which is precisely how it should be (in our opinion).

heavy lifting

Scary

#9: Advanoedprograwwing topics

There are a bunch of advanced programming topics that were never likely to make it into a book like this. Here is a list of six such topics (all supported by Python 3).

Threads are a programming technology that allows you to break your program into discrete coded chunks that can then be executed in parallel or concurrently. Each chunk is known as a thread. Using threads can result in some beautiful coding designs, which can make the solving of a certain class of problem almost trivial. Under certain circumstances and restrictions, it is possible to have threads communicate with each other and share data, which, although possible, is hard to get right and can lead to some rather tricky and hard-to-track-down bugs. If you think you need threads, approach with extreme caution and be prepared to have your brain expanded in ways that you never thought possible.

Recursion is a programming technique that allows you to create a function that is capable of calling itself in order to get its work done. There are certain types of mathematical problems that are well-suited to a recursive solution, but not much else.

Metaprogramming refers to writing programs that can write other programs or manipulate themselves (sounds icky, and it can be). It is not for the faint of heart.

Functional programming (typified by languages such as Lisp) is a technique that treats computation as a series of function calls that specify what is required of the program, as opposed to a procedural program, which details the steps that are required in order to solve a problem.

Regular expressions are a technology that allows you to specify concisely what it is you are looking for in your data. Having written a regular expression (or regex), Python can then go and get it for you. Every programmer should know how to take advantage of regexes. The trouble is, they look so very strange at first that most coders recoil in disgust. This is a pity, as we think regexes are super cool and well-worth learning.

Unicode is an industry standard that allows you to consistently represent and work with text written in most of the world's languages or "writing systems." If you are working with text originating from somewhere other than the place where they speak your language, then you'll want to know a bit about the Unicode character encoding system.

If you find yourself needing any of this stuff, best to start on the Internet and do some background reading on the topic before buying a book with your hard-earned cash. The Wikipedia entries for each of these six topics are good starting points.

#10: Other IDEs, shells, and text editors

It might be hard for you to fathom, but there is life beyond IDLE. There's life beyond the Python Shell, too. Both IDLE and the Python Shell are great learning tools, but sooner or later, you might find yourself wanting more.

iPython is a bit like the Python Shell on steroids, and a lot of Python programmers swear by it. It has a lot of extra features over and above those in the normal shell that are designed to make the Python programmer's life easier. It is well-worth looking into.

When it comes to full-blown programming environments, there are many choices. Search the Internet for "Python IDE" to see a complete list. A few that come up more than most include: Komodo, Eclipse, and Eric. It's worth taking a look at each, and then deciding if one of them (or something else entirely) is just right for you.

IDLE and the Python Shell may satisfy your needs. But, if you are looking for more from your programming tool, there's lots of choices out there.

As for text editors... well, there are lots of choices, too. Some people prefer Emacs, while others prefer vi. The Head First Labs advice is to try out a few editors before picking the one that best suits your needs.

Index ^

Was this article helpful?

0 0

Post a comment