Skip to content

Commit 5a073f0

Browse files
committed
feat: improvements on Serializer
1 parent 9005fbd commit 5a073f0

3 files changed

Lines changed: 72 additions & 80 deletions

File tree

‎python_framework/api/src/helper/Serializer.py‎

Lines changed: 63 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -95,22 +95,25 @@ def isDictionary(thing) :
9595
def isDictionaryClass(thingClass) :
9696
return type({}) == thingClass
9797

98+
@FunctionThrough
99+
def isNone(object) :
100+
not notNone(object)
98101

99102
@FunctionThrough
100103
def notNone(object) :
101104
return object or isDictionary(object) or isList(object)
102105

103106
@FunctionThrough
104-
def isNotNativeClassIsntance(object) :
105-
return object.__class__ not in [
107+
def isNativeClassIsntance(object) :
108+
return object.__class__ in [
106109
int,
107110
str,
108-
float,
109-
dict,
110-
list,
111-
tuple,
112-
set
113-
] and not isList(object)
111+
float
112+
]
113+
114+
@FunctionThrough
115+
def isNotNativeClassIsntance(object) :
116+
return not isNativeClassIsntance(object)
114117

115118
@FunctionThrough
116119
def isNotMethodInstance(object) :
@@ -119,6 +122,15 @@ def isNotMethodInstance(object) :
119122
'builtin_function_or_method'
120123
]
121124

125+
@FunctionThrough
126+
def isCollection(object) :
127+
return object.__class__ in [
128+
list,
129+
dict,
130+
tuple,
131+
set
132+
]
133+
122134
@FunctionThrough
123135
def getTypeName(thingInstance) :
124136
if not type(type) == type(thingInstance) :
@@ -142,26 +154,26 @@ def isModel(thing) :
142154
def isModelClass(thingClass) :
143155
return isinstance(thingClass, DeclarativeMeta)
144156

145-
@FunctionThrough
146-
def getAttributeSet(object, fieldsToExpand, classTree) :
147-
attributeSet = {}
148-
presentClass = object.__class__.__name__
149-
if presentClass not in classTree :
150-
classTree[presentClass] = [object]
151-
elif classTree[presentClass].count(object) < 2 :
152-
classTree[presentClass].append(object)
153-
for attributeName in [name for name in dir(object) if not name.startswith(c.UNDERSCORE) and not name == METADATA_NAME]:
154-
attributeValue = object.__getattribute__(attributeName)
155-
if classTree[presentClass].count(object) > 1 :
156-
attributeSet[attributeName] = None
157-
continue
158-
if isModel(attributeValue) :
159-
if attributeName not in fieldsToExpand :
160-
if EXPAND_ALL_FIELDS not in fieldsToExpand :
161-
attributeSet[attributeName] = None
162-
continue
163-
attributeSet[attributeName] = attributeValue
164-
return attributeSet
157+
# @FunctionThrough
158+
# def getAttributeSet(object, fieldsToExpand, classTree) :
159+
# attributeSet = {}
160+
# presentClass = object.__class__.__name__
161+
# if presentClass not in classTree :
162+
# classTree[presentClass] = [object]
163+
# elif classTree[presentClass].count(object) < 2 :
164+
# classTree[presentClass].append(object)
165+
# for attributeName in [name for name in dir(object) if not name.startswith(c.UNDERSCORE) and not name == METADATA_NAME]:
166+
# attributeValue = object.__getattribute__(attributeName)
167+
# if classTree[presentClass].count(object) > 1 :
168+
# attributeSet[attributeName] = None
169+
# continue
170+
# if isModel(attributeValue) :
171+
# if attributeName not in fieldsToExpand :
172+
# if EXPAND_ALL_FIELDS not in fieldsToExpand :
173+
# attributeSet[attributeName] = None
174+
# continue
175+
# attributeSet[attributeName] = attributeValue
176+
# return attributeSet
165177

166178
# @FunctionThrough
167179
# def getJsonifier(revisitingItself=False, fieldsToExpand=[EXPAND_ALL_FIELDS], classTree=None):
@@ -187,44 +199,34 @@ def getAttributeSet(object, fieldsToExpand, classTree) :
187199

