Skip to content

Instantly share code, notes, and snippets.

@mlsteele
Created April 30, 2016 02: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 mlsteele/c282b1e4852b71e3ae2316d10a20597f to your computer and use it in GitHub Desktop.
Save mlsteele/c282b1e4852b71e3ae2316d10a20597f to your computer and use it in GitHub Desktop.
Observer magic
class SimpleStore(object):
"""Dead simple storage. Basically a dictionary."""
def __init__(self):
self.data = {}
def get(self, key):
return self.data[key]
def set(self, key, value):
self.data[key] = value
class Observer(object):
"""
Slightly magical object on which you can set any attributes.
Prints to stdout all operations.
Uses the get and set methods of the store argument.
Just... don't mess with __store from the outside.
"""
def __init__(self, store):
self.__dict__["__store"] = store
def __getattr__(self, name):
store = getattr(self, "__store")
value = store.get(name)
print "get({}) -> {}".format(name, value)
return value
def __setattr__(self, name, value):
store = getattr(self, "__store")
print "set({}) <- {}".format(name, value)
store.set(name, value)
store = SimpleStore()
o = Observer(store)
# Annoyingly, store is accessible from the outside.
print o.__store
o.x = "absolutely"
print o.x + " not"
print o.x * 5
o.x *= 3
print o.x
<__main__.SimpleStore object at 0x7f1c871db450>
set(x) <- absolutely
get(x) -> absolutely
absolutely not
get(x) -> absolutely
absolutelyabsolutelyabsolutelyabsolutelyabsolutely
get(x) -> absolutely
set(x) <- absolutelyabsolutelyabsolutely
get(x) -> absolutelyabsolutelyabsolutely
absolutelyabsolutelyabsolutely
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment