pickleio.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # -*- coding: utf-8 -*-
  2. """
  3. Module for reading/writing data from/to Python pickle format.
  4. Class:
  5. PickleIO
  6. Supported: Read/Write
  7. Authors: Andrew Davison
  8. """
  9. try:
  10. import cPickle as pickle # Python 2
  11. except ImportError:
  12. import pickle # Python 3
  13. from neo.io.baseio import BaseIO
  14. from neo.core import (Block, Segment,
  15. AnalogSignal, SpikeTrain)
  16. class PickleIO(BaseIO):
  17. """
  18. A class for reading and writing Neo data from/to the Python "pickle" format.
  19. Note that files in this format may not be readable if using a different version
  20. of Neo to that used to create the file. It should therefore not be used for
  21. long-term storage, but rather for intermediate results in a pipeline.
  22. """
  23. is_readable = True
  24. is_writable = True
  25. has_header = False
  26. is_streameable = False # TODO - correct spelling to "is_streamable"
  27. supported_objects = [Block, Segment, AnalogSignal, SpikeTrain] # should extend to other classes.
  28. readable_objects = supported_objects
  29. writeable_objects = supported_objects
  30. mode = 'file'
  31. name = "Python pickle file"
  32. extensions = ['pkl', 'pickle']
  33. def read_block(self, lazy=False, cascade=True):
  34. with open(self.filename, "rb") as fp:
  35. block = pickle.load(fp)
  36. return block
  37. def write_block(self, block):
  38. with open(self.filename, "wb") as fp:
  39. pickle.dump(block, fp)