Collection

Back to Scenes

Classes:

Collection([iterable, item_type]) List-based sequence with convenience methods for mapping and filtering, and NumPy-style fancy indexing
Eacher(iterable) Applies operations chained onto it to each item in an iterator
class Collection(iterable=None, item_type=None)[source]

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

Methods:

append(x) Append x to the end of this Collection.
extend(x) Extend this Collection by appending elements from the iterable.
filter(predicate) Returns a Collection filtered by predicate.
groupby(*predicates) Groups items by predicates.
map(f) Returns a Collection of f applied to each item.
sort(field[, ascending]) Returns a Collection, sorted by the given field and direction.
sorted(*predicates, **reverse) Returns a Collection, sorted by predicates in ascending order.

Attributes:

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

Append x to the end of this Collection.

The type of the item must match the type of the collection.

Parameters:x (Any) – Add an item to the collection
extend(x)[source]

Extend this Collection by appending elements from the iterable.

The type of the items in the list must all match the type of the collection.

Parameters:x (List[Any]) – Extend a collection with the items from the list.
filter(predicate)[source]

Returns a Collection filtered by predicate.

Predicate can either be a callable or an Expression from property_filtering.

If the predicate is a callable, filter() will return all items for which predicate(item) is True.

If the predicate is an Expression, filter() will return all items for which predicate.evaluate(item) is True.

Parameters:predicate (callable or Expression) – Either a callable or a property_filtering Expression which is called or evaluated for each item in the list.
Returns:A new collection with only those items for which the predicate returned or evaluated to True.
Return type:Collection
groupby(*predicates)[source]

Groups items by predicates.

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.

Parameters:predicates (callable or str) – Any positional arguments are predicates. If the predicate is a string, it denotes an attribute for each element, potentially with levels separated by a dot. If the predicate is a callable, it must return the value to sort by for each given element.
Yields:Tuple[str, Collection] – A tuple of (group, Collection) for each group.

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)
...     print(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)
...     print(items)
False
Collection([FooBar(foo='b', bar=False), FooBar(foo='a', bar=False)])
True
Collection([FooBar(foo='a', bar=True)])
map(f)[source]

Returns a Collection of f applied to each item.

Parameters:f (callable) – Apply function f to each element of the collection and return the result as a collection.
Returns:A collection with the results of the function f applied to each element of the original collection.
Return type:Collection
sort(field, ascending=True)[source]

Returns a Collection, sorted by the given field and direction.

Parameters:
  • field (str) – The name of the field to sort by
  • ascending (bool) – Sorts results in ascending order if True (the default), and in descending order if False.
Returns:

The sorted collection.

Return type:

Collection

Example

>>> from descarteslabs.catalog import Product
>>> collection = Product.search().collect() 
>>> sorted_collection = collection.sort("created", ascending=False) 
>>> sorted_collection 
sorted(*predicates, **reverse)[source]

Returns a Collection, 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.

Parameters:
  • predicates (callable or str) – Any positional arguments are predicates. If the predicate is a string, it denotes an attribute for each element, potentially with levels separated by a dot. If the predicate is a callable, it must return the value to sort by for each given element.
  • reverse (bool) – The sort is ascending by default, by setting reverse to True, the sort will be descending.
Returns:

The sorted collection.

Return type:

Collection

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'))])
property each

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

Yields:Any – The result of an item with all operations following each applied to it.

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
>>> list(c.each.capitalize().pipe(len).combine(set))
[3, 4, 5]
class Eacher(iterable)[source]

Applies operations chained onto it to each item in an iterator

Methods:

collect(collection)  collection)
combine(collection)  op)
pipe(callable, *args, **kwargs) self.pipe(f, *args, **kwargs) <–> f(x, *args, **kwargs) for x in self
collect(collection) <--> collection(iter(self))[source]
combine(collection) <--> op(iter(self))[source]
pipe(callable, *args, **kwargs)[source]

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