fold FormattableRecord, NullableRecord into Record
authorGreg Price <price@mit.edu>
Fri, 27 Feb 2009 04:09:33 +0000 (23:09 -0500)
committerGreg Price <price@mit.edu>
Fri, 27 Feb 2009 04:09:33 +0000 (23:09 -0500)
also shorten types in lists to __name__

(Done with iannucci.)

svn path=/trunk/packages/invirt-database/; revision=2192

debian/changelog
python/database/owner.py
python/database/record.py

index c96b33a..ead1ec7 100644 (file)
@@ -6,8 +6,10 @@ invirt-database (0.1.8) unstable; urgency=low
   [Greg Price]
   * use self.c rather than self.__dict__ for SQLAlchemy fields
   * make Record._ignore, Owner.get* classmethods
+  * fold FormattableRecord, NullableRecord into Record
+  * shorten types in lists to __name__
 
- -- Greg Price <price@mit.edu>  Thu, 26 Feb 2009 22:38:02 -0500
+ -- Greg Price <price@mit.edu>  Thu, 26 Feb 2009 22:51:43 -0500
 
 invirt-database (0.1.7) unstable; urgency=low
 
index 43e7ca4..4988bf3 100755 (executable)
@@ -1,6 +1,6 @@
-from record import NullableRecord
+from record import Record
 
-class Owner(NullableRecord):
+class Owner(Record):
     _f = {
         'ram_quota_total': (512, 'MiB'),
         'ram_quota_single': (512, 'MiB'),
index 4962322..dcc99b9 100755 (executable)
@@ -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):
@@ -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)