Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
StringIO Namespace Reference

Data Structures

class  StringIO
 

Functions

def test
 

Variables

int EINVAL = 22
 
list __all__ = ["StringIO"]
 

Detailed Description

File-like objects that read from or write to a string buffer.

This implements (nearly) all stdio methods.

f = StringIO()      # ready for writing
f = StringIO(buf)   # ready for reading
f.close()           # explicitly release resources held
flag = f.isatty()   # always false
pos = f.tell()      # get current position
f.seek(pos)         # set current position
f.seek(pos, mode)   # mode 0: absolute; 1: relative; 2: relative to EOF
buf = f.read()      # read until EOF
buf = f.read(n)     # read up to n bytes
buf = f.readline()  # read until end of line ('\n') or EOF
list = f.readlines()# list of f.readline() results until EOF
f.truncate([size])  # truncate file at to at most size (default: current pos)
f.write(buf)        # write at current position
f.writelines(list)  # for line in list: f.write(line)
f.getvalue()        # return whole file's contents as a string

Notes:
- Using a real file is often faster (but less convenient).
- There's also a much faster implementation in C, called cStringIO, but
  it's not subclassable.
- fileno() is left unimplemented so that code which uses it triggers
  an exception early.
- Seeking far beyond EOF and then writing will insert real null
  bytes that occupy space in the buffer.
- There's a simple test set (see end of this file).

Function Documentation

def StringIO.test ( )

Definition at line 176 of file StringIO.py.

References aifc.open().

177 def test():
178  import sys
179  if sys.argv[1:]:
180  file = sys.argv[1]
181  else:
182  file = '/etc/passwd'
183  lines = open(file, 'r').readlines()
184  text = open(file, 'r').read()
185  f = StringIO()
186  for line in lines[:-2]:
187  f.write(line)
188  f.writelines(lines[-2:])
189  if f.getvalue() != text:
190  raise RuntimeError, 'write failed'
191  length = f.tell()
192  print 'File length =', length
193  f.seek(len(lines[0]))
194  f.write(lines[1])
195  f.seek(0)
196  print 'First line =', `f.readline()`
197  here = f.tell()
198  line = f.readline()
199  print 'Second line =', `line`
200  f.seek(-len(line), 1)
201  line2 = f.read(len(line))
202  if line != line2:
203  raise RuntimeError, 'bad result after seek back'
204  f.seek(len(line2), 1)
205  list = f.readlines()
206  line = list[-1]
207  f.seek(f.tell() - len(line))
208  line2 = f.read()
209  if line != line2:
210  raise RuntimeError, 'bad result after seek back from EOF'
211  print 'Read', len(list), 'more lines'
212  print 'File length =', f.tell()
213  if f.tell() != length:
214  raise RuntimeError, 'bad length'
215  f.close()

Variable Documentation

list __all__ = ["StringIO"]

Definition at line 37 of file StringIO.py.

int EINVAL = 22

Definition at line 35 of file StringIO.py.