Skip to content

Instantly share code, notes, and snippets.

@ecarreras
Last active February 16, 2017 10:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ecarreras/fad12fa6ebf17f1216baff28a2814dcb to your computer and use it in GitHub Desktop.
Save ecarreras/fad12fa6ebf17f1216baff28a2814dcb to your computer and use it in GitHub Desktop.
from collections import namedtuple
import json
class Model(object):
fields = [
'id',
'cini',
'origen',
'desti'
]
@property
def ref(self):
return 1
def __init__(self, *values, **kwvalues):
stored = namedtuple('{0}_store'.fromat(self.__classname__), self.fields)
self.store = stored(*values, **kwvalues)
def dump(self, format='json')
if format == 'json':
return json.dumps(self.store._asdict())
else:
return list(self.store)
def diff(self, obj, fields=None):
assert isinstance(obj, self.__class__)
diffs = {}
if fields is None:
fields = self.store._fields
for field in fields:
self_value = getattr(self, field)
other_value = getattr(obj, field)
if self_value != other_value:
diffs[field] = (self_value, other_value)
def __cmp__(self, other):
if self.diff(other, ['id', 'cini']):
return True
else:
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment