Last change
on this file since 2512 was
2193,
checked in by iannucci, 16 years ago
|
cosmetic change
|
-
Property svn:executable set to
*
|
File size:
1.3 KB
|
Rev | Line | |
---|
[2187] | 1 | class Record(object): |
---|
| 2 | _identity_field = None |
---|
[2192] | 3 | _default = {} |
---|
| 4 | _format = {} |
---|
[2191] | 5 | |
---|
[2187] | 6 | def get(self, field): |
---|
[2192] | 7 | v = getattr(self, field, None) |
---|
| 8 | if v is None: |
---|
| 9 | return self._default.get(field) |
---|
| 10 | return v |
---|
[2191] | 11 | |
---|
[2187] | 12 | def _formatField(self, field): |
---|
| 13 | v = self.get(field) |
---|
[2192] | 14 | func = self._format.get(field) |
---|
| 15 | if func: |
---|
| 16 | return func(v) |
---|
[2187] | 17 | if callable(v): |
---|
| 18 | v = v() |
---|
[2192] | 19 | if not hasattr(v, '__iter__'): |
---|
[2187] | 20 | return repr(v) |
---|
[2192] | 21 | if len(v) == 0: |
---|
| 22 | return '[]' |
---|
| 23 | return '[%d x %s]'%(len(v), type(v[0]).__name__) |
---|
[2191] | 24 | |
---|
| 25 | @classmethod |
---|
| 26 | def _ignore(cls): |
---|
| 27 | return [cls._identity_field] |
---|
| 28 | |
---|
[2187] | 29 | def _fields(self): |
---|
| 30 | ignore = self._ignore() |
---|
[2191] | 31 | keys = sorted(self.c.keys()) |
---|
| 32 | return [(k,self._formatField(k)) for k in keys if k not in ignore] |
---|
| 33 | |
---|
[2187] | 34 | def __repr__(self): |
---|
| 35 | classname = self.__class__.__name__ |
---|
| 36 | |
---|
| 37 | if self._identity_field: |
---|
| 38 | identity = self.__dict__.get(self._identity_field) |
---|
[2193] | 39 | identity = ' ' + (identity and repr(identity) or 'hash=%X'%hash(self)) |
---|
[2187] | 40 | else: |
---|
| 41 | identity = '' |
---|
| 42 | |
---|
| 43 | payload = " ".join(["%s=%s" % (k, v) for k,v in self._fields()]) |
---|
| 44 | if len(payload) > 0: |
---|
| 45 | payload = ": "+payload |
---|
| 46 | |
---|
| 47 | return "<%s%s%s>" % (classname, identity, payload) |
---|
Note: See
TracBrowser
for help on using the repository browser.