fito.specs package

Submodules

fito.specs.base module

exception fito.specs.base.InvalidSpecInstance[source]

Bases: exceptions.Exception

exception fito.specs.base.MainModuleWarning[source]

Bases: exceptions.UserWarning

exception fito.specs.base.MissingUnwiredParamError[source]

Bases: exceptions.Exception

class fito.specs.base.Spec(*args, **kwargs)[source]

Bases: object

Base class for any spec.

It handles the spec checking in order to guarantee that only valid instances can be generated

Suppose you want to specify an experiment

>>> class Experiment(Spec):
>>>    input = SpecField()
>>>    model = SpecField()
>>>
>>> class Input(Spec):
>>>     path = PrimitiveField(0)
>>>
>>> class LinearRegression(Spec):
>>>     regularize = PrimitiveField(default=False)
>>>

Now you can specify an experiment like this

>>> exp = Experiment(input = Input('input.csv'),
...                  model = LinearRegression()
... )
>>>

And write it to a nice yaml

>>> with open('exp_spec.yaml', 'w') as f:
>>>     exp.yaml.dump(f)

And load it back

>>> with open('exp_spec.yaml') as f:
>>>     exp = Experiment.from_yaml()
class Exporter(module, what, **kwargs)[source]

Bases: object

class Spec.Importer(cls, module)[source]

Bases: object

load(path)[source]
loads(string)[source]
classmethod Spec.auto_instance(locals, globals)[source]
Spec.bind(*args, **kwargs)[source]
Spec.copy()[source]
static Spec.dict2spec(dict, path=None)[source]

Loads a Spec from a dictionary :param dict: Dictionary to load it from :param path: Used to build relative paths when referencing other files, see Spec.Importer.load

Spec.diff(other)[source]

Returns a diff that when applied to other returns self

classmethod Spec.from_json()[source]
classmethod Spec.from_yaml()[source]
classmethod Spec.get_bound_fields()[source]
classmethod Spec.get_default_doc_string()[source]
classmethod Spec.get_field_spec(field_name)[source]
classmethod Spec.get_fields()[source]
Spec.get_primitive_fields()[source]
Spec.get_spec_fields()[source]
Spec.get_subspecs(include_self=True)[source]
classmethod Spec.get_unbound_fields()[source]
Spec.initialize(being_created, *args, **kwargs)[source]

Initializes a spec. It handles the bound/unbound semantic.

You have to initialize the spec with all it’s bound fields. Once you’ve initialized the spec, you can initialize it again, this time with all it’s unbound fields.

However, you can initialize the spec all at once too with both bound and unbound fields. That case happens when you load a totally bound serialized spec.

Lastly, the positional arguments’ semantic depends on the initialization state: * When the spec is instanced, *args will map to bound fields * When the spec is then bound, *args will map to unbound fields

Parameters:being_created – Tells the method whether *args map to bound or unbound fields
Spec.inplace_bind(*args, **kwargs)[source]
Spec.json
Spec.key
classmethod Spec.key2dict(str)[source]
static Spec.key2spec(str)[source]
Spec.replace(**kwargs)[source]
Spec.similarity(other)[source]
Spec.to_dict(include_all=False)[source]
Parameters:include_toggles – Wether to include or not toggle_fields, default=False
Spec.to_kwargs(include_all=False)[source]

Useful function to call f(**spec.to_kwargs()) :param include_all: Whether to include the fields whose spec has serialize == False

static Spec.type2spec_class(spec_type)[source]

Can be called either by calling Spec.type2spec_class(‘SomeSpec’) or by calling Spec.type2spec_class(‘some.module:SomeSpec’)

Parameters:spec_type – Either the name of the class, which must be imported before calling this function or the

import path spec :return: A subclass of Spec

Spec.yaml
class fito.specs.base.SpecMeta[source]

Bases: type

exception fito.specs.base.WeirdModulePathException[source]

Bases: exceptions.Exception

fito.specs.base.check_fields(fields, class_name)[source]
fito.specs.base.get_import_path(obj, *attrs)[source]

Builds a string representing an object. The default behaviour is the same than the import statement. Additionaly, you can specify attributes.

For example: >>> get_import_path(Spec) ‘fito.specs.base:Spec’

>>> get_import_path(Spec, 'dict2spec')
'fito.specs.base:Spec.dict2spec'

The inverse function of get_import_path is obj_from_path

fito.specs.base.is_import_path(obj)[source]
fito.specs.base.json_dump(*args, **kwargs)[source]
fito.specs.base.json_dumps(*args, **kwargs)[source]
fito.specs.base.json_load(*args, **kwargs)[source]
fito.specs.base.json_loads(*args, **kwargs)[source]
fito.specs.base.load_object(id)[source]
fito.specs.base.obj_from_path(path)[source]

