code beautifying
This commit is contained in:
@ -6,28 +6,6 @@ import logging
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
|
|
||||||
class JsonifyEncoder(json.JSONEncoder):
|
|
||||||
def default(self, o):
|
|
||||||
res = None
|
|
||||||
try:
|
|
||||||
res = o.jsonify()
|
|
||||||
except (TypeError, AttributeError):
|
|
||||||
if type(o) == datetime.timedelta:
|
|
||||||
res = o.total_seconds()
|
|
||||||
else:
|
|
||||||
res = super().default(o)
|
|
||||||
return res
|
|
||||||
|
|
||||||
def datapointObjectHook(j):
|
|
||||||
if type(j) == dict and 'type' in j and 'args' in j:
|
|
||||||
klass = eval(j['type'])
|
|
||||||
o = klass(**j['args'])
|
|
||||||
return o
|
|
||||||
else:
|
|
||||||
return j
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class DatapointException(Exception): pass
|
class DatapointException(Exception): pass
|
||||||
|
|
||||||
class AbstractModbusDatapoint(object):
|
class AbstractModbusDatapoint(object):
|
||||||
@ -171,6 +149,27 @@ class DiscreteInputDatapoint(ReadOnlyDatapoint):
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class JsonifyEncoder(json.JSONEncoder):
|
||||||
|
def default(self, o):
|
||||||
|
res = None
|
||||||
|
try:
|
||||||
|
res = o.jsonify()
|
||||||
|
except (TypeError, AttributeError):
|
||||||
|
if type(o) == datetime.timedelta:
|
||||||
|
res = o.total_seconds()
|
||||||
|
else:
|
||||||
|
res = super().default(o)
|
||||||
|
return res
|
||||||
|
|
||||||
|
def datapointObjectHook(j):
|
||||||
|
if type(j) == dict and 'type' in j and 'args' in j:
|
||||||
|
klass = eval(j['type'])
|
||||||
|
o = klass(**j['args'])
|
||||||
|
return o
|
||||||
|
else:
|
||||||
|
return j
|
||||||
|
|
||||||
def saveRegisterList(registerList, registerListFile):
|
def saveRegisterList(registerList, registerListFile):
|
||||||
js = json.dumps(registerList, cls=JsonifyEncoder, sort_keys=True, indent=4)
|
js = json.dumps(registerList, cls=JsonifyEncoder, sort_keys=True, indent=4)
|
||||||
with open(registerListFile, 'w') as f:
|
with open(registerListFile, 'w') as f:
|
||||||
|
Reference in New Issue
Block a user