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