I am able to set class attributes by using @property decorator using the following code, however I am not able to set attributes(in a hierarchy) in case it is a nested dict. For example if we need to print the value of 'port' which is within nested 'credentials' dict then how can I use print(obj.credentials.port) rather than using print(obj.port)
class TestDec:
def __init__(self,value) -> None:
self.value = value
@property
def description(self):
return self.value["description"]
@property
def credentials(self):
return self.value["credentials"]
@property
def username(self):
return self.value["credentials"]["username"]
@property
def password(self):
return self.value["credentials"]["password"]
@property
def port(self):
try:
return self.value["credentials"]["port"]
except KeyError:
pass
obj = TestDec({"description":"test decorator","credentials":{"username":"abc","password":"xxxx","port":"5432"}})
print(obj.description)
print(obj.credentials)
print(obj.username)
print(obj.password)
print(obj.port)
CodePudding user response:
Either you have an error with obj.username
and similar and it should be obj.credentials.username
(like in my example below), or you want to recursively search for key, which I don't think is a good idea. But for this nested dot accessing elements of dictionary, this could be sufficient.
from collections import UserDict
class MyDict(UserDict):
def __getattr__(self, arg):
try:
result = self.data[arg]
except KeyError as error:
raise AttributeError from error
if isinstance(result, dict):
return MyDict(result)
return result
obj = MyDict(
{
"description": "test decorator",
"credentials": {"username": "abc", "password": "xxxx", "port": "5432"},
}
)
print(obj.description)
print(obj.credentials)
print(obj.credentials.username)
print(obj.credentials.password)
print(obj.credentials.port)