You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I found a way to improve our Storage class. With this we could allow only particular keys. this could help not to mess around with this class (look at __setattr__)
classStorage(dict):
def__getattr__(self, name):
try:
returnself[name]
exceptKeyError:
raiseAttributeError(name)
__delattr__=dict.__delitem__KEYLIST= ['KEY1', 'KEY2', 'KEY3']
def__repr__(self):
ifself.keys():
m=max(map(len, list(self.keys()))) +1return'\n'.join([k.rjust(m) +': '+repr(v)
fork, vinsorted(self.items())])
else:
returnself.__class__.__name__+"()"def__dir__(self):
returnlist(self.keys())
def__setattr__(self, key, value):
ifkeynotinStorage.KEYLIST:
raiseKeyError("{} is not a legal key of this Storage".format(repr(key)))
dict.__setitem__(self, key, value)
What do you think?
The text was updated successfully, but these errors were encountered:
I found a way to improve our Storage class. With this we could allow only particular keys. this could help not to mess around with this class (look at
__setattr__
)What do you think?
The text was updated successfully, but these errors were encountered: