Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

myjsonencoder integrated #24

Merged
merged 4 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pydicts.epj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"header": {
"comment": "eric project file for project pydicts",
"copyright": "Copyright (C) 2023 , ",
"saved": "2023-12-04, 13:12:01"
"saved": "2023-12-04, 13:20:21"
},
"project": {
"AUTHOR": "",
Expand Down Expand Up @@ -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": "",
Expand Down
35 changes: 0 additions & 35 deletions pydicts/myjsonencoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
57 changes: 57 additions & 0 deletions pydicts/tests/test_myjsonencoder.py
Original file line number Diff line number Diff line change
@@ -0,0 +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():
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"]
Loading