Skip to content

Commit

Permalink
merge main into add_class_conv
Browse files Browse the repository at this point in the history
  • Loading branch information
PythonFZ committed Mar 8, 2022
2 parents 40b6b05 + dcba60a commit 66204ff
Show file tree
Hide file tree
Showing 18 changed files with 818 additions and 324 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fail_fast: true

repos:
- repo: https://github.com/psf/black
rev: 21.12b0
rev: 22.1.0
hooks:
- id: black

Expand Down
528 changes: 251 additions & 277 deletions LICENSE

Large diffs are not rendered by default.

378 changes: 378 additions & 0 deletions example/compare_to_jsonpickle.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,378 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import jsonpickle\n",
"import znjson\n",
"import json\n",
"import sys\n",
"import numpy as np"
]
},
{
"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",
"data3 = {f\"{x}\": np.random.normal(size=(x, x)) for x in range(30)}\n",
"data4 = {\n",
" \"numpy\": np.array([1, 2, 3]),\n",
" \"list\": [1, 2, 3],\n",
" \"nested\": {\"numpy\": np.array([1, 2, 3]), \"list\": [1, 2, 3]},\n",
"}"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "markdown",
"source": [
"The following results are not affected by running one timeit before the other."
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%% md\n"
}
}
},
{
"cell_type": "code",
"execution_count": 4,
"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"
]
}
],
"source": [
"%timeit json.dumps(data1, cls=znjson.ZnEncoder)\n",
"%timeit json.dumps(data2, cls=znjson.ZnEncoder)\n",
"%timeit json.dumps(data3, cls=znjson.ZnEncoder)\n",
"%timeit json.dumps(data4, cls=znjson.ZnEncoder)"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "code",
"execution_count": 5,
"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"
]
}
],
"source": [
"%timeit jsonpickle.dumps(data1)\n",
"%timeit jsonpickle.dumps(data2)\n",
"%timeit jsonpickle.dumps(data3)\n",
"%timeit jsonpickle.dumps(data4)"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "code",
"execution_count": 6,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"numpy\": {\n",
" \"_type\": \"np.ndarray64\",\n",
" \"value\": \"k05VTVBZAQB2AHsnZGVzY3InOiAnPGk0JywgJ2ZvcnRyYW5fb3JkZXInOiBGYWxzZSwgJ3NoYXBlJzogKDMsKSwgfSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAoBAAAAAgAAAAMAAAA=\"\n",
" },\n",
" \"list\": [\n",
" 1,\n",
" 2,\n",
" 3\n",
" ],\n",
" \"nested\": {\n",
" \"numpy\": {\n",
" \"_type\": \"np.ndarray64\",\n",
" \"value\": \"k05VTVBZAQB2AHsnZGVzY3InOiAnPGk0JywgJ2ZvcnRyYW5fb3JkZXInOiBGYWxzZSwgJ3NoYXBlJzogKDMsKSwgfSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAoBAAAAAgAAAAMAAAA=\"\n",
" },\n",
" \"list\": [\n",
" 1,\n",
" 2,\n",
" 3\n",
" ]\n",
" }\n",
"}\n"
]
}
],
"source": [
"print(json.dumps(data4, cls=znjson.ZnEncoder, indent=2))"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "code",
"execution_count": 7,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"numpy\": {\n",
" \"py/reduce\": [\n",
" {\n",
" \"py/function\": \"numpy.core.multiarray._reconstruct\"\n",
" },\n",
" {\n",
" \"py/tuple\": [\n",
" {\n",
" \"py/type\": \"numpy.ndarray\"\n",
" },\n",
" {\n",
" \"py/tuple\": [\n",
" 0\n",
" ]\n",
" },\n",
" {\n",
" \"py/b64\": \"Yg==\"\n",
" }\n",
" ]\n",
" },\n",
" {\n",
" \"py/tuple\": [\n",
" 1,\n",
" {\n",
" \"py/tuple\": [\n",
" 3\n",
" ]\n",
" },\n",
" {\n",
" \"py/reduce\": [\n",
" {\n",
" \"py/type\": \"numpy.dtype\"\n",
" },\n",
" {\n",
" \"py/tuple\": [\n",
" \"i4\",\n",
" false,\n",
" true\n",
" ]\n",
" },\n",
" {\n",
" \"py/tuple\": [\n",
" 3,\n",
" \"<\",\n",
" null,\n",
" null,\n",
" null,\n",
" -1,\n",
" -1,\n",
" 0\n",
" ]\n",
" }\n",
" ]\n",
" },\n",
" false,\n",
" {\n",
" \"py/b64\": \"AQAAAAIAAAADAAAA\"\n",
" }\n",
" ]\n",
" }\n",
" ]\n",
" },\n",
" \"list\": [\n",
" 1,\n",
" 2,\n",
" 3\n",
" ],\n",
" \"nested\": {\n",
" \"numpy\": {\n",
" \"py/reduce\": [\n",
" {\n",
" \"py/function\": \"numpy.core.multiarray._reconstruct\"\n",
" },\n",
" {\n",
" \"py/tuple\": [\n",
" {\n",
" \"py/type\": \"numpy.ndarray\"\n",
" },\n",
" {\n",
" \"py/tuple\": [\n",
" 0\n",
" ]\n",
" },\n",
" {\n",
" \"py/b64\": \"Yg==\"\n",
" }\n",
" ]\n",
" },\n",
" {\n",
" \"py/tuple\": [\n",
" 1,\n",
" {\n",
" \"py/tuple\": [\n",
" 3\n",
" ]\n",
" },\n",
" {\n",
" \"py/id\": 2\n",
" },\n",
" false,\n",
" {\n",
" \"py/b64\": \"AQAAAAIAAAADAAAA\"\n",
" }\n",
" ]\n",
" }\n",
" ]\n",
" },\n",
" \"list\": [\n",
" 1,\n",
" 2,\n",
" 3\n",
" ]\n",
" }\n",
"}\n"
]
}
],
"source": [
"print(jsonpickle.dumps(data4, indent=2))"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "code",
"execution_count": 8,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6141\n",
"45615\n",
"98708\n",
"732\n"
]
}
],
"source": [
"print(sys.getsizeof(jsonpickle.dumps(data1)))\n",
"print(sys.getsizeof(jsonpickle.dumps(data2)))\n",
"print(sys.getsizeof(jsonpickle.dumps(data3)))\n",
"print(sys.getsizeof(jsonpickle.dumps(data4)))"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "code",
"execution_count": 9,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5995\n",
"45467\n",
"97859\n",
"573\n"
]
}
],
"source": [
"print(sys.getsizeof(json.dumps(data1, cls=znjson.ZnEncoder)))\n",
"print(sys.getsizeof(json.dumps(data2, cls=znjson.ZnEncoder)))\n",
"print(sys.getsizeof(json.dumps(data3, cls=znjson.ZnEncoder)))\n",
"print(sys.getsizeof(json.dumps(data4, cls=znjson.ZnEncoder)))"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.black]
line-length = 90
experimental_string_processing = true
preview = true

[tool.isort]
profile = 'black'
Expand Down

0 comments on commit 66204ff

Please sign in to comment.