collectors.core — Core classes

This module contains Collector’s core classes like like the Collector itself and the base base and default storages.

class collectors.core.Collector(*args[, **kwargs])

This class can monitor the values of a given set of variables.

Each variable is described by a name and a collector function which must be passed as tuple (name, func). The list of name-func-tuples may also be nested, which is helpful for some convenience functions.

For each variable a series for the observed values will be created. The variables’ series are accessible via an index (defined by the order you passed them) as well as by an attribute of a Collector instance named after name.

All collector functions will be called (in the same order as they were specified) each time a Collector instance is called. The collector functions may either grab the desired values by themselves or let them be passed manually with each collector call.

Here is an example how this works:

>>> class Spam(object):
...     a = 1
...     b = 2
...
>>> spam = Spam()
>>> c = Collector(('a', lambda: spam.a), ('b', lambda x: x))
>>> c.a == c[0], c.b == c[1]
(True, True)
>>>
>>> c(b=spam.b + 2)
>>> spam.a, spam.b = 3, 4
>>> c(b=spam.b + 2)
>>> c # Collector inherits tuple, so you can do this:
([1, 3], [4, 6])
>>> c.a, c.b # You can also access it's elements by their name.
([1, 3], [4, 6])

In this example, spam is the object to be monitored. The monitor is configured to observe two variables named “a” and “b”. The collector function for “a” automatically retrieves spam.a, while the value for “b” needs to be passed to the monitor manually as a keyword argument. For these common cases, there are the shortcuts get() and manual().

Note that names for data series need to be unique. Collector instanciation will raise a ValueError if there’s a duplicate name.

__call__(**kwargs)

Execute all collector functions and append the retrieved values to the series of each variable.

If a collector function required a parameter, you must pass it as keyword argument with the variable’s name.

collect
This is just an alias to __call__().
class collectors.core.BaseStorage

Base class for storages.

Storages define, where and in which format the collected data will be stored. During its initialization a Collector will call create_series() for each monitored variable. Every time the Collector is asked to collect the current values append() invoked with the appropiate series and value.

append(series, value)

Append value to series.

You can either override this method in a subclass or assign an existing method (or lambda function) to the attribute self.append.

create_series(name, index)
Create a new series for the variable name at index of the collector.
class collectors.core.DefaultStorage

Bases: collectors.core.BaseStorage

The default storage used by Collector. The values of each monitored variable are stored in a simple list.

append(series, value)

Append value to series.

You can either override this method in a subclass or assign an existing method (or lambda function) to the attribute self.append.

create_series(name, index)
Return a new empty list.

Previous topic

API-Reference

Next topic

collectors.shortcuts — Useful shortcut functions

This Page