Retrieves an object from a given import path. The format is slightly different from the standard python one in order to be more expressive.

Examples: >>> obj_from_path(‘fito’) <module ‘fito’>

>>> obj_from_path('fito.specs')
<module 'fito.specs'>
>>> obj_from_path('fito.specs.base:Spec')
fito.specs.base.Spec
>>> obj_from_path('fito.specs.base:Spec.dict2spec')
<function fito.specs.base.dict2spec>
fito.specs.base.set_default_json_options()[source]

fito.specs.fields module

class fito.specs.fields.ArgsField[source]

Bases: fito.specs.fields.SpecCollection

allowed_types
class fito.specs.fields.BaseSpecField(pos=None, default=<object object>, base_type=None, serialize=True, *args, **kwargs)[source]

Bases: fito.specs.fields.Field

Specifies a Field whose value will be an Spec

allowed_types
class fito.specs.fields.BaseUnboundSpec(pos=None, default=<object object>, base_type=None, serialize=True, *args, **kwargs)[source]

Bases: fito.specs.fields.BaseSpecField, fito.specs.fields.UnboundField

class fito.specs.fields.CollectionField(pos=None, default=<object object>, serialize=True, *args, **kwargs)[source]

Bases: fito.specs.fields.PrimitiveField, fito.specs.fields.MockIterable

allowed_types
class fito.specs.fields.Field(pos=None, default=<object object>, serialize=True, *args, **kwargs)[source]

Bases: object

Base class for field definition on an Spec

allowed_types
check_valid_value(value)[source]
has_default_value()[source]
class fito.specs.fields.KwargsField[source]

Bases: fito.specs.fields.SpecCollection

allowed_types
class fito.specs.fields.MockIterable[source]

Bases: object

iteritems()[source]
class fito.specs.fields.NumericField(pos=None, default=<object object>, serialize=True, *args, **kwargs)[source]

Bases: fito.specs.fields.PrimitiveField

allowed_types
class fito.specs.fields.PrimitiveField(pos=None, default=<object object>, serialize=True, *args, **kwargs)[source]

Bases: fito.specs.fields.Field

Specifies a Field whose value is going to be a python object

allowed_types
class fito.specs.fields.SpecCollection(pos=None, default=<object object>, serialize=True, *args, **kwargs)[source]

Bases: fito.specs.fields.Field, fito.specs.fields.MockIterable

Specifies a Field whose value is going to be a collection of specs

allowed_types
check_valid_value(value)[source]
fito.specs.fields.SpecField(pos=None, default=<object object>, base_type=None, serialize=True, spec_field_subclass=None)[source]

Builds a SpecField

Parameters:
  • pos – Position on *args
  • default – Default value
  • base_type – Base type, it does some type checkig + avoids some warnings from IntelliJ
  • serialize – Whether this spec field should be included in the serialization of the object
  • spec_field_subclass – Sublcass of BaseSpecField, useful to extend the lib
Returns:

class fito.specs.fields.UnboundField[source]

Bases: object

class fito.specs.fields.UnboundPrimitiveField(pos=None, default=<object object>, serialize=True, *args, **kwargs)[source]

Bases: fito.specs.fields.PrimitiveField, fito.specs.fields.UnboundField

fito.specs.fields.UnboundSpecField(pos=None, default=<object object>, base_type=None, spec_field_subclass=None)[source]

fito.specs.utils module

Helper functions that allows you to receive and handle collections in a uniform way

fito.specs.utils.general_append(iterable, k, v)[source]
fito.specs.utils.general_iterator(iterable)[source]
fito.specs.utils.general_new(iterable)[source]
fito.specs.utils.is_dict(obj)[source]
fito.specs.utils.is_iterable(obj)[source]
fito.specs.utils.matching_fields(d1, d2)[source]
fito.specs.utils.recursive_map(iterable, callable, recursion_condition=None)[source]

Provides a map that works on lists, dicts and tuples, and is recursive into sub collections (default behaviour)

Parameters:recursion_condition – Predicate specifying on which cases the function should recurse.

Default: is_iterable()

Returns:A similar structure to the given iterable where :param callable: was applied

Example: >>> recursive_map([[1], {“some”: 3, “stuff”:10}], lambda x:x+1) >>> [[2], {‘some’: 4, ‘stuff’: 11}]

>>> recursive_map([[1], {2: 3, 5: 10}], lambda k, v:k+v)
>>> [[1], {2: 5, 5: 15}]

Module contents