pysal

Previous topic

pysal.core — Core Data Structures and IO

This Page

FileIO — File Input/Output System for PySAL: Python Spatial Analysis Library

The FileIO File Input/Output System for PySAL

New in version 1.0.

FileIO: Module for reading and writing various file types in a Pythonic way. This module should not be used directly, instead... >>> import pysal.FileIO as FileIO Readers and Writers will mimic python file objects. .seek(n) seeks to the n’th object .read(n) reads n objects, default == all .next() reads the next object Example Usage:

>>> import pysal
>>> pysal.open == pysal.core.FileIO.FileIO
True
>>> shp = pysal.open('shapefile.shp','r')
>>> wktout = pysal.open('output.wkt','w')
>>> for polygon in shp:
...     wktout.write(polygon)
...
>>> wktout.close()
>>> shp.seek(0)
>>> poly = shp.read(1)
>>> ring = poly[0]
>>> pt = ring[0]
class pysal.core.FileIO.FileIO(dataPath='', mode='r')
>>> geoReaderObj = FileIO.open('filename.shp','r')
>>> geoWriterObj = FileIO.open('filename.shp','w')
>>> TableObj = FileIO.open('filename.dbf','r')
>>> CVS = FileIO.open('filename.csv','w')
>>> weights = FileIO.open('weights.gal','r')

How this works, FileIO.open(*args) == FileIO(*args) When creating a new instance of FileIO the .__new__ method intercepts .__new__ parses the filename to determine the fileType next, .__registry and checked for that type. Each type supports one or more modes [‘r’,’w’,’a’,etc] If we support the type and mode, an instance of the appropriate handler

is created and returned.
All handlers must inherit from this class, and by doing so are automatically
added to the .__registry and are forced to conform to the prescribed API.

The metaclass takes cares of the registration by parsing the class definition.

It doesn’t make much sense to treat weights in the same way as shapefiles and dbfs, ....for now we’ll just return an instance of W on mode=’r’ .... on mode=’w’, .write will expect an instance of W

Methods

by_row
cast
check
close
flush
get
getType
next
open
read
seek
tell
truncate
write
cast(key, typ)
cast key as typ
classmethod check()
Prints the contents of the registry
close()
subclasses should clean themselves up and then call this method
get(n)
Seeks the file to n and returns n If .ids is set n should be an id, else, n should be an offset
static getType(dataPath)
Parse the dataPath and return the data type
next()
A FileIO object is its own iterator, see StringIO
classmethod open(*args, **kwargs)
Alias for FileIO()
read(n=-1)
Read at most n objects, less if read hits EOF if size is negative or omitted read all objects until EOF returns None if EOF is reached before any objects.
seek(n)
Seek the FileObj to the beginning of the n’th record, if ids are set, seeks to the beginning of the record at id, n
tell()
Return id (or offset) of next object
truncate(size=None)
Should be implemented by subclasses and redefine this doc string
write(obj)
Must be implemented by subclasses that support ‘w’ subclasses should increment .pos subclasses should also check if obj is an instance of type(list) and redefine this doc string