From 988f5833fe4b272a977a89afe1fac119860b4a5d Mon Sep 17 00:00:00 2001 From: turulomio Date: Mon, 4 Dec 2023 13:18:09 +0100 Subject: [PATCH 1/3] updated project file --- pydicts.epj | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pydicts.epj b/pydicts.epj index e0353b0..7832d18 100644 --- a/pydicts.epj +++ b/pydicts.epj @@ -2,7 +2,7 @@ "header": { "comment": "eric project file for project pydicts", "copyright": "Copyright (C) 2023 , ", - "saved": "2023-11-26, 17:52:26" + "saved": "2023-12-04, 13:17:59" }, "project": { "AUTHOR": "", @@ -77,10 +77,12 @@ "pydicts/casts.py", "pydicts/classes.py", "pydicts/devscripts.py", + "pydicts/exceptions.py", "pydicts/lod.py", "pydicts/lod_xyv.py", "pydicts/lod_ymv.py", "pydicts/lol.py", + "pydicts/myjsonencoder.py", "pydicts/pylatex.py", "pydicts/tests/__init__.py", "pydicts/tests/test_casts.py", From 8108eea1c4bd10c69e75b44f518d9cde6559f254 Mon Sep 17 00:00:00 2001 From: turulomio Date: Mon, 4 Dec 2023 13:26:24 +0100 Subject: [PATCH 2/3] Added basic test --- pydicts.epj | 3 ++- pydicts/myjsonencoder.py | 35 ---------------------------- pydicts/tests/test_myjsonencoder.py | 36 +++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 36 deletions(-) create mode 100644 pydicts/tests/test_myjsonencoder.py diff --git a/pydicts.epj b/pydicts.epj index 7832d18..46b9d18 100644 --- a/pydicts.epj +++ b/pydicts.epj @@ -2,7 +2,7 @@ "header": { "comment": "eric project file for project pydicts", "copyright": "Copyright (C) 2023 , ", - "saved": "2023-12-04, 13:17:59" + "saved": "2023-12-04, 13:20:21" }, "project": { "AUTHOR": "", @@ -89,6 +89,7 @@ "pydicts/tests/test_lod.py", "pydicts/tests/test_lod_ymv.py", "pydicts/tests/test_lol.py", + "pydicts/tests/test_myjsonencoder.py", "pydicts/tests/test_pylatex.py" ], "SOURCESDIR": "", diff --git a/pydicts/myjsonencoder.py b/pydicts/myjsonencoder.py index 780dab3..9608e36 100644 --- a/pydicts/myjsonencoder.py +++ b/pydicts/myjsonencoder.py @@ -218,38 +218,3 @@ def hooks_MyJSONEncoderAsFloat(iter_value): def hooks_MyJSONEncoderAsString(iter_value): return hooks(iter_value, DecimalsWay.String) - -if __name__ == '__main__': - from datetime import timezone - d={} - d["None"]=None - d["Integer"]=12112 - d["Float"]=12121.1212 - d["Date"]=date.today() - d["Datetime"]=datetime.now() - d["Timedelta"]=timedelta(hours=4, days=2, minutes=12, seconds=12) - d["Time"]=time(12, 12, 12, 123456) - d["Datetime aware"]=d["Datetime"].replace(tzinfo=timezone.utc) - d["Bytes"]=b"Byte array" - d["Decimal"]=Decimal("12.12123414") - print ("Dictionary") - print(d) - print() - print ("MyJSONEncoder_dumps") - print (MyJSONEncoder_dumps(d)) - print() - print ("MyJSONEncoder_loads") - print(MyJSONEncoder_loads(MyJSONEncoder_dumps(d))) - print() - print ("MyJSONEncoderDecimalsAsFloat_dumps") - print (MyJSONEncoderDecimalsAsFloat_dumps(d)) - print() - print ("MyJSONEncoderDecimalsAsFloat_loads") - print(MyJSONEncoderDecimalsAsFloat_loads(MyJSONEncoderDecimalsAsFloat_dumps(d))) - print() - print ("MyJSONEncoderDecimalsAsString_dumps") - print (MyJSONEncoderDecimalsAsString_dumps(d)) - print() - print ("MyJSONEncoderDecimalsAsString_loads") - print(MyJSONEncoderDecimalsAsString_loads(MyJSONEncoderDecimalsAsString_dumps(d))) - print() diff --git a/pydicts/tests/test_myjsonencoder.py b/pydicts/tests/test_myjsonencoder.py new file mode 100644 index 0000000..ff4b114 --- /dev/null +++ b/pydicts/tests/test_myjsonencoder.py @@ -0,0 +1,36 @@ +from decimal import Decimal +from datetime import timezone, date, datetime, time, timedelta +from pydicts import myjsonencoder +def test_myjsonencoder(): + d={} + d["None"]=None + d["Integer"]=12112 + d["Float"]=12121.1212 + d["Date"]=date.today() + d["Datetime"]=datetime.now() + d["Timedelta"]=timedelta(hours=4, days=2, minutes=12, seconds=12) + d["Time"]=time(12, 12, 12, 123456) + d["Datetime aware"]=d["Datetime"].replace(tzinfo=timezone.utc) + d["Bytes"]=b"Byte array" + d["Decimal"]=Decimal("12.12123414") + print ("Dictionary") + print(d) + print() + print ("MyJSONEncoder_dumps") + print (myjsonencoder.MyJSONEncoder_dumps(d)) + print() + print ("MyJSONEncoder_loads") + print(myjsonencoder.MyJSONEncoder_loads(myjsonencoder.MyJSONEncoder_dumps(d))) + print() + print ("MyJSONEncoderDecimalsAsFloat_dumps") + print (myjsonencoder.MyJSONEncoderDecimalsAsFloat_dumps(d)) + print() + print ("MyJSONEncoderDecimalsAsFloat_loads") + print(myjsonencoder.MyJSONEncoderDecimalsAsFloat_loads(myjsonencoder.MyJSONEncoderDecimalsAsFloat_dumps(d))) + print() + print ("MyJSONEncoderDecimalsAsString_dumps") + print (myjsonencoder.MyJSONEncoderDecimalsAsString_dumps(d)) + print() + print ("MyJSONEncoderDecimalsAsString_loads") + print(myjsonencoder.MyJSONEncoderDecimalsAsString_loads(myjsonencoder.MyJSONEncoderDecimalsAsString_dumps(d))) + print() From 22366ebdc40022d8914c34a5d56dc8e98d9af56c Mon Sep 17 00:00:00 2001 From: turulomio Date: Mon, 4 Dec 2023 13:48:34 +0100 Subject: [PATCH 3/3] myjsonencoder integrated --- pydicts/tests/test_myjsonencoder.py | 85 ++++++++++++++++++----------- 1 file changed, 53 insertions(+), 32 deletions(-) diff --git a/pydicts/tests/test_myjsonencoder.py b/pydicts/tests/test_myjsonencoder.py index ff4b114..189bc26 100644 --- a/pydicts/tests/test_myjsonencoder.py +++ b/pydicts/tests/test_myjsonencoder.py @@ -1,36 +1,57 @@ from decimal import Decimal from datetime import timezone, date, datetime, time, timedelta from pydicts import myjsonencoder +d={} +d["None"]=None +d["Integer"]=12112 +d["Float"]=12121.1212 +d["Date"]=date.today() +d["Datetime"]=datetime.now() +d["Timedelta"]=timedelta(hours=4, days=2, minutes=12, seconds=12) +d["Time"]=time(12, 12, 12, 123456) +d["Datetime aware"]=d["Datetime"].replace(tzinfo=timezone.utc) +d["Bytes"]=b"Byte array" +d["Decimal"]=Decimal("12.12123414") + def test_myjsonencoder(): - d={} - d["None"]=None - d["Integer"]=12112 - d["Float"]=12121.1212 - d["Date"]=date.today() - d["Datetime"]=datetime.now() - d["Timedelta"]=timedelta(hours=4, days=2, minutes=12, seconds=12) - d["Time"]=time(12, 12, 12, 123456) - d["Datetime aware"]=d["Datetime"].replace(tzinfo=timezone.utc) - d["Bytes"]=b"Byte array" - d["Decimal"]=Decimal("12.12123414") - print ("Dictionary") - print(d) - print() - print ("MyJSONEncoder_dumps") - print (myjsonencoder.MyJSONEncoder_dumps(d)) - print() - print ("MyJSONEncoder_loads") - print(myjsonencoder.MyJSONEncoder_loads(myjsonencoder.MyJSONEncoder_dumps(d))) - print() - print ("MyJSONEncoderDecimalsAsFloat_dumps") - print (myjsonencoder.MyJSONEncoderDecimalsAsFloat_dumps(d)) - print() - print ("MyJSONEncoderDecimalsAsFloat_loads") - print(myjsonencoder.MyJSONEncoderDecimalsAsFloat_loads(myjsonencoder.MyJSONEncoderDecimalsAsFloat_dumps(d))) - print() - print ("MyJSONEncoderDecimalsAsString_dumps") - print (myjsonencoder.MyJSONEncoderDecimalsAsString_dumps(d)) - print() - print ("MyJSONEncoderDecimalsAsString_loads") - print(myjsonencoder.MyJSONEncoderDecimalsAsString_loads(myjsonencoder.MyJSONEncoderDecimalsAsString_dumps(d))) - print() + json_string=myjsonencoder.MyJSONEncoder_dumps(d) + json_=myjsonencoder.MyJSONEncoder_loads(json_string) + assert json_["None"]==d["None"] + assert json_["Integer"]==d["Integer"] + assert json_["Float"]==d["Float"] + assert json_["Date"]==d["Date"] + assert json_["Datetime"]==d["Datetime"] + assert json_["Datetime aware"]==d["Datetime aware"] + assert json_["Bytes"]==d["Bytes"] + assert json_["Decimal"]==d["Decimal"] +# assert json_["Time"]==d["Time"] +# assert json_["Timedelta"]==d["Timedelta"] + +def test_myjsonencoder_decimals_as_float(): + json_string=myjsonencoder.MyJSONEncoderDecimalsAsFloat_dumps(d) + json_=myjsonencoder.MyJSONEncoderDecimalsAsFloat_loads(json_string) + assert json_["None"]==d["None"] + assert json_["Integer"]==d["Integer"] + assert json_["Float"]==d["Float"] + assert json_["Date"]==d["Date"] + assert json_["Datetime"]==d["Datetime"] + assert json_["Datetime aware"]==d["Datetime aware"] + assert json_["Bytes"]==d["Bytes"] + assert json_["Decimal"]==float(d["Decimal"]) +# assert json_["Time"]==d["Time"] +# assert json_["Timedelta"]==d["Timedelta"] + + +def test_myjsonencoder_decimals_as_string(): + json_string=myjsonencoder.MyJSONEncoderDecimalsAsString_dumps(d) + json_=myjsonencoder.MyJSONEncoderDecimalsAsString_loads(json_string) + assert json_["None"]==d["None"] + assert json_["Integer"]==d["Integer"] + assert json_["Float"]==d["Float"] + assert json_["Date"]==d["Date"] + assert json_["Datetime"]==d["Datetime"] + assert json_["Datetime aware"]==d["Datetime aware"] + assert json_["Bytes"]==d["Bytes"] + assert json_["Decimal"]==str(d["Decimal"]) +# assert json_["Time"]==d["Time"] +# assert json_["Timedelta"]==d["Timedelta"]