fields¶
How it works: the library is composed of 2 major parts:
The sealers. They return a class that implements a container according the given specification (list of field names and default values).
The factory. A metaclass that implements attribute/item access, so you can do
Fields.a.b.c
. On each getattr/getitem it returns a new instance with the new state. Its__new__
method takes extra arguments to store the contruction state and it works in two ways:Construction phase (there are no bases). Make new instances of the Factory with new state.
Usage phase. When subclassed (there are bases) it will use the sealer to return the final class.
-
fields.
class_sealer
(fields, defaults, base=<class 'fields.__base__'>, make_init_func=<function make_init_func>, initializer=True, comparable=True, printable=True, convertible=False, pass_kwargs=False)[source]¶ This sealer makes a normal container class. It’s mutable and supports arguments with default values.
-
fields.
factory
(sealer, **sealer_options)[source]¶ Create a factory that will produce a class using the given
sealer
.- Parameters
sealer (func) –
A function that takes
fields, defaults
as arguments, where:fields (list): A list with all the field names in the declared order.
defaults (dict): A dict with all the defaults.
sealer_options – Optional keyword arguments passed to
sealer
.
- Returns
A class on which you can do .field1.field2.field3…. When it’s subclassed it “seals”, and whatever the
sealer
returned for the given fields is used as the baseclass.Example:
>>> def sealer(fields, defaults): ... print("Creating class with:") ... print(" fields = {0}".format(fields)) ... print(" defaults = {0}".format(defaults)) ... return object ... >>> Fields = factory(sealer) >>> class Foo(Fields.foo.bar.lorem[1].ipsum[2]): ... pass ... Creating class with: fields = ['foo', 'bar', 'lorem', 'ipsum'] defaults = OrderedDict([('lorem', 1), ('ipsum', 2)]) >>> Foo <class '...Foo'> >>> Foo.__bases__ (<... 'object'>,)
-
fields.
slots_class_sealer
(fields, defaults)[source]¶ This sealer makes a container class that uses
__slots__
(it usesclass_sealer()
internally).The resulting class has a metaclass that forcibly sets
__slots__
on subclasses.
-
class
fields.
Fields
¶ Container class generator. The resulting class will implement
__repr__
,__init__
,__eq__
,__ne__
,__lt__
,__gt__
,__le__
,__ge__
and__hash__
.Usage:
class Foobar(Fields.foo.bar): pass
-
class
fields.
BareFields
¶ Container class generator. The resulting class will implement
__init__
.Usage:
class Foobar(BareFields.foo.bar): pass
-
class
fields.
PrintableMixin
¶ Container class generator. The resulting class will implement
__repr__
.Usage:
class Foobar(PrintableMixin.foo.bar): # we need to have the `foo` and `bar` attributes foo = None bar = None
-
class
fields.
ComparableMixin
¶ Container class generator. The resulting class will implement
__eq__
,__ne__
,__lt__
,__gt__
,__le__
,__ge__
and__hash__
.Usage:
class Foobar(BareFields.name.extra, ComparableMixin.name): """ A class that only compares on `name` but has an `extra` field. """ pass