| Total Complexity | 7 |
| Total Lines | 50 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | from pandas import HDFStore |
||
|
|
|||
| 2 | |||
| 3 | |||
| 4 | # This object is to decouple the serialization from any particular implementation. At the moment we use HDF5 through |
||
| 5 | # pandas but this might change in the future. Without changing the external API, only changes here will be necessary. |
||
| 6 | class Serialization(object): |
||
| 7 | |||
| 8 | def __init__(self, filename, mode='r', compress=True): |
||
| 9 | |||
| 10 | self._filename = filename |
||
| 11 | self._compress = compress |
||
| 12 | self._mode = mode |
||
| 13 | |||
| 14 | def __enter__(self): |
||
| 15 | |||
| 16 | if self._compress: |
||
| 17 | |||
| 18 | self._store = HDFStore(self._filename, complib='blosc:lz4', complevel=9, mode=self._mode) |
||
| 19 | |||
| 20 | else: # pragma: no cover |
||
| 21 | |||
| 22 | self._store = HDFStore(self._filename, mode=self._mode) |
||
| 23 | |||
| 24 | return self |
||
| 25 | |||
| 26 | def __exit__(self, exc_type, exc_val, exc_tb): |
||
| 27 | |||
| 28 | self._store.close() |
||
| 29 | |||
| 30 | @property |
||
| 31 | def keys(self): |
||
| 32 | |||
| 33 | return self._store.keys() |
||
| 34 | |||
| 35 | def store_pandas_object(self, path, obj, **metadata): |
||
| 36 | |||
| 37 | self._store.put(path, obj, format='fixed') |
||
| 38 | |||
| 39 | self._store.get_storer(path).attrs.metadata = metadata |
||
| 40 | |||
| 41 | def retrieve_pandas_object(self, path): |
||
| 42 | |||
| 43 | # Get the metadata |
||
| 44 | metadata = self._store.get_storer(path).attrs.metadata |
||
| 45 | |||
| 46 | # Get the object |
||
| 47 | obj = self._store.get(path) |
||
| 48 | |||
| 49 | return obj, metadata |
||
| 50 |
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.