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]
>>> 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.
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 |