class Person(LegalPerson): age = Property(isa=int) kids = Property(isa=int, extraneous=True) interests = SafeProperty(isa=list) info = SafeProperty(isa=dict) primary_key = ['id'] family = DictProperty(of=LegalPerson)
class Diff(ListCollection): """Container for a list of differences.""" base_type_name = SafeProperty(isa=str, extraneous=True, doc="Type name of the source object") other_type_name = SafeProperty( isa=str, extraneous=True, doc="Type name of the compared object; normally the same, unless " "the ``duck_type`` option was specified.") itemtype = DiffInfo def __str__(self): what = ( "%s vs %s" % (self.base_type_name, self.other_type_name) if self.base_type_name != self.other_type_name else self.base_type_name ) diffstate = collections.defaultdict(list) for diff in self: if diff.diff_type == DiffTypes.ADDED: diffstate["+NEW"].append(diff.other) elif diff.diff_type == DiffTypes.REMOVED: diffstate["-OLD"].append(diff.base) elif diff.diff_type == DiffTypes.MODIFIED: if diff.base.path == diff.other.path: diffstate['<>X'].append(diff.base) else: diffstate['<->OLD'].append(diff.base) diffstate['<+>NEW'].append(diff.other) elif diff.diff_type == DiffTypes.NO_CHANGE: diffstate['==X'].append(diff.base) prefix_paths = [] for k, v in diffstate.items(): prefix_paths.append( "{prefix}({paths})".format( prefix=k, paths=MultiFieldSelector(*v).path, ) ) return "<Diff [{what}]; {n} diff(s){summary}>".format( n=len(self), what=what, summary=( ": " + "; ".join( "{prefix}({paths})".format( prefix=k, paths=MultiFieldSelector(*v).path, ) for (k, v) in diffstate.items() ) if diffstate else "" ), )
class DiffInfo(Record): """ Container for storing diff information that can be used to reconstruct the values diffed. """ diff_type = SafeProperty( coerce=_coerce_diff, isa=DiffTypes.EnumValue, empty_attr=None, required=True, doc="Enumeration describing the type of difference; a " ":py:class:`DiffType` value.") base = SafeProperty( isa=FieldSelector, required=True, doc="A FieldSelector object referring to the location within the " "base object that the changed field was found. If the " "``diff_type`` is ``DiffTypes.ADDED``, then this will be the " "location of the record the field was added in, not the " "(non-existant) field itself.", ) other = SafeProperty( isa=FieldSelector, required=True, doc="A FieldSelector object referring to the location within the " "'other' object that the changed field was found. If the " "``diff_type`` is ``DiffTypes.REMOVED``, then this will be " "location of the record the field was removed from, not the " "(non-existant) field itself.", ) def __str__(self): if self.base.path != self.other.path: pathinfo = ( self.base.path if ( len(self.base) > len(self.other) and self.base.startswith(self.other) ) else self.other.path if ( len(self.other) > len(self.base) and self.other.startswith(self.base) ) else "(%s/%s)" % (self.base.path, self.other.path) ) else: pathinfo = self.other.path difftype = self.diff_type.display_name return "<DiffInfo: %s %s>" % (difftype, pathinfo)
class VariedRecord(Record): def _lazy(self): return "%s.%d" % (self.must, self.id) id = ROLazyProperty( required=True, check=lambda i: i > 0, default=seq, ) must = SafeProperty(required=True) lazy = LazySafeProperty( check=lambda i: re.match(r'\w+\.\d+$', i), default=_lazy, )
class FussyRecord(Record): id = Property(required=True, isa=int) natural = SafeProperty(check=lambda i: i > 0) must = SafeProperty(required=True) rbn = SafeProperty(required=True, isa=(str, type(None)))
class CheeseCupboardRecord(Record): id = ROProperty(required=True, isa=int) name = SafeProperty(isa=str) best_cheese = SafeProperty(isa=CheeseRecord) cheeses = ListProperty(of=CheeseRecord) favorites = DictProperty(of=CheeseRecord)
class CheeseRecord(Record): variety = SafeProperty(isa=str) smelliness = SafeProperty(isa=float, check=lambda x: 0 < x < 100)