Skip to content

Instantly share code, notes, and snippets.

@kevinjqiu
Created February 19, 2014 19:25
Show Gist options
  • Save kevinjqiu/9099621 to your computer and use it in GitHub Desktop.
Save kevinjqiu/9099621 to your computer and use it in GitHub Desktop.
class _AppendOnlyAttribute(object):
def __init__(self, name):
self.name = name
def __get__(self, instance, owner):
if instance:
return getattr(instance._history[0], self.name)
else:
return getattr(owner.__history_class__, self.name)
def __set__(self, instance, value):
prev_history_record = instance._history[0]
instance._history.insert(
0, prev_history_record.fork(**{
self.name: value,
'id': instance.next_history_id(),
}))
def append_only_attr(name, **kwargs):
retval = _AppendOnlyAttribute(name)
if 'default_value' in kwargs:
retval.default_value = kwargs.pop('default_value')
return retval
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment