This Tutorial

Mastering Python 3 I/O

David Beazley

Presented at PyCon'2010 Atlanta, Georgia

Copyright (C) 2010, David Beazley,

1

This Tutorial

? It's about a very specific aspect of Python 3 ? Maybe the most important part of Python 3 ? Namely, the reimplemented I/O system

Copyright (C) 2010, David Beazley,

2

Why I/O?

? Real programs interact with the world ? They read and write files ? They send and receive messages ? They don't compute Fibonacci numbers

? I/O is at the heart of almost everything that Python is about (scripting, gluing, frameworks, C extensions, etc.)

3

Copyright (C) 2010, David Beazley,

The I/O Problem

? Of all of the changes made in Python 3, it is my observation that I/O handling changes are the most problematic for porting

? Python 3 re-implements the entire I/O stack ? Python 3 introduces new programming idioms ? I/O handling issues can't be fixed by automatic

code conversion tools (2to3)

Copyright (C) 2010, David Beazley,

4

The Plan

? We're going to take a detailed top-to-bottom tour of the whole Python 3 I/O system ? Text handling ? Binary data handling ? System interfaces ? The new I/O stack ? Standard library issues ? Memory views, buffers, etc.

Copyright (C) 2010, David Beazley,

5

Prerequisites

? I assume that you are already reasonably familiar with how I/O works in Python 2 ? str vs. unicode ? print statement ? open() and file methods ? Standard library modules ? General awareness of I/O issues

? Prior experience with Python 3 not required

Copyright (C) 2010, David Beazley,

6

Performance Disclosure

? There are some performance tests

? Execution environment for tests:

? 2.4 GHZ 4-Core MacPro, 3GB memory

? OS-X 10.6.2 (Snow Leopard)

? All Python interpreters compiled from source using same config/compiler

? Tutorial is not meant to be a detailed performance study so all results should be

viewed as rough estimates

Copyright (C) 2010, David Beazley,

7

Let's Get Started

? I have made a few support files:



? You can try some of the examples as we go ? However, it is fine to just watch/listen and try

things on your own later

Copyright (C) 2010, David Beazley,

8

Part 1

Introducing Python 3

Copyright (C) 2010, David Beazley,

9

Syntax Changes

? As you know, Python 3 changes syntax ? print is now a function print()

print("Hello World")

? Exception handling syntax changed slightly

try:

added

...

except IOError as e:

...

? Yes, your old code will break

Copyright (C) 2010, David Beazley,

10

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download