# libpysal.io.fileio.FileIO

class libpysal.io.fileio.FileIO(dataPath='', mode='r', dataFormat=None)

  Metaclass for supporting spatial data file read and write.

  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 care 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, so…

  - … for now we’ll just return an instance of W on `mode='r'`.

  - … on `mode='w'`, `.write` will expect an instance of W.

  __init__(dataPath='', mode='r', dataFormat=None)

  Methods

  `__init__`([dataPath, mode, dataFormat]) |
  `cast`(key, typ) | Cast `key` as `typ`.
  `check`() | Prints the contents of the registry.
  `close`() | Subclasses should clean themselves up and then call this method.
  `flush`() |
  `get`(n) | Seeks the file to `n` and returns `n`.
  `getType`(dataPath, mode[, dataFormat]) | Parse the `dataPath` and return the data type.
  `open`(*args, **kwargs) | Alias for `FileIO()`.
  `read`([n]) | Read at most `n` objects, less if read hits EOF.
  `seek`(n) | Seek the FileObj to the beginning of the `n`'th record.
  `tell`() | Return ID (or offset) of next object.
  `truncate`([size]) | Should be implemented by subclasses and redefine this doc string.
  `write`(obj) | Must be implemented by subclasses that support 'w' subclasses Should increment `.pos`.

  Attributes

  `by_row` |
  `ids` |
  `rIds` |

  property by_row
  cast(key, typ)

    Cast `key` as `typ`.

    Raises:
      `TypeError`

        Raised when a cast object in not callable.

      `KeyError`

        Raised when a key is not present.

  classmethod check()

    Prints the contents of the registry.

  close()

    Subclasses should clean themselves up and then call this method.

  flush()
    Raises:
      `NotImplementedError`
  get(n: int) → list

    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: str, mode: str, dataFormat=None) → str

    Parse the `dataPath` and return the data type.

  property ids
  classmethod open(*args, **kwargs)

    Alias for `FileIO()`.

  property rIds: dict | None
  read(n=-1) → list | None

    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.

    Raises:
      `StopIteration`

        Raised at the EOF.

  seek(n: int)

    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() → int

    Return ID (or offset) of next object.

  truncate(size=None)

    Should be implemented by subclasses and redefine this doc string.

    Raises:
      `NotImplementedError`
  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.

    Raises:
      `NotImplementedError`