6
Python in Bigger Projects

WHAT YOU WILL LEARN IN THIS CHAPTER:    

  • Testing your Python code
  • Debugging your Python code
  • Handling errors in your Python code
  • Structuring and releasing your Python code
  • Tuning the performance of your Python code

WROX.COM DOWNLOADS FOR THIS CHAPTER

You can find the wrox.com downloads for this chapter at www.wrox.com/go/pythonprojects on the Download Code tab. The code is in the Chapter 6 download, called Chapter 6.zip, and individually named according to the names throughout the chapter.

So far you’ve looked at many ways to use Python. You’ve made local scripts to handle small tasks, you’ve handled medium-sized tasks locally, and you’ve even made a small web app using Flask. But what if you find yourself in the midst of a larger project? Python, as you have seen by now, is a very powerful language. It’s also very open, meaning you, the developer, have access to all aspects of the language. This openness, however, makes testing your Python code more important than ever. Every object in Python is a first-class object, so you can change and manipulate any object available to you. Because you can change and manipulate objects, you must make sure to test and verify the logic of our code.

Python is not a “typed” language in the same way that C and Java are explicitly typed. You can pass objects around in Python and the interpreter will try to manipulate them to the best of its ability. If it cannot perform an operation on an object or data that is available, however, it raises an exception, which causes your program to crash. So, how can you prevent this? How can you write code, share that code, and guarantee that others can use it and that the code will function as expected? Testing.

Testing with the Doctest Module

The simplest form of testing in Python is the doctest module. This module is made for testing the simpler parts of your code, to verify that it will function as expected, as written in your document strings (triple quotes '''...''' or"""...""", single or double quotes will both work). Doctest tests are written like this:

  '''
  this function should take in a number and return its squared value
  >>> sq(3)
  9
  '''

  def sq(n):
 	return n*n

