pierrepo/PBxplore — pull request #17 by jbarnoud (2014-10-04 05:55:11+00:00)
Title: Python 3 compatibility

This pull request adds python 3 compatibility while maintaining python 2 compatibility with the same codebase.

I did 3 changes to reach this goal: I converted all print keyword calls to function calls, I replaced the semantics of range, and I removed calls to the iterkeys method.

In python 3, print is a function rather than a keyword like n python 2. This makes the print syntax from python 2 incompatible with python 3. Yet, since python 2.6, the print function semantics is available through the \__future__ module. All new python file should contains the print_function import to allow the python 3-like syntax and disallow the python 2-like one:

```python
from __future__ import print_function
```

The range function in python 3 returns a generator rather than a list like in python 2. This way, the range function behave like the python 2 xrange function. The xrange function is not available in python 3. I overwrote the range function in python 2 so it is an alias for xrange. I did this in a per file fashion; having a compatibility module that we import everywhere may be worth at one point. Note that there is no python 2-like range function available anymore. To get a list, you should write list(range(x)).

Python 3 focusses on generators, and most method return generators rather than lists. We seen this already with the range function. Dictionary methods in python 3 (keys, values, items) are among these function that return generators in python 3. Because the min methods return generators, the iter* methods have been removed. PBxplore used the iterkeys method. I replaced all iterations over iterkeys with iteration over the dictionary. In both cases, it means iterating over a generator that yields the dictionary keys. The chosen syntax has the same behaviour in python 2 and 3, and keeps the previous behaviour. It is slightly less readable, though.

In addition to the changes in this pull requests, other things should be taken into account to keep the compatibility with both python 2 and python 3. A useful guide can be found at .