Skip to content

Commit 5c069ce

Browse files
committed
Tweak cheat sheet
1 parent 55c03b2 commit 5c069ce

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

docs/compatible_idioms.rst

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ print
5353
5454
# Python 2 and 3:
5555
print('Hello')
56+
5657
To print multiple strings, import ``print_function`` to prevent Py2 from
5758
interpreting it as a tuple:
5859

@@ -96,6 +97,7 @@ Raising exceptions
9697
9798
# Python 2 and 3:
9899
raise ValueError("dodgy value")
100+
99101
Raising bare string exceptions:
100102

101103
.. code:: python
@@ -106,6 +108,7 @@ Raising bare string exceptions:
106108
107109
# Python 2 and 3:
108110
raise Exception("dodgy value")
111+
109112
Raising exceptions with a traceback:
110113

111114
.. code:: python
@@ -137,16 +140,18 @@ Catching exceptions
137140

138141
.. code:: python
139142
143+
# Python 2 only:
140144
try:
141-
# ...
142-
except ValueError, e: # Python 2 only
143-
# ...
145+
...
146+
except ValueError, e:
147+
...
144148
.. code:: python
145149
150+
# Python 2 and 3:
146151
try:
147-
# ...
148-
except ValueError as e: # Python 2 and 3
149-
# ...
152+
...
153+
except ValueError as e:
154+
...
150155
Division
151156
~~~~~~~~
152157

@@ -160,6 +165,7 @@ Integer division (rounding down):
160165
161166
# Python 2 and 3:
162167
assert 2 // 3 == 0
168+
163169
"True division" (float division):
164170

165171
.. code:: python
@@ -172,6 +178,7 @@ Integer division (rounding down):
172178
from __future__ import division # (at top of module)
173179
174180
assert 3 / 2 == 1.5
181+
175182
"Old division" (i.e. compatible with Py2 behaviour):
176183

177184
.. code:: python
@@ -293,6 +300,7 @@ prefixes:
293300
# Python 2 and 3
294301
s1 = u'The Zen of Python'
295302
s2 = u'きたないのよりきれいな方がいい\n'
303+
296304
The ``futurize`` and ``python-modernize`` tools do not currently offer
297305
an option to do this automatically.
298306

@@ -306,6 +314,7 @@ this idiom to make all string literals in a module unicode strings:
306314
307315
s1 = 'The Zen of Python'
308316
s2 = 'きたないのよりきれいな方がいい\n'
317+
309318
See http://python-future.org/unicode\_literals.html for more discussion
310319
on which style to use.
311320

@@ -408,6 +417,7 @@ StringIO
408417
# Python 2 and 3:
409418
from io import BytesIO # for handling byte strings
410419
from io import StringIO # for handling unicode strings
420+
411421
Imports relative to a package
412422
-----------------------------
413423

@@ -437,12 +447,14 @@ and the code below is in ``submodule1.py``:
437447
# To make Py2 code safer (more like Py3) by preventing
438448
# implicit relative imports, you can also add this to the top:
439449
from __future__ import absolute_import
450+
440451
Dictionaries
441452
------------
442453

443454
.. code:: python
444455
445456
heights = {'Fred': 175, 'Anne': 166, 'Joe': 192}
457+
446458
Iterating through ``dict`` keys/values/items
447459
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
448460

@@ -487,6 +499,7 @@ Iterable dict values:
487499
488500
for key in itervalues(heights):
489501
# ...
502+
490503
Iterable dict items:
491504

492505
.. code:: python
@@ -508,6 +521,7 @@ Iterable dict items:
508521
509522
for (key, value) in iteritems(heights):
510523
# ...
524+
511525
dict keys/values/items as a list
512526
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
513527

@@ -523,6 +537,7 @@ dict keys as a list:
523537
# Python 2 and 3:
524538
keylist = list(heights)
525539
assert isinstance(keylist, list)
540+
526541
dict values as a list:
527542

528543
.. code:: python
@@ -556,6 +571,7 @@ dict values as a list:
556571
from six import itervalues
557572
558573
valuelist = list(itervalues(heights))
574+
559575
dict items as a list:
560576

561577
.. code:: python
@@ -1058,7 +1074,7 @@ reload()
10581074
from imp import reload
10591075
reload(mymodule)
10601076
Standard library
1061-
================
1077+
----------------
10621078

10631079
StringIO module
10641080
~~~~~~~~~~~~~~~

docs/notebooks/Writing Python 2-3 compatible code.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:8a3d07779ec3e94f063cb2c8d9fc84d463cf7ffbc69b191c52dd6bc9099c8100"
4+
"signature": "sha256:a318d976dd8aacb67a39acbcafddb513645c61ae27874c0d32d4a64c1621ca35"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -22,7 +22,7 @@
2222
"source": [
2323
"- **Copyright (c):** 2013-2014 Python Charmers Pty Ltd, Australia.\n",
2424
"- **Author:** Ed Schofield.\n",
25-
"- **Licence:** Creative Commons Attribution\n",
25+
"- **Licence:** Creative Commons Attribution.\n",
2626
"\n",
2727
"This notebook shows you idioms for writing future-proof code that is compatible with both versions of Python: 2 and 3.\n",
2828
"\n",
@@ -2293,7 +2293,7 @@
22932293
},
22942294
{
22952295
"cell_type": "heading",
2296-
"level": 1,
2296+
"level": 2,
22972297
"metadata": {},
22982298
"source": [
22992299
"Standard library"

0 commit comments

Comments
 (0)