Home > Software design >  How to convert dict to class attributes in Python
How to convert dict to class attributes in Python

Time:01-04

Instead of using a dict to store and pass data we are going completely OOPS approach of storing the data as class attributes and call the get methods defined according to need.

In Java i was able to achieve this but having some trouble in Python. Any Solution would be helpful.

import json

class InputModel:
    def __init__(self, input_payload):
        self.id1 = input_payload["id1"]
        self.route = RouteModel(input_payload["route"])
        self.id2 = input_payload["id2"]
        self.id3 = input_payload["id3"]
        self.id4 = input_payload["id4"]
        self.id5 = input_payload["id5"]

        def get_id1(self):
            return self.id1

        #similar for other ids


class RouteModel:
    def __init__(self, input_payload_route):
        self.id6 = input_payload_route["id6"]
        self.id7 = input_payload_route["id7"]
       
        def get_id6(self):
            return self.id6

        #similar for other ids
        
        

json_str = '{"id1":"string","route":{"id6":"string","id7":"string"},"id2": "string","id3": "string","id4": "string","id5": "string"}'
json_dict = json.loads(json_str)
im = InputModel(json_dict)
print(im.get_id1())
print(im.get_id6())

not able to access the nested class attributes

CodePudding user response:

Seems like you went for 1 extra indent in your class methods, thus you couldn't reach them.

Also, to reach id6 of RouteModel, you had to refer to 'route' first:

import json

class InputModel:
    def __init__(self, input_payload):
        self.id1 = input_payload["id1"]
        self.route = RouteModel(input_payload["route"])
        self.id2 = input_payload["id2"]
        self.id3 = input_payload["id3"]
        self.id4 = input_payload["id4"]
        self.id5 = input_payload["id5"]

    def get_id1(self):
        return self.id1

        #similar for other ids


class RouteModel:
    def __init__(self, input_payload_route):
        self.id6 = input_payload_route["id6"]
        self.id7 = input_payload_route["id7"]
    
    def get_id6(self):
        return self.id6

        #similar for other ids
        
        

json_str = '{"id1":"string","route":{"id6":"string","id7":"string"},"id2": "string","id3": "string","id4": "string","id5": "string"}'
json_dict = json.loads(json_str)
im = InputModel(json_dict)
print(im.get_id1())
print(im.route.get_id6())

Output:

string
string

CodePudding user response:

The problem is that you are only defining get_id* in your local scope, you need to assign it to the instance if you insist on defining it inside the __init__ method.

I minimized your code example to isolate your issue.

class RouteModel:
    def __init__(self):
        self.id6 = "foo"

        def get_id6(self_=self):
            return self_.id6

        self.get_id6 = get_id6

rm = RouteModel()
print(rm.get_id6())

>>> "foo"
  •  Tags:  
  • Related