@@ -95,22 +95,25 @@ def isDictionary(thing) :
9595def isDictionaryClass (thingClass ) :
9696 return type ({}) == thingClass
9797
98+ @FunctionThrough
99+ def isNone (object ) :
100+ not notNone (object )
98101
99102@FunctionThrough
100103def 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
116119def 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
123135def getTypeName (thingInstance ) :
124136 if not type (type ) == type (thingInstance ) :
@@ -142,26 +154,26 @@ def isModel(thing) :
142154def 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
189201def 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
230232def jsonifyIt (object , fieldsToExpand = [EXPAND_ALL_FIELDS ]) :
0 commit comments