Collection

Back to Scenes

A list-based sequence with helper methods, which serves as the base class for SceneCollection.

class Collection(iterable=None)[source]

List-based sequence with convenience methods for mapping and filtering, and NumPy-style fancy indexing

append(x)[source]

Append x to end of self

extend(x)[source]

Extend self by appending elements from the iterable

filter(predicate)[source]

Returns Collection of items in self for which predicate(item) is True

groupby(*predicates)[source]

Groups items by predicates and yields tuple of (group, items) for each group, where items is a Collection.

Each predicate can be a key function, or a string of dot-chained attributes to use as sort keys.

Examples

>>> import collections
>>> FooBar = collections.namedtuple("FooBar", ["foo", "bar"])
>>> c = Collection([FooBar("a", True), FooBar("b", False), FooBar("a", False)])
>>> for group, items in c.groupby("foo"):
...     print(group, items)
a Collection([FooBar(foo='a', bar=True), FooBar(foo='a', bar=False)])
b Collection([FooBar(foo='b', bar=False)])
>>> for group, items in c.groupby("bar"):
...     print(group, items)
False Collection([FooBar(foo='b', bar=False), FooBar(foo='a', bar=False)])
True Collection([FooBar(foo='a', bar=True)])
map(f)[source]

Returns Collection of f applied to each item in self

sorted(*predicates, **reverse)[source]

Returns a copy of self, sorted by predicates in ascending order.

Each predicate can be a key function, or a string of dot-chained attributes to use as sort keys. The reverse flag returns results in descending order.

Examples

>>> import collections
>>> FooBar = collections.namedtuple("FooBar", ["foo", "bar"])
>>> X = collections.namedtuple("X", "x")
>>> c = Collection([FooBar(1, X("one")), FooBar(2, X("two")), FooBar(3, X("three"))])
>>> c.sorted("foo")
Collection([FooBar(foo=1, bar=X(x='one')), FooBar(foo=2, bar=X(x='two')), FooBar(foo=3, bar=X(x='three'))])
>>> c.sorted("bar.x")
Collection([FooBar(foo=1, bar=X(x='one')), FooBar(foo=3, bar=X(x='three')), FooBar(foo=2, bar=X(x='two'))])
each

Any operations chained onto .each (attribute access, item access, and calls) are applied to each item in the Collection.

Notes

  • Add .combine() at the end of the operations chain to combine the results into a list by default, or any container type passed into .combine()
  • Use .pipe(f, *args, **kwargs) to yield f(x, *args, **kwargs) for each item x yielded by the preceeding operations chain

Examples

>>> c = Collection(["one", "two", "three", "four"])
>>> for x in c.each.capitalize():
...     print(x)
One
Two
Three
Four
>>> c.each.capitalize()[:2]
'On'
'Tw'
'Th'
'Fo'
>>> c.each.capitalize().pipe(len)
3
3
5
4
>>> c.each.capitalize().pipe(len).combine(set)
{3, 4, 5}
Yields:item with all operations following .each applied to it
class Eacher(iterable)[source]

Applies operations chained onto it to each item in an iterator

combine(collection=<type 'list'>)[source]

self.combine(collection) <--> collection(iter(self))

pipe(callable, *args, **kwargs)[source]

self.pipe(f, *args, **kwargs) <--> f(x, *args, **kwargs) for x in self