X-Git-Url: http://xvm.mit.edu/gitweb/invirt/packages/invirt-database.git/blobdiff_plain/4688abd0023864267ae3ccd69b84d14c58dd8237..8ff8bfe4ba94dd1ed0a89a06d528c1b48072f2dd:/python/database/record.py diff --git a/python/database/record.py b/python/database/record.py index 4962322..bcf3d45 100755 --- a/python/database/record.py +++ b/python/database/record.py @@ -1,23 +1,26 @@ class Record(object): _identity_field = None + _default = {} + _format = {} def get(self, field): - try: - return self.__getattribute__(field) - except: - return None + v = getattr(self, field, None) + if v is None: + return self._default.get(field) + return v def _formatField(self, field): v = self.get(field) + func = self._format.get(field) + if func: + return func(v) if callable(v): v = v() - if hasattr(v, '__iter__'): - if len(v) == 0: - return '[]' - else: - return '[%d x %s]'%(len(v), type(v[0])) - else: + if not hasattr(v, '__iter__'): return repr(v) + if len(v) == 0: + return '[]' + return '[%d x %s]'%(len(v), type(v[0]).__name__) @classmethod def _ignore(cls): @@ -33,7 +36,7 @@ class Record(object): if self._identity_field: identity = self.__dict__.get(self._identity_field) - identity = ' ' + (identity and repr(identity) or 'hash=%d'%hash(self)) + identity = ' ' + (identity and repr(identity) or 'hash=%X'%hash(self)) else: identity = '' @@ -42,21 +45,3 @@ class Record(object): payload = ": "+payload return "<%s%s%s>" % (classname, identity, payload) - -class FormattableRecord(Record): - _format = {} - def _formatField(self, field): - func = self._format.get(field) - if func: - return func(self.get(field)) - else: - return super(FormattableRecord, self)._formatField(field) - -class NullableRecord(FormattableRecord): - _default = {} - def get(self, field): - v = self.__dict__.get(field) - if v != None: - return v - else: - return self._default.get(field)