utils.Storage is one of the reusable components that makes one wonder
how come it is not integral part of Python. It gives my configuration
files an elegant name-spacing, and I use it instead of dict anywhere
it make sense.
However, it would have been a perfect component once it will get a
recursion capability.
What do I mean?
An example:
For a given x = Storage({'server': {'username': 'root', 'password': 'pa
$$word', 'ip': '1.1.1.1'}})
I can only do x.server['username'] and not x.server.username.
To get that behaviour I found that there are two ways either by
hacking or by hard-coding it.
hard-coding came out simple:
Instead of:
x = Storage({'server': {'username': 'root', 'password': 'pa$$word',
'ip': '1.1.1.1'}})
I can write:
x = Storage({'server': Storage( {'username': 'root', 'password': 'pa$
$word', 'ip': '1.1.1.1'})})
hacking was even simpler:
I added a single line at __getattr__ just before return self[key]:
if type(self[key]).__name__ == 'dict': self[key] = Storage(self[key])
This is how my __getattr__ looks like:
def __getattr__(self, key):
try:
if type(self[key]).__name__ == 'dict': self[key] =
Storage(self[key])
return self[key]
except KeyError, k:
raise AttributeError, k
Then I thought, let's do it on __setattr__ as well and so to save time
at reading:
And this is how my __setattr__ looks like:
def __setattr__(self, key, value):
if type(value).__name__ == 'dict': value = Storage(value)
self[key] = value
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"web.py" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/webpy?hl=en
-~----------~----~----~----~------~----~------~--~---