188200
@FunctionThrough
189201
def getObjectAsDictionary(object, fieldsToExpand=[EXPAND_ALL_FIELDS], visitedInstances=[]) :
190-
# print()
191-
# print()
192-
# print()
193-
# print(f'object.__class__.__name__ : {object.__class__.__name__}')
202+
if isNativeClassIsntance(object) or isNone(object) :
203+
return object
194204
if object not in visitedInstances :
195-
jsonInstance = {}
196205
innerVisitedInstances = visitedInstances.copy()
197-
innerVisitedInstances.append(object)
198-
# print(f'object.__class__ = {object.__class__}')
199-
# print(f'object.__class__.__name__ = {object.__class__.__name__}')
200-
# InstrumentedList
201-
atributeNameList = getAttributeNameList(object.__class__)
202-
# print(f'atributeNameList = {atributeNameList}')
203-
# print()
204-
# print(f'atributeNameList : {atributeNameList}')
205-
for attributeName in atributeNameList :
206-
attributeValue = getattr(object, attributeName)
207-
# print(' ' + attributeName + ' : ' + str(attributeValue))
208-
# print(' ' + f'type({attributeName}) :' + str(type(attributeValue)))
209-
# print(f'attributeValue = {attributeValue}')
210-
# print(f'attributeValue.__class__ = {attributeValue.__class__}')
211-
# print(f'attributeValue.__class__.__name__ = {attributeValue.__class__.__name__}')
212-
if isNotNativeClassIsntance(attributeValue) :
213-
if isNotMethodInstance(attributeValue) and notNone(attributeValue) :
206+
if isDictionary(object) :
207+
for key,value in object.items() :
208+
object[key] = getObjectAsDictionary(value, visitedInstances=innerVisitedInstances)
209+
return object
210+
elif isList(object) or type(tuple()) == type(object) or type(set()) == type(object) :
211+
objectValueList = []
212+
for innerObject in object :
213+
innerAttributeValue = getObjectAsDictionary(innerObject, visitedInstances=innerVisitedInstances)
214+
if notNone(innerAttributeValue) :
215+
objectValueList.append(innerAttributeValue)
216+
return objectValueList
217+
else :
218+
jsonInstance = {}
219+
innerVisitedInstances.append(object)
220+
InstrumentedList
221+
atributeNameList = getAttributeNameList(object.__class__)
222+
for attributeName in atributeNameList :
223+
attributeValue = getattr(object, attributeName)
224+
if isNotMethodInstance(attributeValue):
214225
jsonInstance[attributeName] = getObjectAsDictionary(attributeValue, visitedInstances=innerVisitedInstances)
215226
else :
216227
jsonInstance[attributeName] = None
217-
elif isList(attributeValue) :
218-
attributeValueList = []
219-
for innerObject in attributeValue :
220-
innerAttributeValue = getObjectAsDictionary(innerObject, visitedInstances=innerVisitedInstances)
221-
if notNone(innerAttributeValue) :
222-
attributeValueList.append(innerAttributeValue)
223-
jsonInstance[attributeName] = attributeValueList
224-
else :
225-
jsonInstance[attributeName] = attributeValue
226-
# print(jsonInstance)
227-
return jsonInstance
228+
if jsonInstance :
229+
return jsonInstance
228230

229231
@Function
230232
def jsonifyIt(object, fieldsToExpand=[EXPAND_ALL_FIELDS]) :

‎python_framework/api/test/SerializerTest.py‎

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ def __init__(self, myClass=None):
1717
self.myClass = myClass
1818
self.myNeutralClassAttribute = 'someOtherString'
1919

20+
MY_DICTIONARY = {
21+
'myString' : 'myValue',
22+
'myInteger' : 0,
23+
'myFloat' : 0.099
24+
}
25+
2026
MODEL = sap.getNewModel()
2127

2228
class MyEntityClass(MODEL) :
@@ -58,6 +64,8 @@ def Serializer_isJsonifyable() :
5864
log.debug(Serializer_isJsonifyable, 'Success')
5965

6066
def Serializer_jsonifyIt() :
67+
assert '{"myString": "myValue", "myInteger": 0, "myFloat": 0.099}' == jsonifyIt(MY_DICTIONARY)
68+
6169
myGenerator = generatorFunction()
6270
assert myGenerator == jsonifyIt(myGenerator)
6371
assert '{"myAttribute": null, "myNeutralAttribute": "someString"}' == jsonifyIt(MyClass())
@@ -104,28 +112,10 @@ def Serializer_jsonifyIt() :
104112
assert '{"brother": {"child": null, "father": {"brotherList": [], "childList": [], "id": 4}, "fatherId": 4, "id": 3}, "brotherId": 3, "father": {"brotherList": [{"child": {"brother": null, "brotherId": 5, "father": null, "fatherId": 1, "id": 6}, "father": null, "fatherId": 1, "id": 5}], "childList": [{"brother": {"child": null, "father": null, "fatherId": 1, "id": 5}, "brotherId": 5, "father": null, "fatherId": 1, "id": 6}], "id": 1}, "fatherId": 1, "id": 2}' == jsonifyIt(child)
105113
assert '{"brotherList": [{"child": {"brother": null, "brotherId": 5, "father": null, "fatherId": 1, "id": 6}, "father": null, "fatherId": 1, "id": 5}], "childList": [{"brother": {"child": null, "father": {"brotherList": [], "childList": [], "id": 4}, "fatherId": 4, "id": 3}, "brotherId": 3, "father": null, "fatherId": 1, "id": 2}, {"brother": {"child": null, "father": null, "fatherId": 1, "id": 5}, "brotherId": 5, "father": null, "fatherId": 1, "id": 6}], "id": 1}' == jsonifyIt(father)
106114

107-
108-
from python_helper import StringHelper
109-
import json
110-
111-
class MyClass :
112-
def __init__(self, myAttribute=None):
113-
self.myAttribute = myAttribute
114-
self.myNeutralAttribute = 'someString'
115-
116-
class MyAttributeClass :
117-
def __init__(self, myClass=None):
118-
self.myClass = myClass
119-
self.myNeutralClassAttribute = 'someOtherString'
120-
121-
122-
# print(StringHelper.stringfyThisDictionary(json.loads(jsonifyIt(child))))
123-
124115
myClass = MyClass()
125116
myAttributeClass = MyAttributeClass(myClass=myClass)
126117
myClass.myAttribute = myAttributeClass
127118
assert '{"myAttribute": {"myClass": null, "myNeutralClassAttribute": "someOtherString"}, "myNeutralAttribute": "someString"}' == jsonifyIt(myClass)
128-
# # assert 'someOtherString' == jsonifyIt(myClass.myAttributeClass)
129119
assert '{"myClass": {"myAttribute": null, "myNeutralAttribute": "someString"}, "myNeutralClassAttribute": "someOtherString"}' == jsonifyIt(myClass.myAttribute)
130120

131121
log.debug(Serializer_jsonifyIt, 'Success')

‎setup.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from distutils.core import setup
22
import os, site
33

4-
VERSION = '0.1.8-4'
4+
VERSION = '0.1.8-5'
55
NAME = 'python_framework'
66
URL = f'https://github.com/SamuelJansen/{NAME}/'
77

0 commit comments

Comments
 (0)