The cStringIO Module

The cStringIO is an optional module, which contains a faster implementation of the StringIO module. It works exactly like the StringIO module, but it cannot be subclassed. Example 2-11 shows how cStringIO is used.

Example 2-11. Using the cStringIO Module

File: cstringio-example-1.py

import cStringIO

MESSAGE = "That man is depriving a village somewhere of a computer scientist."

file = cStringIO.StringIO(MESSAGE)

print file.read()

That man is depriving a village somewhere of a computer scientist.

To make your code as fast as possible, but also robust enough to run on older Python installations, you can fall back on the StringIO module if cStringIO is not available, as Example 2-12 does.

Example 2-12. Falling Back on the StringIO Module

File: cstringio-example-2.py

try:
 import cStringIO
 StringIO = cStringIO
except ImportError:
 import StringIO

print StringIO

Core Modules

More Standard Modules

Threads and Processes

Data Representation

File Formats

Mail and News Message Processing

Network Protocols

Internationalization

Multimedia Modules

Data Storage

Tools and Utilities

Platform-Specific Modules

Implementation Support Modules

Other Modules

show all menu





Python Standard Library
Python Standard Library (Nutshell Handbooks) with
ISBN: 0596000960
EAN: 2147483647
Year: 2000
Pages: 252
Authors: Fredrik Lundh
Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net