-1

object looks like as below


{
"id":1,
"image":"path/to/image",
"employee_data":<sql_alcahmy_object>,
}

again sql_alcahmy_object is as below

{
"employee_previous":<sql_alchemy_object2>,
"employee_salary":"1$",

}

again sql_alcahmy_object2 is as below


{"company":"xyz","years":10}

below method will be used to extract all data from sql alchemy object

sql_alchemy_object.__dict__

below is the recursive method planned but it didn't work out

def extract_recursive(deepvalue,alldata={}):
    for eachkey,eachvalue in deepvalue.__dict__.iteritems():
        if hasattr(eachvalue,"__dict__"):
            alldata.update({eachkey:extract_recursive(eachvalue)})
        else:   
            alldata.update({eachkey:eachvalue})
    print(alldata)

expected output


{
"id":1,
"image":"path/to/image",
"employee_data":{
             "employee_previous":{"company":"xyz","years":10},
             "employee_salary":"1$",

              }
}

Available methods in deepvalue and sql_alcahmy_object as below

 ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__mapper__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__table__', '__tablename__', '__weakref__', '_decl_class_registry', '_sa_class_manager', 'age'] 

Harish
  • 157
  • 2
  • 11
  • 1
    There isn't a question anywhere here, just a series of statements. What exactly are you asking? What are the issues you face? – roganjosh Mar 22 '19 at 13:49

1 Answers1

0

There is a misunderstanding of what is __dict__ here

It actually is

A dictionary or other mapping object used to store an object’s (writable) attributes.

but it does not allow to access nested dicts of a dict.

So, if you want to iterate over items in a dict, you just have to call thedict.iteritems() not thedict.__dict__.iteritems().

Then, if you want to check if variable contains a dict instance, use isinstance(a_dict, dict), not hasattr(a_dict, '__dict__').

Additionaly, using a mutable object as a default value for a function argument can produce non-intuitive result and is strongly discouraged (see "Least Astonishment" and the Mutable Default Argument) Instead you should probably pass None as default value and add a alldata = alldata or {} at the begining of your function.

Finally, although I do not understand the point of your function, this version should work better:

def extract_recursive(deepvalue, alldata=None):
    alldata = alldata or {}
    for eachkey, eachvalue in deepvalue.iteritems():
        if isinstance(eachvalue, dict):
            alldata.update({eachkey:extract_recursive(eachvalue)})
        else:
            alldata.update({eachkey:eachvalue})
    print(alldata)
Tryph
  • 5,946
  • 28
  • 49
  • but issue here is I don't have deepvalue.iteritems(), I can only fetch it through deep value__dict__.iteritems() – Harish Mar 22 '19 at 15:25
  • @Harish: what is the type those "sql_alchemy_object" – Tryph Mar 22 '19 at 15:28
  • its class object it doesn't have iteritems method and model was written by others so I am not authorised to change – Harish Mar 22 '19 at 15:42
  • @Harish: It will be difficult to help without detail. What does return a `type()`? If it is a standard type, we could potentially help you. It is a custom type and you cannot share the code, you still can inspect it with `dir()` looking for a suitable attribute... – Tryph Mar 22 '19 at 15:50