Skip to content

Commit b2a6ad3

Browse files
authored
Add class conv (#5)
* add pickle for class conversion * fix lint * Add NumpySmall * rename representation to identifier and order to level. * Update .gitignore * update notebook with `znjson.config.ACTIVE_CONVERTER` * revert: rename `representation` to `identifier`
1 parent 29b024b commit b2a6ad3

File tree

11 files changed

+46
-17
lines changed

11 files changed

+46
-17
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
__pycache__/
22
.idea
33
.coverage
4-
znjson.egg-info/
4+
znjson.egg-info/

example/example.ipynb

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,25 @@
1717
"cell_type": "markdown",
1818
"metadata": {},
1919
"source": [
20-
"1. Register the required converter"
20+
"1. You can register / deregister `znjson.converter`. By default all available converters will be registered."
2121
]
2222
},
2323
{
2424
"cell_type": "code",
2525
"execution_count": 2,
2626
"metadata": {},
27-
"outputs": [],
27+
"outputs": [
28+
{
29+
"data": {
30+
"text/plain": "[znjson.converter.small_numpy_converter.SmallNumpyConverter,\n znjson.converter.numpy_converter_base64.NumpyConverter,\n znjson.converter.pathlib_converter.PathlibConverter,\n znjson.converter.pandas_converter.PandasConverter,\n znjson.converter.numpy_converter_latin1.NumpyConverterLatin1,\n znjson.converter.class_converter.ClassConverter]"
31+
},
32+
"execution_count": 2,
33+
"metadata": {},
34+
"output_type": "execute_result"
35+
}
36+
],
2837
"source": [
29-
"znjson.register(znjson.converter.SmallNumpyConverter)"
38+
"znjson.config.ACTIVE_CONVERTER"
3039
]
3140
},
3241
{
@@ -81,7 +90,11 @@
8190
}
8291
],
8392
"source": [
84-
"data_dump = json.dumps(obj=example_data, cls=znjson.ZnEncoder, indent=4)\n",
93+
"data_dump = json.dumps(\n",
94+
" obj=example_data,\n",
95+
" cls=znjson.ZnEncoder,\n",
96+
" indent=4\n",
97+
")\n",
8598
"print(data_dump)"
8699
]
87100
},

tests/converter/test_small_numpy_converter.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ def numpy_array():
1111
return np.arange(10)
1212

1313

14+
@pytest.fixture
15+
def numpy_array_large():
16+
return np.arange(1000)
17+
18+
1419
@pytest.fixture
1520
def numpy_float_array():
1621
return np.arange(10).astype(float)
@@ -21,6 +26,11 @@ def test_encode(numpy_array):
2126
assert arr == '{"_type": "np.ndarray_small", "value": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]}'
2227

2328

29+
def test_encode_large(numpy_array_large):
30+
arr = json.dumps(numpy_array_large, cls=znjson.ZnEncoder)
31+
assert arr.startswith('{"_type": "np.ndarray64"')
32+
33+
2434
def test_encode_float(numpy_float_array):
2535
arr = json.dumps(numpy_float_array, cls=znjson.ZnEncoder)
2636
assert (

znjson/base.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ class ConverterBase(abc.ABC):
1212
the type of the object to convert, e.g. np.ndarray or pathlib.Path
1313
representation: str
1414
the name of the object to convert. should e.g. be `pathlib.Path`
15-
order: int
16-
The order in which the encoding should be applied. A higher number means this
17-
is tried later. E.g. pickle should have a higher order using other serializers
15+
level: int
16+
The level in which the encoding should be applied. A higher number means it will
17+
try this first. E.g. test small numpy conversion before pickle
1818
first.
1919
"""
2020

2121
instance: type = None
2222
representation: str = None
23-
order: int = 0
23+
level: int = 0
2424

2525
@abc.abstractmethod
2626
def _encode(self, obj) -> str:
@@ -97,4 +97,4 @@ def __eq__(self, other) -> bool:
9797
return isinstance(other, self.instance)
9898

9999
def __lt__(self, other: ConverterBase):
100-
return self.order < other.order
100+
return self.level < other.level

znjson/config.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ class Config:
99
ACTIVE_CONVERTER: List[Type[ConverterBase]] = field(default_factory=list)
1010

1111
def sort(self):
12-
"""Sort the ACTIVE_CONVERTER by their order"""
13-
active_converters = set(self.ACTIVE_CONVERTER)
14-
sort = sorted([x() for x in active_converters])
15-
self.ACTIVE_CONVERTER = [type(x) for x in sort]
12+
"""Sort the ACTIVE_CONVERTER by their level
13+
14+
Start from high levels to low levels
15+
"""
16+
active_converter = set(self.ACTIVE_CONVERTER)
17+
active_converter = sorted([x() for x in active_converter], reverse=True)
18+
self.ACTIVE_CONVERTER = [type(x) for x in active_converter]
1619

1720

1821
config = Config()

znjson/converter/class_converter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class ClassConverter(ConverterBase):
99
instance = object
1010
representation = "class"
11-
order = 100
11+
level = 0
1212

1313
def _encode(self, obj):
1414
with io.BytesIO() as f:

znjson/converter/numpy_converter_base64.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class NumpyConverter(ConverterBase):
1010
instance = np.ndarray
1111
representation = "np.ndarray64"
12-
order = 10
12+
level = 30
1313

1414
def _encode(self, obj):
1515
with io.BytesIO() as f:

znjson/converter/numpy_converter_latin1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class NumpyConverterLatin1(ConverterBase):
1010

1111
instance = np.ndarray
1212
representation = "np.ndarray"
13-
order = 999
13+
level = 0
1414

1515
def _encode(self, obj):
1616
with io.BytesIO() as f:

znjson/converter/pandas_converter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
class PandasConverter(ConverterBase):
99
instance = pandas.DataFrame
1010
representation = "pandas.DataFrame"
11+
level = 10
1112

1213
def _encode(self, obj: pandas.DataFrame):
1314
with io.BytesIO() as f:

znjson/converter/pathlib_converter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
class PathlibConverter(ConverterBase):
77
instance = pathlib.Path
88
representation = "pathlib.Path"
9+
level = 10
910

1011
def _encode(self, obj: pathlib.Path):
1112
return obj.as_posix()

0 commit comments

Comments
 (0)