I have below file
file.py
data = {
"SerialNumber": 1234,
"ActualCount": "TY"
}
In app.py
I can easily import this file and can call the dictionary like below:
app.py
import file as file
print(file.data['SerialNumber'])
Is there any type of parser available which can parse this dict objects and then we can get its items like below:
print(file.data.SerialNumber)
print(file.data.ActualCount)
CodePudding user response:
If you really want this, you can implement your own dict class supporting __getattribute__
method, rough example:
data = {
"SerialNumber": 1234,
"ActualCount": "TY"
}
class MyDict(dict):
def __getattribute__(self, item):
return self[item]
my_dict = MyDict(data)
print(my_dict.SerialNumber) # 1234
CodePudding user response:
if you need this behavior you can also try namedtuple
:
from collections import namedtuple
data = {"SerialNumber": 1234, "ActualCount": "TY"}
MyDict = namedtuple('MyDict', data.keys())
my_dict = MyDict(**data)
print(my_dict.SerialNumber) # 1234
print(my_dict.ActualCount) # TY