Ignore:
Timestamp:
Feb 26, 2009, 11:09:33 PM (16 years ago)
Author:
price
Message:

fold FormattableRecord?, NullableRecord? into Record

also shorten types in lists to name

(Done with iannucci.)

Location:
trunk/packages/invirt-database/python/database
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/invirt-database/python/database/owner.py

    r2191 r2192  
    1 from record import NullableRecord
     1from record import Record
    22
    3 class Owner(NullableRecord):
     3class Owner(Record):
    44    _f = {
    55        'ram_quota_total': (512, 'MiB'),
  • trunk/packages/invirt-database/python/database/record.py

    r2191 r2192  
    11class Record(object):
    22    _identity_field = None
     3    _default = {}
     4    _format = {}
    35
    46    def get(self, field):
    5         try:
    6             return self.__getattribute__(field)
    7         except:
    8             return None
     7        v = getattr(self, field, None)
     8        if v is None:
     9            return self._default.get(field)
     10        return v
    911
    1012    def _formatField(self, field):
    1113        v = self.get(field)
     14        func = self._format.get(field)
     15        if func:
     16            return func(v)
    1217        if callable(v):
    1318            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__'):
    2020            return repr(v)
     21        if len(v) == 0:
     22            return '[]'
     23        return '[%d x %s]'%(len(v), type(v[0]).__name__)
    2124
    2225    @classmethod
     
    4346
    4447        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 v
    61         else:
    62             return self._default.get(field)
Note: See TracChangeset for help on using the changeset viewer.