Running the examples in this book
The code examples in this book have been written for Python 3.4. They use some syntax that is not available in older versions of Python. Therefore, there are a few places we will need to change the code if we want to run the examples in, say, Python 2.6.
Note
The entire source code with all the changes below is available online at https://github.com/siddhi/test_driven_python. Get this code if you would like to run the example code in this book under Python 2.6, 2.7, 3.0, 3.1, 3.2, or 3.3
The following changes are required:
Enum: TheEnumlibrary is not in the standard library with older Python versions. It has been backported and can be installed from PyPi. Install theEnum34library to use this feature.setsyntax: Newer versions of Python support the single curly braces shorthand syntax to createsetobjects like{"MSFT"}. In older versions, we will need to explicitly create sets with this equivalent longhand syntax:set(["MSFT"]).printstatement:printis defined as a statement in Python 2.x, so we cannot call it as a function, neither can we mock it out. We can get around this by adding the linefrom __future__ import print_functionto the top of all the files that useprint.builtins: Thebuiltinsmodule is called__builtin__in Python 2.x. Therefore, we need to use__builtin__.printor__builtin__.openwhen we want to mock theprintoropenfunctions.yield fromexpression: This expression is not available in older Python versions. It has to be replaced with an iteration.mock_open: This mock helper only mocks thereadmethod in the backported version. It doesn't support mocking iteration on the file object. So, we need to change the implementation to not use iteration.
With these changes, the examples in this book will work on Python 2.6 onward.