auxlib.collection

Common collection classes.

class auxlib.collection.AttrDict(*args, **kwargs)[source]

Sub-classes dict, and further allows attribute-like access to dictionary items.

Examples

>>> d = AttrDict({'a': 1})
>>> d.a, d['a'], d.get('a')
(1, 1, 1)
>>> d.b = 2
>>> d.b, d['b']
(2, 2)
auxlib.collection.call_each(seq)[source]

Calls each element of sequence to invoke the side effect.

Parameters:seq

Returns: None

auxlib.collection.first(seq, key=<function <lambda>>, default=None, apply=<function <lambda>>)[source]

Give the first value that satisfies the key test.

Parameters:
  • seq (iterable) –
  • key (callable) – test for each element of iterable
  • default – returned when all elements fail test
  • apply (callable) – applied to element before return

Returns: first element in seq that passes key, mutated with optional apply

Examples

>>> first([0, False, None, [], (), 42])
42
>>> first([0, False, None, [], ()]) is None
True
>>> first([0, False, None, [], ()], default='ohai')
'ohai'
>>> import re
>>> m = first(re.match(regex, 'abc') for regex in ['b.*', 'a(.*)'])
>>> m.group(1)
'bc'

The optional key argument specifies a one-argument predicate function like that used for filter(). The key argument, if supplied, must be in keyword form. For example: >>> first([1, 1, 3, 4, 5], key=lambda x: x % 2 == 0) 4

auxlib.collection.firstitem(map, key=<function <lambda>>, default=None, apply=<function <lambda>>)[source]
class auxlib.collection.frozendict[source]
auxlib.collection.last(seq, key=<function <lambda>>, default=None, apply=<function <lambda>>)[source]