|
3 | 3 | Futurize: 2 to both |
4 | 4 | -------------------- |
5 | 5 |
|
6 | | -For example, running ``futurize`` turns this Python 2 code: |
7 | | - |
8 | | -.. code-block:: python |
9 | | -
|
10 | | - import ConfigParser # Py2 module name |
11 | | -
|
12 | | - class Upper(object): |
13 | | - def __init__(self, iterable): |
14 | | - self._iter = iter(iterable) |
15 | | - def next(self): # Py2-style iterator interface |
16 | | - return next(self._iter).upper() |
17 | | - def __iter__(self): |
18 | | - return self |
19 | | -
|
20 | | - itr = Upper('hello') |
21 | | - print next(itr), |
22 | | - for letter in itr: |
23 | | - print letter, # Py2-style print statement |
24 | | -
|
25 | | -into this code which runs on both Py2 and Py3: |
26 | | - |
27 | | -.. code-block:: python |
28 | | -
|
29 | | - from __future__ import print_function |
30 | | - from future import standard_library |
31 | | - standard_library.install_hooks() |
32 | | - from future.builtins import next |
33 | | - from future.builtins import object |
34 | | - import configparser # Py3-style import |
35 | | -
|
36 | | - class Upper(object): |
37 | | - def __init__(self, iterable): |
38 | | - self._iter = iter(iterable) |
39 | | - def __next__(self): # Py3-style iterator interface |
40 | | - return next(self._iter).upper() |
41 | | - def __iter__(self): |
42 | | - return self |
43 | | -
|
44 | | - itr = Upper('hello') |
45 | | - print(next(itr), end=' ') # Py3-style print function |
46 | | - for letter in itr: |
47 | | - print(letter, end=' ') |
48 | | -
|
49 | | -
|
50 | | -To write out all the changes to your Python files that ``futurize`` suggests, |
51 | | -use the ``-w`` flag. |
52 | | - |
53 | | -For complex projects, it is probably best to divide the porting into two stages. |
54 | | -Stage 1 is for "safe" changes that modernize the code but do not break Python |
55 | | -2.6 compatibility or introduce a depdendency on the ``future`` package. Stage 2 |
56 | | -is to complete the process. |
| 6 | +.. include:: futurize_overview.rst |
57 | 7 |
|
58 | 8 |
|
59 | 9 | .. _forwards-conversion-stage1: |
@@ -157,16 +107,16 @@ The complete set of fixers applied by ``futurize --stage1`` is: |
157 | 107 | libfuturize.fixes.fix_raise |
158 | 108 |
|
159 | 109 |
|
160 | | -Not applied: |
| 110 | +The following fixers from ``lib2to3`` are not applied: |
161 | 111 |
|
162 | 112 | .. code-block:: python |
163 | 113 |
|
164 | 114 | lib2to3.fixes.fix_import |
165 | 115 |
|
166 | | -The ``fix_absolute_import`` fixer in`` libfuturize.fixes`` is applied instead of |
167 | | -this. The new fixer both makes implicit relative imports explicit and |
168 | | -adds the declaration ``from __future__ import absolute_import`` at the top |
169 | | -of each relevant module. |
| 116 | +The ``fix_absolute_import`` fixer in ``libfuturize.fixes`` is applied instead of |
| 117 | +``lib2to3.fixes.fix_import``. The new fixer both makes implicit relative |
| 118 | +imports explicit and adds the declaration ``from __future__ import |
| 119 | +absolute_import`` at the top of each relevant module. |
170 | 120 |
|
171 | 121 | .. code-block:: python |
172 | 122 |
|
@@ -208,7 +158,7 @@ This converts ``set([1, 2, 3]``) to ``{1, 2, 3}``, breaking Python 2.6 support. |
208 | 158 | lib2to3.fixes.fix_ws_comma |
209 | 159 |
|
210 | 160 | This performs cosmetic changes. This is not applied by default because it |
211 | | -does not serve improve Python 2/3 compatibility. (In some cases it may |
| 161 | +does not serve to improve Python 2/3 compatibility. (In some cases it may |
212 | 162 | also reduce readability: see issue #58.) |
213 | 163 |
|
214 | 164 |
|
@@ -272,7 +222,7 @@ becomes:: |
272 | 222 | standard_library.install_hooks() |
273 | 223 | import configparser |
274 | 224 |
|
275 | | -A complete list of fixers applied in Stage 2 is:: |
| 225 | +The complete list of fixers applied in Stage 2 is:: |
276 | 226 |
|
277 | 227 | lib2to3.fixes.fix_basestring |
278 | 228 | lib2to3.fixes.fix_dict |
@@ -321,8 +271,7 @@ Not applied:: |
321 | 271 |
|
322 | 272 | Fixes applied with the ``futurize --conservative`` option:: |
323 | 273 |
|
324 | | - libfuturize.fixes.fix_division_safe |
325 | | - (instead of libfuturize.fixes.fix_division). |
| 274 | + libfuturize.fixes.fix_division_safe # instead of libfuturize.fixes.fix_division. |
326 | 275 |
|
327 | 276 |
|
328 | 277 |
|
|
0 commit comments