Last active
February 16, 2017 10:28
-
-
Save ecarreras/fad12fa6ebf17f1216baff28a2814dcb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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