Skip to content

Commit

Permalink
replace _decode with decode (#16)
Browse files Browse the repository at this point in the history
* introduce `decode` and `decode_obj` and replace `_decode` method.

* add docstrings, add black, isort, pylint

* cleanup code

* fix some cyclic imports

* some refactoring and updating docs

* poetry update + test version

* ensure backwards compatibility

* Update README.md

* disable cron tests

* poetry update
  • Loading branch information
PythonFZ authored Oct 4, 2022
1 parent edadb06 commit eb61655
Show file tree
Hide file tree
Showing 25 changed files with 2,441 additions and 386 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/pytest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ name: pytest
on:
push:
pull_request:
schedule:
- cron: '14 3 * * 1' # at 03:14 on Monday.
# schedule:
# - cron: '14 3 * * 1' # at 03:14 on Monday.

jobs:
build:
Expand All @@ -22,7 +22,6 @@ jobs:
- 3.8
os:
- ubuntu-latest
# - windows-latest

steps:
- uses: actions/checkout@v2
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,17 @@ class DatetimeConverter(ConverterBase):
representation = "datetime"
instance = datetime

def _encode(self, obj: datetime) -> str:
def encode(self, obj: datetime) -> str:
"""Convert the datetime object to str / isoformat"""
return obj.isoformat()
def _decode(self, value: str) -> datetime:
def decode(self, value: str) -> datetime:
"""Create datetime object from str / isoformat"""
return datetime.fromisoformat(value)
````

This allows us to use this new serializer:
````python
znjson.register(DatetimeConverter) # we need to register the new converter first
znjson.config.register(DatetimeConverter) # we need to register the new converter first
json_string = json.dumps(dt, cls=znjson.ZnEncoder, indent=4)
json.loads(json_string, cls=znjson.ZnDecoder)
````
Expand Down
102 changes: 36 additions & 66 deletions example/compare_to_jsonpickle.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,6 @@
"cell_type": "code",
"execution_count": 2,
"outputs": [],
"source": [
"znjson.register(znjson.converter.NumpyConverterBase64)"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "code",
"execution_count": 3,
"outputs": [],
"source": [
"data1 = {\"a\": np.arange(1000), \"b\": [x for x in range(100)]}\n",
"data2 = {\"a\": np.random.normal(size=(16, 16, 16)), \"b\": [x / 3 for x in range(100)]}\n",
Expand All @@ -44,10 +30,7 @@
"}"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
"collapsed": false
}
},
{
Expand All @@ -56,24 +39,21 @@
"The following results are not affected by running one timeit before the other."
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%% md\n"
}
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 3,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"59.6 µs ± 2.02 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n",
"241 µs ± 466 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n",
"1.07 ms ± 14 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n",
"50.7 µs ± 334 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
"253 µs ± 53.8 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)\n",
"1.11 ms ± 128 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)\n",
"4.25 ms ± 861 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n",
"55.3 µs ± 11 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)\n"
]
}
],
Expand All @@ -84,24 +64,21 @@
"%timeit json.dumps(data4, cls=znjson.ZnEncoder)"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 4,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"199 µs ± 1.32 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n",
"386 µs ± 6.62 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n",
"2.24 ms ± 22.5 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n",
"200 µs ± 9.13 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
"736 µs ± 72.7 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)\n",
"1.65 ms ± 174 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)\n",
"8.57 ms ± 808 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n",
"734 µs ± 114 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)\n"
]
}
],
Expand All @@ -112,24 +89,25 @@
"%timeit jsonpickle.dumps(data4)"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 5,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"numpy\": {\n",
" \"_type\": \"np.ndarray64\",\n",
" \"value\": \"k05VTVBZAQB2AHsnZGVzY3InOiAnPGk0JywgJ2ZvcnRyYW5fb3JkZXInOiBGYWxzZSwgJ3NoYXBlJzogKDMsKSwgfSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAoBAAAAAgAAAAMAAAA=\"\n",
" \"_type\": \"np.ndarray_small\",\n",
" \"value\": [\n",
" 1,\n",
" 2,\n",
" 3\n",
" ]\n",
" },\n",
" \"list\": [\n",
" 1,\n",
Expand All @@ -138,8 +116,12 @@
" ],\n",
" \"nested\": {\n",
" \"numpy\": {\n",
" \"_type\": \"np.ndarray64\",\n",
" \"value\": \"k05VTVBZAQB2AHsnZGVzY3InOiAnPGk0JywgJ2ZvcnRyYW5fb3JkZXInOiBGYWxzZSwgJ3NoYXBlJzogKDMsKSwgfSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAoBAAAAAgAAAAMAAAA=\"\n",
" \"_type\": \"np.ndarray_small\",\n",
" \"value\": [\n",
" 1,\n",
" 2,\n",
" 3\n",
" ]\n",
" },\n",
" \"list\": [\n",
" 1,\n",
Expand All @@ -155,15 +137,12 @@
"print(json.dumps(data4, cls=znjson.ZnEncoder, indent=2))"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 6,
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -291,15 +270,12 @@
"print(jsonpickle.dumps(data4, indent=2))"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 7,
"outputs": [
{
"name": "stdout",
Expand All @@ -319,15 +295,12 @@
"print(sys.getsizeof(jsonpickle.dumps(data4)))"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 8,
"outputs": [
{
"name": "stdout",
Expand All @@ -336,7 +309,7 @@
"5995\n",
"45467\n",
"97859\n",
"573\n"
"219\n"
]
}
],
Expand All @@ -347,10 +320,7 @@
"print(sys.getsizeof(json.dumps(data4, cls=znjson.ZnEncoder)))"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
"collapsed": false
}
}
],
Expand All @@ -375,4 +345,4 @@
},
"nbformat": 4,
"nbformat_minor": 0
}
}
Loading

0 comments on commit eb61655

Please sign in to comment.