collectors.shortcuts — Useful shortcut functions — Collectors v0.2 documentation

### Navigation

- index
- modules |
- next |
- previous |
- Collectors v0.2 documentation »
- API-Reference »

# `collectors.shortcuts` — Useful shortcut functions

Shortcut functions for `Collector`.

`collectors.shortcuts.` `get`(obj, *attributes)

  This is a shortcut that creates lambda functions for several attributes of an object. All functions will automatically get the attribute’s value each time they are called.

  The results of `get` can be directly passed into a collector.

      >>> from collectors import Collector
      >>> class Spam(object):
      ...     a = 1
      ...     b = 2
      >>> spam = Spam()
      >>> get(spam, 'a', 'b') #doctest: +ELLIPSIS
      (('a', <function <lambda> at 0x...>), ('b', <function <lambda> at 0x...>))
      >>> # Create a collector with help of get:
      >>> c = Collector(get(spam, 'a', 'b'))
      >>> c() # Collect values.
      >>> c
      ([1], [2])

`collectors.shortcuts.` `get_objects`(objs, id_attr, *attributes)

  This function works similarly to `get()`, but it creates the lambda functions for multiple objects (`objs`).

  That becomes useful if you want to monitor the same attributes of several homogeneous objects with one `Collector` instance. The objects must have a unique attribute (`id_attr`), which will be used as prefix in the resulting `(name, func)` tuples. The value of that attribute should begin with a letter or an underscore.

      >>> from collectors import Collector
      >>> class Spam(object):
      ...     a = 1
      ...     b = 2
      ...     def __init__(self, obj_id):
      ...         self.obj_id = 'spam%d' % i
      ...
      >>> spams = [Spam(i) for i in range(2)]
      >>> c = Collector(get_objects(spams, 'obj_id', 'a', 'b'))
      >>> # Attributes are now named like "objid_attrname":
      >>> c.spam0_a, c.spam0_b, c.spam1_a, c.spam1_b
      ([], [], [], [])
      >>> c() # This will collect the values of a and be for each spam object.

`collectors.shortcuts.` `manual`(value)

  This a simple shortcut for a function like `lambda x: x` if you want to pass variable values manually to a monitor:

      >>> from collectors import Collector
      >>> c = Collector(('a', manual))
      >>> c(a=3)
      >>> c
      ([3],)

### Navigation

- index
- modules |
- next |
- previous |
- Collectors v0.2 documentation »
- API-Reference »