diff --git a/case-study-porting-chardet-to-python-3.html b/case-study-porting-chardet-to-python-3.html index a3c50ea..78f3bf7 100644 --- a/case-study-porting-chardet-to-python-3.html +++ b/case-study-porting-chardet-to-python-3.html @@ -9,6 +9,7 @@

Case study: porting chardet to Python 3

    +
  1. Diving in
  2. Running 2to3
  3. False is invalid syntax
  4. No module named constants
  5. @@ -17,6 +18,16 @@
  6. Can't convert 'bytes' object to str implicitly
+
+ +

Diving in

+ +

FIXME intro

+ +

...

+ +
+

Running 2to3

diff --git a/dip3.css b/dip3.css index 15c2c6f..c5bc3ed 100644 --- a/dip3.css +++ b/dip3.css @@ -18,8 +18,8 @@ img{border:0} pre,blockquote{line-height:2.154;margin:2.154em 0;padding:0 0 0 2.154em;border-left:1px dotted} blockquote{font-size:small;font-style:oblique;margin-left:2.154em} blockquote p{margin:2.154em 0} -.f,.c{text-align:center;clear:both} -section h2 + p:first-letter{float:left;background:transparent;color:gainsboro;padding:0.11em 4px 0 0;font:normal 4em/0.68 serif} +.c{text-align:center;clear:both;font-size:small} +p.fancy:first-letter{float:left;background:transparent;color:gainsboro;padding:0.11em 4px 0 0;font:normal 4em/0.68 serif} #arc{width:100%,border-collapse:collapse} #arc th,#arc td{list-style:none;margin:0;padding:0} #arc th{padding:0 1.75em 0 0;text-align:right;vertical-align:baseline} diff --git a/index.html b/index.html index 509e777..c7c9368 100644 --- a/index.html +++ b/index.html @@ -846,6 +846,7 @@
diff --git a/porting-code-to-python-3-with-2to3.html b/porting-code-to-python-3-with-2to3.html new file mode 100644 index 0000000..89187c4 --- /dev/null +++ b/porting-code-to-python-3-with-2to3.html @@ -0,0 +1,1277 @@ + + + + +Porting code to Python 3 with 2to3 - Dive into Python 3 + + + + +

Porting code to Python 3 with 2to3

+ +
    +
  1. Diving in
  2. +
  3. print statement
  4. +
  5. <> comparison
  6. +
  7. has_key() dictionary method
  8. +
  9. Dictionary methods
  10. +
  11. filter() global function
  12. +
  13. map() global function
  14. +
  15. apply() global function
  16. +
  17. intern() global function
  18. +
  19. exec statement
  20. +
  21. repr literals (backticks)
  22. +
  23. try...except statement
  24. +
  25. raise statement
  26. +
  27. throw statement
  28. +
  29. long data type
  30. +
  31. xrange() global function
  32. +
  33. raw_input() global function
  34. +
  35. input() global method
  36. +
  37. func_* function attributes
  38. +
  39. xreadlines() I/O method
  40. +
  41. imports
  42. +
  43. lambda functions with multiple parameters
  44. +
  45. __class__ special class attribute
  46. +
  47. next() iterator method
  48. +
  49. __nonzero__ special class attribute
  50. +
  51. Number literals
  52. +
  53. sys.maxint
  54. +
  55. unicode() global function
  56. +
  57. Unicode string literals
  58. +
  59. callable() global function
  60. +
  61. zip() global function
  62. +
  63. StandardError() exception
  64. +
  65. types module constants
  66. +
  67. basestring datatype
  68. +
  69. itertools module
  70. +
  71. Relative imports
  72. +
  73. sys.exc_type, sys.exc_value, sys.exc_traceback
  74. +
  75. List comprehensions over tuples
  76. +
  77. os.getcwdu() function
  78. +
  79. Metaclasses
  80. +
  81. set() literals
  82. +
  83. buffer() global function
  84. +
  85. Whitespace around commas
  86. +
  87. Common idioms
  88. +
+ +
+

Diving in

+ +

FIXME intro

+ +

...

+ +
+ +
+

print statement

+ +

FIXME intro

+ +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NotesPython 2Python 3
printprint()
print 1print(1)
print 1, 2print(1, 2)
print 1, 2,print(1, 2, end=' ')
print >>sys.stderr, 1, 2, 3print(1, 2, 3, file=sys.stderr)
+ +

...

+ +
+ +
+

<> comparison

+ +

FIXME intro

+ +

+ + + + + + + + + + + + + + + + +
NotesPython 2Python 3
if x <> y:if x != y:
if x <> y <> z:if x != y != z:
+ +

...

+ +
+ +
+

has_key() dictionary method

+ +

FIXME intro

+ +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NotesPython 2Python 3
aDictionary.has_key("PapayaWhip")"PapayaWhip" in aDictionary
aDictionary.has_key(x) or aDictionary.has_key(y)x in aDictionary or y in aDictionary
aDictionary.has_key(x + y)(x + y) in aDictionary
x + aDictionary.has_key(y)x + (y in aDictionary)
aDictionary.has_key(x or y)(x or y) in aDictionary
+ +

...

+ +
+ +
+

Dictionary methods

+ +

FIXME intro

+ +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NotesPython 2Python 3
aDictionary.keys()list(aDictionary.keys())
aDictionary.items()list(aDictionary.items())
aDictionary.iterkeys()iter(aDictionary.keys())
[i for i in aDictionary.iterkeys()][i for i in aDictionary.keys()]
min(aDictionary.keys())no change
+ +

...

+ + + +
+ +
+

filter() global function

+ +

FIXME intro

+ +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NotesPython 2Python 3
filter(aFunction, aList)list(filter(aFunction, aList))
list(filter(aFunction, aList))no change
filter(None, aList)[i for i in aList if i]
for i in filter(None, aList)no change
[i for i in filter(aFunction, aList)]no change
+ +

...

+ +
+ +
+

map() global function

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

apply() global function

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

intern() global function

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

exec statement

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

repr literals (backticks)

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

try...except statement

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

raise statement

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

throw statement

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

long data type

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

xrange() global function

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

raw_input() global function

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

input() global method

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

func_* function attributes

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

xreadlines() I/O method

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

imports

+ +

FIXME intro

+

this includes fixer_imports, fixer_imports2, fixer_urllib

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +
+ +
+

lambda functions with multiple parameters

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

__class__ special class attribute

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

next() iterator method

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

__nonzero__ special class attribute

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

Number literals

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

sys.maxint

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

unicode() global function

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

Unicode string literals

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

callable() global function

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

zip() global function

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

StandardError() exception

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

types module constants

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

basestring datatype

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

itertools module

+ +

FIXME intro

+

this includes fixer_itertools, fixer_itertools_imports + + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

+ +
+

Relative imports

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

sys.exc_type, sys.exc_value, sys.exc_traceback

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

List comprehensions over tuples

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

os.getcwdu() function

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

Metaclasses

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

set() literals

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

buffer() global function

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

Whitespace around commas

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ +
+

Common idioms

+ +

FIXME intro

+ +

+ + + + + + + + + + + +
NotesPython 2Python 3
FIXMEFIXME
+ +

...

+ +
+ + + + +