Skip to content

Instantly share code, notes, and snippets.

@galvez
Created June 12, 2009 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save galvez/128840 to your computer and use it in GitHub Desktop.
Save galvez/128840 to your computer and use it in GitHub Desktop.
'''
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