Skip to content

Commit

Permalink
Add NumpySmall
Browse files Browse the repository at this point in the history
  • Loading branch information
PythonFZ committed Jan 10, 2022
1 parent abc5876 commit 82b9348
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[flake8]
ignore = W503, E203, E402, C901
ignore = W503, E203, E402, C901, F401, F403
max-line-length = 90
exclude = .git,__pycache__,docs/source/conf.py,old,build,dist,examples,tmp
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
__pycache__/
.idea
.coverage
znjson.egg-info/
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import json
import znjson

znjson.register(
znjson.converter.NumpyConverter
znjson.converter.SmallNumpyConverter
)

data = json.dumps(
Expand All @@ -34,20 +34,18 @@ The resulting ``*.json`` file is partially readable and looks like this:
````json
{
"data_np": {
"_type": "np.ndarray",
"value": "\u0093NUMPY\u0001\u0000v\u0000{'descr': '<i4', 'fortran_order': False, 'shape': (2,), } \n\u0000\u0000\u0000\u0000\u0001\u0000\u0000\u0000"
"_type": "np.ndarray_small",
"value": [
0,
1
]
},
"data": [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9
4
]
}
````
37 changes: 28 additions & 9 deletions example/example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
},
"outputs": [],
"source": [
"import znconv\n",
"import numpy as np\n",
"import json"
"import json\n",
"import znjson"
]
},
{
Expand All @@ -26,8 +26,8 @@
"metadata": {},
"outputs": [],
"source": [
"znconv.register(\n",
" znconv.converter.NumpyConverter\n",
"znjson.register(\n",
" znjson.converter.SmallNumpyConverter\n",
")"
]
},
Expand All @@ -44,7 +44,7 @@
"metadata": {},
"outputs": [],
"source": [
"example_data = {\"data\": np.arange(10), \"extra\": {\"p\": 3, \"i\": .14}}"
"example_data = {\"data_np\": np.arange(2), \"data\": [x for x in range(5)]}"
]
},
{
Expand All @@ -63,12 +63,31 @@
"name": "stdout",
"output_type": "stream",
"text": [
"{\"data\": {\"_type\": \"np.ndarray\", \"value\": \"\\u0093NUMPY\\u0001\\u0000v\\u0000{'descr': '<i4', 'fortran_order': False, 'shape': (10,), } \\n\\u0000\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000\\u0002\\u0000\\u0000\\u0000\\u0003\\u0000\\u0000\\u0000\\u0004\\u0000\\u0000\\u0000\\u0005\\u0000\\u0000\\u0000\\u0006\\u0000\\u0000\\u0000\\u0007\\u0000\\u0000\\u0000\\b\\u0000\\u0000\\u0000\\t\\u0000\\u0000\\u0000\"}, \"extra\": {\"p\": 3, \"i\": 0.14}}\n"
"{\n",
" \"data_np\": {\n",
" \"_type\": \"np.ndarray_small\",\n",
" \"value\": [\n",
" 0,\n",
" 1\n",
" ]\n",
" },\n",
" \"data\": [\n",
" 0,\n",
" 1,\n",
" 2,\n",
" 3,\n",
" 4\n",
" ]\n",
"}\n"
]
}
],
"source": [
"data_dump = json.dumps(example_data, cls=znconv.ZnEncoder)\n",
"data_dump = json.dumps(\n",
" obj=example_data,\n",
" cls=znjson.ZnEncoder,\n",
" indent=4\n",
")\n",
"print(data_dump)"
]
},
Expand All @@ -88,12 +107,12 @@
"name": "stdout",
"output_type": "stream",
"text": [
"{'data': array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), 'extra': {'p': 3, 'i': 0.14}}\n"
"{'data_np': array([0, 1]), 'data': [0, 1, 2, 3, 4]}\n"
]
}
],
"source": [
"data_loaded = json.loads(data_dump, cls=znconv.ZnDecoder)\n",
"data_loaded = json.loads(data_dump, cls=znjson.ZnDecoder)\n",
"print(data_loaded)"
]
}
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setuptools.setup(
name="znjson",
version="0.0.3",
version="0.0.4",
author="zincwarecode",
author_email="zincwarecode@gmail.com",
description="A Python Package to Encode/Decode some common file formats to json",
Expand Down
8 changes: 5 additions & 3 deletions tests/converter/test_numpy_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@

import znjson

znjson.register(znjson.converter.NumpyConverter)
znjson.register([znjson.converter.NumpyConverter, znjson.converter.SmallNumpyConverter])


@pytest.fixture
def numpy_array():
return np.arange(10)
return np.arange(100)


def test_encode(numpy_array):
_ = json.dumps(numpy_array, cls=znjson.ZnEncoder)
arr = json.dumps(numpy_array, cls=znjson.ZnEncoder)
# check that the correct encoder is used
assert arr.startswith('{"_type": "np.ndarray"')


def test_decode(numpy_array):
Expand Down
49 changes: 49 additions & 0 deletions tests/converter/test_small_numpy_converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import json

import numpy as np
import pytest

import znjson

znjson.register(znjson.converter.SmallNumpyConverter)


@pytest.fixture
def numpy_array():
return np.arange(10)


@pytest.fixture
def numpy_float_array():
return np.arange(10).astype(float)


def test_encode(numpy_array):
arr = json.dumps(numpy_array, cls=znjson.ZnEncoder)
assert arr == '{"_type": "np.ndarray_small", "value": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]}'


def test_encode_float(numpy_float_array):
arr = json.dumps(numpy_float_array, cls=znjson.ZnEncoder)
assert (
arr
== '{"_type": "np.ndarray_small", "value": [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0,'
" 7.0, 8.0, 9.0]}"
)


def test_decode(numpy_array):
arr = '{"_type": "np.ndarray_small", "value": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]}'

np.testing.assert_array_equal(numpy_array, json.loads(arr, cls=znjson.ZnDecoder))


def test_decode_float(numpy_float_array):
arr = (
'{"_type": "np.ndarray_small", "value": [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0,'
" 8.0, 9.0]}"
)

np.testing.assert_array_equal(
numpy_float_array, json.loads(arr, cls=znjson.ZnDecoder)
)
10 changes: 0 additions & 10 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,7 @@ def test_encoder_serializable(simple_dict):
_ = json.dumps(simple_dict, cls=znjson.ZnEncoder)


def test_encoder_not_serializable(pathlib_path):
with pytest.raises(TypeError):
_ = json.dumps(pathlib_path, cls=znjson.ZnEncoder)


def test_decoder_serializable(simple_dict):
data_str = json.dumps(simple_dict, cls=znjson.ZnEncoder)

assert simple_dict == json.loads(data_str, cls=znjson.ZnDecoder)


def test_decoder_not_serializable(serialized_pathlib_path):
with pytest.raises(TypeError):
_ = json.loads(serialized_pathlib_path, cls=znjson.ZnDecoder)
2 changes: 1 addition & 1 deletion znjson/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

__all__ = ["ConverterBase", "ZnDecoder", "ZnEncoder", "register", "config"]

__version__ = "0.0.3"
__version__ = "0.0.4"
3 changes: 2 additions & 1 deletion znjson/converter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

try:
from .numpy_converter import NumpyConverter
from .small_numpy_converter import SmallNumpyConverter

__all__.append("NumpyConverter")
__all__ += ["NumpyConverter", "SmallNumpyConverter"]
except ModuleNotFoundError:
pass

Expand Down
1 change: 1 addition & 0 deletions znjson/converter/numpy_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class NumpyConverter(ConverterBase):
instance = np.ndarray
representation = "np.ndarray"
order = 10

def _encode(self, obj):
with io.BytesIO() as f:
Expand Down
17 changes: 17 additions & 0 deletions znjson/converter/small_numpy_converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import numpy as np

from znjson import ConverterBase


class SmallNumpyConverter(ConverterBase):
instance = np.ndarray
representation = "np.ndarray_small"

def _encode(self, obj):
return obj.tolist()

def _decode(self, value):
return np.array(value)

def __eq__(self, other):
return len(np.shape(other)) == 1 and len(other) < 20

0 comments on commit 82b9348

Please sign in to comment.