Changeset 2192 for trunk/packages/invirt-database/python/database/record.py
- Timestamp:
- Feb 26, 2009, 11:09:33 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/packages/invirt-database/python/database/record.py
r2191 r2192 1 1 class Record(object): 2 2 _identity_field = None 3 _default = {} 4 _format = {} 3 5 4 6 def get(self, field): 5 try:6 return self.__getattribute__(field)7 except:8 return None7 v = getattr(self, field, None) 8 if v is None: 9 return self._default.get(field) 10 return v 9 11 10 12 def _formatField(self, field): 11 13 v = self.get(field) 14 func = self._format.get(field) 15 if func: 16 return func(v) 12 17 if callable(v): 13 18 v = v() 14 if hasattr(v, '__iter__'): 15 if len(v) == 0: 16 return '[]' 17 else: 18 return '[%d x %s]'%(len(v), type(v[0])) 19 else: 19 if not hasattr(v, '__iter__'): 20 20 return repr(v) 21 if len(v) == 0: 22 return '[]' 23 return '[%d x %s]'%(len(v), type(v[0]).__name__) 21 24 22 25 @classmethod … … 43 46 44 47 return "<%s%s%s>" % (classname, identity, payload) 45 46 class FormattableRecord(Record):47 _format = {}48 def _formatField(self, field):49 func = self._format.get(field)50 if func:51 return func(self.get(field))52 else:53 return super(FormattableRecord, self)._formatField(field)54 55 class NullableRecord(FormattableRecord):56 _default = {}57 def get(self, field):58 v = self.__dict__.get(field)59 if v != None:60 return v61 else:62 return self._default.get(field)
Note: See TracChangeset
for help on using the changeset viewer.