pickleio.py 1.4 KB

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