Serialization.store_pandas_object()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nop 4
1
from pandas import HDFStore
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
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):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
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)
0 ignored issues
show
Coding Style introduced by
The attribute _store was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
19
20
        else:  # pragma: no cover
21
22
            self._store = HDFStore(self._filename, mode=self._mode)
0 ignored issues
show
Coding Style introduced by
The attribute _store was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
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):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
32
33
        return self._store.keys()
34
35
    def store_pandas_object(self, path, obj, **metadata):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
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):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
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