From 172217471d32dfe3352fc857ec6678d549f9f75f Mon Sep 17 00:00:00 2001 From: Felix Stupp Date: Fri, 3 Apr 2020 09:12:46 +0000 Subject: [PATCH] Extracted JsonClass into own file --- tinytinypy/JsonClass.py | 16 ++++++++++++++++ tinytinypy/main.py | 22 +--------------------- 2 files changed, 17 insertions(+), 21 deletions(-) create mode 100644 tinytinypy/JsonClass.py diff --git a/tinytinypy/JsonClass.py b/tinytinypy/JsonClass.py new file mode 100644 index 0000000..4060fa4 --- /dev/null +++ b/tinytinypy/JsonClass.py @@ -0,0 +1,16 @@ +class JsonClass: + @classmethod + def getClassKey(cls, jsonKey): + for key, val in cls.TRANS.items(): + if val == jsonKey or (key == jsonKey and val == True): + return key + return None + @classmethod + def getJsonKey(cls, classKey): + val = cls.TRANS.get(classKey, None) + return classKey if val == True else val + @classmethod + def fromJson(cls, dataDict): + return cls(**{cls.getClassKey(key): val for key, val in dataDict.items() if cls.getClassKey(key) is not None and val is not None}) + def toJson(self): + return {self.__class__.getJsonKey(key): val for key, val in self.__dict__.items() if self.__class__.getJsonKey(key) is not None and val is not None} diff --git a/tinytinypy/main.py b/tinytinypy/main.py index 899126d..6660ce0 100755 --- a/tinytinypy/main.py +++ b/tinytinypy/main.py @@ -4,27 +4,7 @@ import atexit import http.client import json -class JsonClass: - TRANS_EXP = { - "same_key": True, - "class_key": "json_key", - "ignore_class_key": None, - } - @classmethod - def getClassKey(cls, jsonKey): - for key, val in cls.TRANS.items(): - if val == jsonKey or (key == jsonKey and val == True): - return key - return None - @classmethod - def getJsonKey(cls, classKey): - val = cls.TRANS.get(classKey, None) - return classKey if val == True else val - @classmethod - def fromJson(cls, dataDict): - return cls(**{cls.getClassKey(key): val for key, val in dataDict.items() if cls.getClassKey(key) is not None and val is not None}) - def toJson(self): - return {self.__class__.getJsonKey(key): val for key, val in self.__dict__.items() if self.__class__.getJsonKey(key) is not None and val is not None} +from .JsonClass import JsonClass # Class Invalid class TtRssCounters: