in Education by
object looks like as below { "id":1, "image":"path/to/image", "employee_data":, } again sql_alcahmy_object is as below { "employee_previous":, "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'] JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
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)

Related questions

0 votes
    object looks like as below { "id":1, "image":"path/to/image", "employee_data":, } again ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Bitmaps can be combined with regular B+-tree indices for relations where a few attribute values are ... Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    I am trying to scrape the data off the webpage below, using Selenium in Python 3: https://www. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I am trying to scrape the data off the webpage below, using Selenium in Python 3: https://www. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I have this JSON tree view that represents a menu : var menus = [ { label : "1", items: [ ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    I was unable to find a solution that is identical with the issue I am trying to solve. If is a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I was unable to find a solution that is identical with the issue I am trying to solve. If is a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    Set the cron job, but the order email will auto cc to other customers and I don't know why. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    When you setup an Auto Scaling groups in AWS EC2 Min and Max bounds seem to make sense: The minimum number of ... is larger than Min? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    Multiple objects can be de parsed at once using the ______ function. (a) dput() (b) write() (c) ... and Operations of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    Multiple objects can be de parsed at once and read back using function _____ (a) source() (b) read() ( ... Operations of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    Which of these is a process of extracting/removing the state of an object from a stream? (a) ... programming questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    I need to use environment variable "PATH" in yaml file which needs to be parsed with a script. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    I want to send the following JSON text {"Email":"[email protected]","Password":"123456"} to a web ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    Below is a crude for-loop to illustrate what I need to do. Basically, if there are any 'Variable' ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
...