Created
June 12, 2009 18:59
-
-
Save galvez/128840 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
jshash, an improved python dictionary. | |
basic functionality comes from web.py's storage() | |
added support for the with-statement | |
if you're running python <2.6, you need to put | |
from __future__ import with_statement | |
at the first line of the script. | |
''' | |
class jshash(dict): | |
def __getattr__(self, k): | |
if self.has_key(k): return self[k] | |
raise AttributeError, repr(k) | |
def __setattr__(self, k, v): self[k] = v | |
def __delattr__(self, k): del self[k] | |
def __enter__(self): return self | |
def __exit__(self, type, value, tv): pass | |
# indifferent access | |
o = jshash() | |
o['foo'] = 1 | |
o.bar = 2 | |
# avoids funky {} multi-line blocks | |
with jshash() as o: | |
o.foo = 1 | |
o.bar = 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment