Skip to content

Commit fab4254

Browse files
committed
Docs: add link to cheat sheet from contents page; tweak
1 parent 58c2523 commit fab4254

File tree

5 files changed

+340
-116
lines changed

5 files changed

+340
-116
lines changed

docs/compatible_idioms.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.. _compatible-idioms:
12

23
Cheat Sheet: Writing Python 2-3 compatible code
34
===============================================

docs/contents.rst.inc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ Contents:
1111
standard_library_imports
1212
what_else
1313
automatic_conversion
14-
porting
14+
compatible_idioms
15+
futurize_cheatsheet
1516
stdlib_incompatibilities
1617
faq
1718
changelog
Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
.. _porting:
1+
.. _futurize_cheatsheet:
22

3-
Python 2 to 2&3 porting cheat-sheet
4-
===================================
3+
``futurize`` cheat-sheet: automatic conversion from Py2 to Py2&3
4+
================================================================
55

6-
Instructions and notes on porting code from Python 2 to both Python 3 and 2 using ``future``:
6+
Instructions and notes on converting code from supporting only Python 2 to
7+
supporting both Python 3 and 2 with a single codebase using ``futurize``:
78

89
.. _porting-setup:
910

@@ -14,7 +15,7 @@ Step 0 goal: set up and see the tests passing on Python 2 and failing on Python
1415

1516
a. Clone the package from github/bitbucket. Optionally rename your repo to ``package-future``. Examples: ``reportlab-future``, ``paramiko-future``, ``mezzanine-future``.
1617
b. Create and activate a Python 2 conda environment or virtualenv. Install the package with ``python setup.py install`` and run its test suite on Py2.7 or Py2.6 (e.g. ``python setup.py test`` or ``py.test`` or ``nosetests``)
17-
c. Optionally: if there’s a ``.travis.yml`` file, add Python version 3.3 and remove any versions < 2.6.
18+
c. Optionally: if there is a ``.travis.yml`` file, add Python version 3.3 and remove any versions < 2.6.
1819
d. Install Python 3.3 with e.g. ``sudo apt-get install python3``. On other platforms, an easy way is to use `Miniconda <http://repo.continuum.io/miniconda/index.html>`_. Then e.g.::
1920
2021
conda create -n py33 python=3.3 pip
@@ -31,7 +32,8 @@ The goal for this step is to modernize the Python 2 code without introducing any
3132
pip install future
3233
3334
**1b**. Run ``futurize --stage1 -w *.py subdir1/*.py subdir2/*.py``. Note that with
34-
``zsh``, you can apply stage1 to all Python source files recursively with::
35+
recursive globbing in ``bash`` or ``zsh``, you can apply stage 1 to all Python
36+
source files recursively with::
3537

3638
futurize --stage1 -w **/*.py
3739

@@ -76,10 +78,10 @@ again with the help of the ``future`` package.
7678

7779
**2a**. Run::
7880

79-
futurize -stage2 myfolder1/*.py myfolder2/*.py
81+
futurize --stage2 myfolder1/*.py myfolder2/*.py
8082

81-
Alternatively, with ``zsh``, you can view the stage 2 changes to all Python source files
82-
recursively with::
83+
Or, using recursive globbing with ``bash`` or ``zsh``, you can view the stage 2
84+
changes to all Python source files recursively with::
8385

8486
futurize --stage2 **/*.py
8587

@@ -93,13 +95,13 @@ These will likely require imports from ``future``, such as::
9395
from future.builtins import bytes
9496
from future.builtins import open
9597

96-
Optionally, you can use the ``--unicode-literals`` flag to adds this further
97-
import to the top of each module::
98+
Optionally, you can use the ``--unicode-literals`` flag to add this import to
99+
the top of each module::
98100

99101
from __future__ import unicode_literals
100102

101-
All strings would then be unicode (on Py2 as on Py3) unless explicitly marked
102-
with a ``b''`` prefix.
103+
All strings in the module would then be unicode on Py2 (as on Py3) unless
104+
explicitly marked with a ``b''`` prefix.
103105

104106
If you would like ``futurize`` to import all the changed builtins to have their
105107
Python 3 semantics on Python 2, invoke it like this::
@@ -111,19 +113,11 @@ Python 3 semantics on Python 2, invoke it like this::
111113

112114
**2c**. Commit your changes! :)
113115

114-
**2d**. Now run your tests on Python 2 and notice the errors. Add wrappers from ``future`` to re-enable Python 2 compatibility:
115-
116-
- :func:`utils.reraise()` function for raising exceptions compatibly
117-
- ``bytes(b'blah')`` instead of ``b'blah'``
118-
- ``str('my string')`` instead of ``'my string'`` if you need to enforce Py3’s strict type-checking on Py2
119-
- ``int(1234)`` instead of ``1234`` if you want to enforce a Py3-like long integer
120-
- :func:`@utils.implements_iterator` decorator for any custom iterator class with a ``.__next__()`` method (which used to be ``.next()``)
121-
- :func:`@utils.python_2_unicode_compatible` decorator for any class with a ``__str__`` method (which used to be ``__unicode__``).
122-
- :func:`utils.with_metaclass` to define any metaclasses.
123-
124-
See :ref:`what-else` for more info.
116+
**2d**. Now run your tests on Python 2 and notice the errors. Add wrappers from
117+
``future`` to re-enable Python 2 compatibility. See the
118+
:ref:`compatible-idioms` cheat sheet or :ref:`what-else` for more info.
125119

126120
After each change, re-run the tests on Py3 and Py2 to ensure they pass on both.
127121

128-
**2e.** Youre done! Celebrate! Push your code and announce to the world! Hashtags
122+
**2e**. You're done! Celebrate! Push your code and announce to the world! Hashtags
129123
#python3 #python-future.

0 commit comments

Comments
 (0)