segram.datastruct.collections module

Enhanced collections.abc classes implementing generic data filtering and transformation method.

class segram.datastruct.collections.DataABC[source]

Bases: Iterable

Abstract base class for data classes.

pipe(func: str | Callable[[Self, ...], Any], *args: Any, **kwds: Any) Any[source]

Pipe self to a function.

class segram.datastruct.collections.DataIterable[source]

Bases: DataABC

Abstract base class for data iterables.

get(attr: str) Self[source]

Extract attributes from data items.

map(func: str | Callable[[Any, ...], Any], *args: Any, **kwds: Any) Self[source]

Map data iterator.

filter(func: str | Callable[[Any, ...], bool] | None, *args: Any, **kwds: Any) Self[source]

Filter data iterator.

unique(key: str | Callable[[Any, ...], Any] | None = None) Self[source]

Return unique values (only first unique occurences are returned).

groupby(*args: Any, **kwds: Any) Self[source]

Group by key attribute or function/method.

Importantly, the key function/values must be sortable.

Parameters:
  • *args – First argument is interpreted as key function (or its name). The rest is passed as actual *args to the function. No grouping is done if no function/name is passed.

  • **kwds – Passed to the function.

zip(iterable: Iterable) Self[source]

Zip with other iterable.

class segram.datastruct.collections.DataIterator(data: Iterable, /)[source]

Bases: Iterator, DataIterable

Data iterators class.

class segram.datastruct.collections.DataSequence[source]

Bases: Sequence, DataIterable

Data sequence class.

pairwise() Iterable[tuple[Any, Any]][source]

Iterate over all pairs of data items.

sort(*args: Any, reverse: bool = False, show_keys: bool = False, **kwds: Any) Self[source]

Sort elements.

It is typically best to first flatten the sequence in case it contains nested sequences.

Parameters:
  • *args – Name of an attribute or a method defined on items. Alternatively a callable. Further positional arguments are passed to the key function. If no positional arguments are used then standard data item sorting is used. Alternatively, an iterable of values used for sorting can be passed.

  • show_keys – Should sorting key values be returned together with the objects (so 2-tuples are returned).

  • **kwds – Passed to the sorting callable.

class segram.datastruct.collections.DataMapping[source]

Bases: Mapping, DataABC

Abstract base class for data mappings.

keys() a set-like object providing a view on D's keys[source]
values() an object providing a view on D's values[source]
items() a set-like object providing a view on D's items[source]
map(_what: Literal['items', 'keys', 'values'], *args: Any, **kwds: Any) Self[source]

Map over keys, values or items and return a transformed dictionary.

Parameters:
  • _what – Part of dictionary to process.

  • *args – Passed to DataIteratorABC.map().

  • **kwds – Passed to DataIteratorABC.map().

filter(_what: Literal['items', 'keys', 'values'], *args: Any, **kwds: Any) Self[source]

Filter dictonary by keys, values or items.

Parameters:
  • _what – Part of dictionary to process.

  • *args – Passed to DataIteratorABC.map().

  • **kwds – Passed to DataIteratorABC.map().

sort(_what: Literal['items', 'keys', 'values'], *args: Any, **kwds: Any) Self[source]

Sort dictionary.

Parameters:
  • _what – Part of dictionary to process.

  • *args – Passed to DataSequenceABC.sort(), which is called on self.items().

  • **kwds – Passed to DataSequenceABC.sort(), which is called on self.items().

class segram.datastruct.collections.DataTuple(iterable=(), /)[source]

Bases: tuple, DataSequence

Data tuple class.

class segram.datastruct.collections.DataList(iterable=(), /)[source]

Bases: list, DataSequence

Data list class.

class segram.datastruct.collections.DataDict[source]

Bases: dict, DataMapping

Data dict class.

keys() a set-like object providing a view on D's keys
values() an object providing a view on D's values
items() a set-like object providing a view on D's items