From 682136427352a1152b841badc47025a771a9a4ea Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 16 Jul 2019 14:27:12 +0200 Subject: [PATCH] code beautifying --- src/RegisterDatapoint.py | 43 ++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/src/RegisterDatapoint.py b/src/RegisterDatapoint.py index f837478..13dced1 100644 --- a/src/RegisterDatapoint.py +++ b/src/RegisterDatapoint.py @@ -6,28 +6,6 @@ import logging 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 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): js = json.dumps(registerList, cls=JsonifyEncoder, sort_keys=True, indent=4) with open(registerListFile, 'w') as f: