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
: TheEnum
library is not in the standard library with older Python versions. It has been backported and can be installed from PyPi. Install theEnum34
library to use this feature.set
syntax: Newer versions of Python support the single curly braces shorthand syntax to createset
objects like{"MSFT"}
. In older versions, we will need to explicitly create sets with this equivalent longhand syntax:set(["MSFT"])
.print
statement:print
is 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_function
to the top of all the files that useprint
.builtins
: Thebuiltins
module is called__builtin__
in Python 2.x. Therefore, we need to use__builtin__.print
or__builtin__.open
when we want to mock theprint
oropen
functions.yield from
expression: This expression is not available in older Python versions. It has to be replaced with an iteration.mock_open
: This mock helper only mocks theread
method 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.