The usual way of writing doctest tests is to use the interpreter, write the code, and then run it in the interpreter. Then you copy and paste the interpreter text into the doctest string, as follows:

  Python 3.3.3 (default, Feb 14 2014, 12:35:03)
  [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
  Type "help", "copyright", "credits" or "license" for more information.
  >>> def sq(n):
  ... return n*n
  ...
  >>> sq(3)
  9
  >>>

So you would simply copy the following lines, and put them in your doctest strings:

  >>> sq(3)
  9

Doctest is not suitable for testing of large, complicated methods or functions. But it is really good at “contract programming.” By using doctest strings and saying “this function, when passed a 3 as an argument, will return a 9” and then calling the function, you are setting up a contract: if you pass a certain piece of data, the function will behave as you expect it to. However, you cannot test every possible outcome, so doctest will hit its limitations fairly quickly with larger projects.

In the following example you create and then run a small Python script with some doctest strings to test your code.

Although doctest is good for evaluating whether your documentation strings are true and the code behaves as expected, it is not meant for thorough, robust testing of more complicated codebases. There are many other facets to the doctest API. You should check out the documentation to familiarize yourself with the full functionality of the module.

Testing with the Unittest Module

What if you need significant testing and you want to verify that your codebase is operating as expected? This is a job for the unittest module. This module is more robust than the doctest module, and will test your code thoroughly. Unittest is like the baseline testing module on which most testing libraries are based. It is also an excellent introduction to test-driven development (TDD) in Python.

The term unit test is not unique to Python. If you’re familiar with other languages and programming, you have no doubt heard of unit testing. Unit testing is simply testing your code in units. So, if you have five functions in your code, you want to have a minimum of five units in your testing harness for each unit of functionality in your codebase. Unit tests also consist of a test file, which contains all of your tests, written in the same structure or format as any other Python file. The only difference is that each test begins with test, and each test harness is a class from the Unittest.Test object. For example, if you have a function named login, and you want to test that function, create a test named test_login, which would then call your login function and run your tests against the output of that function.

Don’t forget that when you are writing unittest classes, you need to import the code module you’ll be testing into your test code. If you were testing users.py, you would need to import users into your test.py file, so that you can test the functions in the users module with your unittests.

You create unittest tests by creating classes that are subclasses to the TestCase class, as follows:

  import unittest

  class PythonProjectsTest(unittest.TestCase):
 	eturn

You want to put statements within your class that will be evaluated when the test is run and return an assertion value of True or False:

  import unittest

  class PythonProjectsTest(unittest.TestCase):
 	def test_to_fail(self):
 		self.failIf(False)

  if __name__ == '__main__':
 	unittest.main()

In the preceding example, you use the assertion method failIf() to evaluate the value in the parentheses. If the value is true, you will receive a failure message when you run the test. In this case, you’re passing in False, which will, of course, evaluate to false. Therefore, this test will return a failure.

If you run this test you should see the following output:

  ======================================================================
  FAIL: test_to_fail (__main__.PythonProjectsTest)
  ----------------------------------------------------------------------
  Traceback (most recent call last):
  File "<stdin>", line 3, in test_to_fail
  AssertionError: True is not false

  ----------------------------------------------------------------------
  Ran 1 test in 0.000s

  FAILED (failures=1)

If you change self.failIf(True) to self.failIf(False), you should see your output change to:

  ----------------------------------------------------------------------
  Ran 1 test in 0.000s
  OK

Note that unittest doesn’t evaluate whether a test is actually passing; it is simply evaluates whether an exception is thrown. Therefore, if an exception is not thrown, the test is considered OK. This could mean that your precise calculation, while returning a not-so precise number, shows as passing, or OK, not because the result is correct—which it isn’t—but simply because the test is not raising an exception.

Following are the three possible outcomes of unittest if it doesn’t actually have passing tests:

  • OK: The test is OK; no exception raised.
  • Fail: An AssertionError was raised (the test has failed).
  • Error: An exception was raised that is not an AssertionError.

The best way to understand unit testing and the unittest module is to just do some testing.

Some readers may quickly realize that testing with static data isn’t foolproof. What if the data that is passed in isn’t a type that you’ve tested? This is why writing good tests is important. One function in your program may have multiple tests, or one test could verify multiple situations.

Test-Driven Development in Python

A term that is becoming more and more popular in the Python community is test-driven development (TDD). What exactly does that mean? Although TDD is a very important topic when it comes to Python development, it is also a very robust topic. Therefore, this section gives only a very brief introduction of TDD so that you can familiarize yourself with the term and its basic definition.

TDD simply means writing your tests first. Most developers groan when they hear the word “testing.” They think it means longer development time and more effort on their part, and less of the fun stuff like writing the actual code that will make their project run. However, testing can be just as fun as the other stuff. And although it does require the developer to write more lines of code, it leads to better quality code and more maintainability later on in the project. Your future self and co-collaborators will thank you for taking the time to write tests first and develop against those tests.

So, how exactly does TDD work? Write tests! It’s really that simple. There is, of course, an art form to writing good tests, and it’s important that you take the time to study up and become familiar with proper TDD practices. Here are the basics:

  1. Write tests first.
  2. All tests should fail at first.
  3. Write code.
  4. Test code against tests.
  5. Rewrite code.
  6. Retest code against tests.
  7. Repeat until all tests are passing.

This is the gist of TDD. You can probably see why doctest may not be the best answer for all testing situations. Once you have to test and retest, and you begin testing more complex ideas, doctest will hit its limitations. As stated, there is an art to writing effective tests, however, and that is where the beauty of TDD comes in.

Debugging Your Python Code

Most developers will likely tell you that they hate debugging. It’s tedious, persnickety, and can become rather boring or infuriating fairly quickly. It doesn’t have to be this way. Taking a new look at debugging and testing can make even the most cynical developer a little less irritated.

When you run into a bug with your code, rather than think about how annoying it is (don’t worry, it’s the natural reaction), think about how this is actually an opportunity to learn. Something is broken somewhere, some stone has gone unturned. This is your chance to find that stone, turn it over, and see what there is to see! You’re well on your way to becoming a seasoned programmer with every bug you squash.

Python makes debugging a little less of a hassle with the Python debugger module, or the pdb. If you read Chapter 5 and explored the Chrome Developer Tools, you may notice some similarities. If you’re a web developer by trade who is trying Python on for size, you’ll probably find that you like the pdb and it reminds you a little of your favorite web debugging software.

The pdb is fairly powerful in that it enables you to insert breakpoints in your code that will stop your code running, and drop you into a pdb prompt or terminal. This is very handy because you can then begin examining the data you have in scope at that moment. If you find an exception is being raised when a certain function is called, you can put a pdb() call in that function and then you can start to examine the data in an interactive interpreter in your terminal. Let’s try it out.

The following example illustrates using the pdb module for debugging your Python code.

Handling Exceptions in Python

Python is an interpreted language, which means that there is no compiler to compile your code and find any logic or syntax errors before you run it. So how does Python handle this? Python uses exceptions to handle errors. This type of handling can mean that making one small mistake in your code can cause your entire program to fail. Because of this you want to test thoroughly, but on top of that, you also want to set up some fail-safes in case you encounter exceptions with your code during run time.

For example, if you try the following code in your interpreter,

  >>> def sum(a, b):
  ...	return a + b
  ...
  >>> sum("no", 4)

you’ll get the following error:

  Traceback (most recent call last):
 	File "<stdin>", line 1, in <module>
 	File "<stdin>", line 2, in sum
  TypeError: Can't convert 'int' object to str implicitly

As you can see, when you try to pass a string to a mathematic function, which can only operate on integers and floats, it throws a TypeError. This tells you that the data you sent to the function is not of the correct type. Because Python is not a strongly typed language, nor is it compiled, the only errors that you will get are exceptions, which will crop up at run time. When an exception is thrown at run time, your entire program will quit if there is no exception handling in place. It is imperative that you check for these sorts of “gotchas.” Not checking for them can render your code unusable, and that’s not a very good codebase to have!

A number of exceptions are built into the Python language. Here is a list of those exceptions:

  BaseException
  	+-- SystemExit
  	+-- KeyboardInterrupt
  	+-- GeneratorExit
  	+-- Exception
  	+-- StopIteration
  	+-- StandardError
  	|	+-- BufferError
  	|	+-- ArithmeticError
  	| |		+-- FloatingPointError
  	| |		+-- OverflowError
  	| |		+-- ZeroDivisionError
  	|	+-- AssertionError
  	|	+-- AttributeError
  	|	+-- EnvironmentError
  	| |		+-- IOError
  	| |		+-- OSError
  	| |		+-- WindowsError (Windows)
  	| |		+-- VMSError (VMS)
  	|	+-- EOFError
  	|	+-- ImportError
  	|	+-- LookupError
  	| |		+-- IndexError
  	| |		+-- KeyError
  	|	+-- MemoryError
  	|	+-- NameError
  	| |		+-- UnboundLocalError
  	|	+-- ReferenceError
  	|	+-- RuntimeError
  	| |		+-- NotImplementedError
  	|	+-- SyntaxError
  	| |	 +-- IndentationError
  	| |		+-- TabError
  	|	+-- SystemError
  	|	+-- TypeError
  	|	+-- ValueError
  	|		+-- UnicodeError
  	|			+-- UnicodeDecodeError
  	|			+-- UnicodeEncodeError
  	|			+-- UnicodeTranslateError
  	+-- Warning
  		+-- DeprecationWarning
  		+-- PendingDeprecationWarning
  		+-- RuntimeWarning
  		+-- SyntaxWarning
  		+-- UserWarning
  		+-- FutureWarning
  		+-- ImportWarning
  		+-- UnicodeWarning
  		+-- BytesWarning

With so much that can go wrong, how do you gracefully handle exceptions in Python? With a try-except block. The try-except block will try a piece of code and if the code throws one of the preceding exceptions, it will catch that exception and print out an error message, as defined in the base exception class, or you can even print your own error messages for each exception:

  >>> try:
  ...	sum("yes", 9)
  ... except TypeError:
  ...	print("Both inputs must be integers")
  ...
  Both inputs must be integers

You can also have try-except blocks handle exceptions so that your program doesn’t fail and you can continue moving down the stack:

  >>> try:
  ...	some_function()
  ... except:
  ...	graceful_function()
  ... else:
  ... next_function()

Sometimes you will want to run a function no matter if your try-catch catches an exception or runs. In that case you want to use the finally statement.

  >>> try:
  ...	some_function()
  ... except:
  ...	graceful_function()
  ... finally:
 	cleanup_function()

But what if you want your code to throw its own exceptions? What if you want to check for some certain type of data, and if that is not present, you want to alert the user? You can make custom exception classes to use on top of built-ins.

In the following example, you create and use customs exceptions.

As you can see, this feature can be incredibly powerful when writing larger projects. Hopefully this has given you enough of a glimpse into the formulation of exceptions that you can write your own, should the need arise.

Working on Larger Python Projects

When developing with Python you may find that different projects have different versions of different packages. What do you do when your local environment is Python 2.7, but that project you want to work on (or inherited) is 2.6? Or 3.4? This is a problem that many Python developers have encountered, so of course they created a solution. Enter virtualenv.

Virtualenv is a virtual environment for your Python projects. It enables you to create numerous Python instances and develop against all the libraries you need for certain projects. Say you want to work on a project that uses Python 2.7, which you have installed locally, but the project needs a different version of a library than what you have installed locally. The Python versions match up but the library’s versions do not. This is a job for virtualenv!

In this example, you create and then activate a virtualenv to create sandboxes for your individual Python projects.

Oftentimes you have projects where more than one person is working in the environment. What happens when you have a long list of requirements that your project needs and you have four people working on the project, on different machines? Do you want to have your teammates simply type pip install <module_name> over and over? No, you do not.

Virtualenv has a very nice feature that enables you to make a requirements.txt file and put the packages needed for your program into the file. Anyone using your package can simply type pip install requirements.txt and get all the dependencies that your package requires! It really is that easy!

Releasing Python Packages

The __init__.py ('dunder, init, dunder') file is fairly important when releasing code out into the wild. For Python projects, __init__.py needs to be at each level of the codebase’s directory structure. For example, say you have a rather large codebase that has multiple .py files. You start by putting a __init__.py in the first layer of the directory structure:

  my_package
 	|----__init__.py
 		|---- my_package.py
 			|---- my_subpackage
 				|---- __init__.py
 				|---- my_subpackage.py

This tells the Python interpreter that you want to treat the directory as a Python package. The cool part is that you can leave the __init__.py file empty, or you can put configuration variables in it. Commonly, folks will import modules/libraries, or other configurations in their __init__.py file—basic setup work to help the package function.

So what happens when you create an __init__.py file and import something? How does Python’s namespacing work now? Suppose you have the following import statement in my_package/__init__.py :

  from file import File

When you want to call that import in the my_package.py file you would simply say:

  from my_package import File

Another use of the __init__.py file is to import all the modules that you’d like to import into the namespace of your package. You do this by assigning the __all__ variable to your subpackage in your package level __init__.py (the first one):

  __all__ = ['my_subpackage']

Doing this makes it so that when your users declare from my_package import * it will import all of the modules from my_subpackage.

Now that you have your code written, and your __init__.py files in place, what if you want to release this code out into the wild? What if you want to be able to install this module on other machines by simply typing pip install <package_name>?

Summary

We’ve looked over some of the basics of testing and packaging for your Python projects. You should now have a clear idea of just how most Python packages and modules/libraries are architected and created. A good exercise for the reader is to go back through the beginning of the book and work through the exercises using the concepts you’ve learned in this chapter. Can you rewrite the code in Chapter 3 to be test-driven? Can you package your Flask app from Chapter 5 and send it to another computer to be run and developed? You should try these things out so that you have a clear idea of just how all parts and pieces of Python packages are working together.

EXERCISES

  1. In the zip file for this chapter, open the file markets.py and write a doctest string to test the value being returned by the function in the file. Can you think of a reason why a simple doctest string in this code could be incredibly useful for maintaining the code in the future?

  2. Write a unittest for a function that will take a string and return that string reversed. Make sure the test fails, because you haven’t written the function to test, yet.

  3. Write a function for your unittest that takes a string and returns the reverse of that string. Now, run your unittest against that function and modify the function until it passes.

arrow2  WHAT YOU LEARNED IN THIS CHAPTER

TOPIC DESCRIPTION
Unit test Usually a function that is written in a separate testing script, that imports the code to be tested, and that tests each function in the imported code.
Virtualenv Third-party software that allows developers to create system sandboxes for Python development, using customized versions of Python and Python libraries/modules.
TDD (Test-Driven Development A development style where one writes tests first, which will fail, then writes the actual functioning code to make the tests past, therefore driving the development cycle based on testing first.
Pdb Python Debugger, an interactive debugging module for Python.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset