Hi All,

For those who couldn't make last Thursday's meeting, here are some meeting
notes which you can also find on http://pynw.org.uk along with a group
picture!

This month, on St. Patrick's Day (Thursday 17th March 2011), Python
Northwest got together for a coding session at MadLab to convert some Python
2 code to Python 3. Eleven Pythoneers were in attendance.

David Jones warmed us up by showing us a case study on porting ccc-gistemp
() from Python 2 to Python 3. David described how with each Python 2
release, ccc-gistemp would be ported to Python 3 using the 2to3 tool
provided with Python. Hand edits to the resulting Python code were needed,
but the aim is to edit the Python 2 code such that little or no hand edits
are required.

As well as the usual gotchas associated with porting Python code, David
described the lesser known gotcha of using builtin Python types such as
"long" as variable names. The type "long" is obsolete in Python 3 and is
converted to "int" by 2to3. Unfortunately, the converter doesn't know if the
word "long" is a type or a user defined variable so all instances of the
word "long" are converted to "int".

Visit
http://code.google.com/p/ccc-gistemp/source/browse/#svn%2Ftrunk%2Fdoc%2F2011-03-15%2Fpython3for
the case study.

We then proceeded to port PyPNG (http://code.google.com/p/pypng/), a pure
Python PNG encoder/decoder, from Python 2 to Python 3. After running 2to3 on
the code we were faced with several errors to correct. A major change in
Python 3 is the clean separation between strings and bytes so it'll come as
no surprise for a file format encoder/decoder that most errors concerned the
handling of strings versus bytes.

Other interesting points to come out of the exercise include:

- The map function no longer returns a list in Python 3. Interestingly
map(function, sequence) is converted to a list comprehension only if
function is a lambda expression, otherwise the idiom list(map()) is used
which can lead to an excessive use of list().
- The 'hex' character encoding no longer exists and the use of the binascii
module is recommended. To encode and decode to hex, use binascii.hexlify and
binascii.unhexlify. The gratuitous 'l' in 'hexlify' was hotly debated.
- We were pleasantly surprised that several Python 3 classes used while
porting (e.g. the use of BytesIO rather than StringIO and the use of
binascii) are available in Python 2.6. By updating the Python 2 source to
use these classes, conversion should require little hand editing. It also
highlighted the fact that supporting Python 3 as well as versions older than
Python 2.6 which don't contain these classes would require a lot more work.

After the coding session, we decamped to the English Lounge (aptly named
considering it was St. Patrick's Day) to continue Pythonic discussions.

Thanks to all for attending and contributing, thanks to MadLab for providing
the meeting space, and thanks to David Jones for presenting his case study
and taking the reins with the coding.

Next month's meeting will be on Thursday 21st April and will be a talks
meeting.  Any and all suggestions for talks warmly welcomed!

Cheers,

Safe


Safe Hammad
http://safehammad.com
@safehammad

-- 
To post: [email protected]
To unsubscribe: [email protected]
Feeds: http://groups.google.com/group/python-north-west/feeds
More options: http://groups.google.com/group/python-north-west

Reply via email to