cola.model – Serializable model base class

This module provides the Model class, a generic, serializable data container.

class cola.model.Model

Model is a base class that implements convenient serialization methods.

clone()

Creates a clone of the current object.

>>> m = Model()
>>> m.answer = 42
>>> clone = m.clone()
>>> clone.answer
42
>>> type(clone)
<class 'cola.model.Model'>
copy_params(model, params_to_copy=None)

Copies params from one model to another.

>>> m = Model()
>>> m.answer = 42
>>> m._question = 'unknown'
>>> m2 = Model()
>>> m2.copy_params(m)
>>> m2._question
'unknown'
>>> m2.answer
42
from_dict(source_dict)

Import a complex model from a dictionary. If it looks like a duck, it’s a duck.

>>> Model().from_dict({'answer': 42}).answer
42
get_param(param, default=None)

Returns the value of a model parameter.

>>> m = Model()
>>> m.answer = 42
>>> m.get_param('answer')
42
>>> m.get_param('another answer', 42)
42
get_param_names(export=False)

Returns a list of serializable attribute names.

>>> m = Model()
>>> m._question = 'unknown'
>>> m.answer = 42
>>> m.get_param_names()
['answer']
>>> m.get_param_names(export=True)
['_question', 'answer']
has_param(param)

Returns true if a parameter exists in a model.

>>> m = Model()
>>> m.answer = 42
>>> m.has_param('answer')
True
>>> m.has_param('question')
False
static instance(filename)
Instances a model from a filename.
items(raw=False)

Provides a dictionary-like items() iterator.

>>> m = Model().from_dict(dict(a='a', b='b'))
>>> value = 'nada'
>>> for k, v in m.items(): value = v;
>>> value
'b'
iteritems(raw=False)

Provides a dictionary-like iteritems() iterator.

>>> m = Model().from_dict(dict(a='a', b='b'))
>>> value = 'nada'
>>> for k, v in m.iteritems(): value = v;
>>> value
'b'
load(filename)
Loads model state from a file.
save(filename)
Saves a model to a file.
set_param(param, value, check_params=False)

Set attributes with optional validity checks.

>>> m = Model()
>>> m.answer = 41
>>> m.set_param('answer', 42)
>>> m.answer
42
to_dict()

Exports a model to a dictionary. This simplifies serialization.

>>> is_dict(Model().to_dict())
True
class cola.model.ModelIterator(model)

Provides an iterator over model (key, value) pairs.

next()
cola.model.is_atom(item)
Is this an atom?
cola.model.is_dict(item)
Is this an instance of a dictionary?
cola.model.is_function(item)
Is this a function?
cola.model.is_list(item)
Is this a list?
cola.model.is_model(item)
Is this an instance of a Model-like object?

Previous topic

Base Classes

Next topic

cola.observable – Observable base class

This Page