Attribute DSL¶
We provide an attribute based DSL for converting attributes into the fields dictionary, with a classmethod for creating a create_spec object for the class.
So we can define something like:
class MyAmazingKls(dictobj.Spec):
one = dictobj.Field(string_spec)
two = dictobj.Field(string_spec, wrapper=listof)
three = dictobj.NullableField(string_spec)
Which is equivalent to
class MyAmazingKls(six.with_metaclass(Field.metaclass, dictobj)):
one = Field(string_spec)
two = Field(string_spec, wrapper=listof)
three = Field(string_spec, nullable=True)
Which is equivalent to:
class MyAmazingKls(dictobj, Field.mixin):
fields = {"one": string_spec, "two": listof(string_spec()), "three": defaulted(or_spec(none_spec(), string_spec()), None)}
and have MyAmazingKls.FieldSpec().normalise for normalising a dictionary into an instance of MyAmazingKls!
-
class
input_algorithms.field_spec.
Field
(spec=<class 'input_algorithms.spec_base.NotSpecified'>, help=None, formatted=False, wrapper=<class 'input_algorithms.spec_base.NotSpecified'>, default=<class 'input_algorithms.spec_base.NotSpecified'>, nullable=False, format_into=<class 'input_algorithms.spec_base.NotSpecified'>, after_format=<class 'input_algorithms.spec_base.NotSpecified'>)¶ Representation of a single Field
This has a reference to the mixin and metaclass in this file.
It also let’s you define things like:
Whether this is a formatted field
has a default
Is wrapped by some other spec class
-
make_spec
(meta, formatter)¶ Create the spec for this Field:
If callable, then call it
If is nullable * or the spec with none_spec * if we have an after format, do the same with that
if it has a default, wrap in defaulted
If it can be formatted, wrap in formatted
If it has a wrapper, wrap it with that
Return the result!
Helpers¶
-
class
input_algorithms.field_spec.
FieldSpec
(kls, formatter=None, create_kls=None)¶ Responsible for defining the Spec object used to convert a dictionary into an instance of the kls.
-
make_spec
(meta)¶ Assume self.kls has a fields dictionary of
fields = {name: (description, options)}
or
fields = {name: options}
Where options may be:
A callable object to a spec or Field
A spec
A Field
If it’s a Field, we create a spec from that
Finally we create a create_spec with all the fields
-
normalise
(meta, val)¶ Normalise val with the spec from self.make_spec
-
-
class
input_algorithms.field_spec.
FieldSpecMetakls
¶ A metaclass that converts attributes into a fields dictionary at class creation time.
It looks for any attributes on the class that have an attibute of “is_input_algorithms_field” that is a Truthy value
It will then:
Ensure FieldSpecMixin is one of the base classes
There is a fields dictionary containing all the defined Fields
-
static
__new__
(metaname, classname, baseclasses, attrs)¶ Create and return a new object. See help(type) for accurate signature.