Changeset 2192 for trunk/packages
- Timestamp:
- Feb 26, 2009, 11:09:33 PM (16 years ago)
- Location:
- trunk/packages/invirt-database
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/packages/invirt-database/debian/changelog
r2191 r2192 7 7 * use self.c rather than self.__dict__ for SQLAlchemy fields 8 8 * make Record._ignore, Owner.get* classmethods 9 10 -- Greg Price <price@mit.edu> Thu, 26 Feb 2009 22:38:02 -0500 9 * fold FormattableRecord, NullableRecord into Record 10 * shorten types in lists to __name__ 11 12 -- Greg Price <price@mit.edu> Thu, 26 Feb 2009 22:51:43 -0500 11 13 12 14 invirt-database (0.1.7) unstable; urgency=low -
trunk/packages/invirt-database/python/database/owner.py
r2191 r2192 1 from record import NullableRecord1 from record import Record 2 2 3 class Owner( NullableRecord):3 class Owner(Record): 4 4 _f = { 5 5 'ram_quota_total': (512, 'MiB'), -
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.