flib.base.operator module

Classes and functions for functional programming.

class Getter(*args, domain: Union[type, None, Tuple[Optional[type], Tuple[Hashable, ...]], Domain] = None, target: Union[type, None, Tuple[Optional[type], Tuple[Hashable, ...]], Domain] = None)

Bases: flib.base.operator.Operator

Class for Getters.

A getter essentially is the composition of a fetch operation, that specifies the fields of a given domain type and a subsequent representation of the fetched fields as an object of given target type, by using the target frame (if given) as field identifiers.

Parameters:
  • *args – Valid field identifiers within the domain type.
  • domain – Optional domain like parameter, that specifies the type and (if required) the frame of the operator’s domain. Supported domain types are object, subclasses of the Mapping class and subclasses of the Sequence class. If no domain type is specified (which is indicated by the default value None) the fields are identified by their argument positions of the operator.
  • target – Optional domain like parameter, that specifies the type and (if required) the frame of the operator’s target. Supported target types are tuple, list and dict. If no target is specified (which is indicated by the default value None) the target type depends on the arguments, that are passed to the operator. In this case for a single argument, the target type equals the type of the argument and for multiple argumnts, the target type is tuple.
class Identity(domain: Union[type, None, Tuple[Optional[type], Tuple[Hashable, ...]], Domain] = None)

Bases: flib.base.operator.Operator

Class for identity operators.

Parameters:domain
class Lambda(expression: str = '', domain: Union[type, None, Tuple[Optional[type], Tuple[Hashable, ...]], Domain] = None, variables: Tuple[str, ...] = (), default: Optional[Callable[[...], Any]] = None, compile: bool = True)

Bases: flib.base.operator.Operator

Class for operators, that are based on arithmetic expressions.

Parameters:
  • expression
  • domain – Optional domain category of the operator. If provided, the category has to be given as a type. Supported types are object, subclasses of the class:Mapping class <collection.abs.Mapping> and subclasses of the Sequence class. The default domain is object.
  • variables – Tuple of variable names. This parameter is only required, if the domain category is a subclass of the Sequence class. In this case the variable names are used to map the fields (given as names) to their indices within the domain tuple.
  • default
  • compile – Optional Boolean parameter, which determines if the operator is compiled after it is parsed.
variables
class Operator(*args, domain: Union[type, None, Tuple[Optional[type], Tuple[Hashable, ...]], Domain] = None, target: Union[type, None, Tuple[Optional[type], Tuple[Hashable, ...]], Domain] = None)

Bases: collections.abc.Callable, flib.base.abc.Multiton

Abstract Base Class for operators.

Parameters:
  • *args
  • domain
  • target
domain
target
class Vector(*args, domain: Union[type, None, Tuple[Optional[type], Tuple[Hashable, ...]], Domain] = None, target: Union[type, None, Tuple[Optional[type], Tuple[Hashable, ...]], Domain] = None, default: Optional[Callable[[...], Any]] = None)

Bases: collections.abc.Sequence, flib.base.operator.Operator

Class for vectorial functions.

Parameters:
  • *args – Optional definitions of the function components. If provided, any component has to be given as a valid variable definition.
  • domain – Optional domain like parameter, that specifies the type and (if required) the frame of the operator’s domain. The accepted parameter values are documented in the class Getter.
  • target – Optional domain like parameter, that specifies the type and (if required) the frame of the operator’s target. The accepted parameter values are documented in the class Getter.
  • default – Default operator which is used to map fields to field variables. By default the identity is used.
components
fields
class Zero(target: Union[type, None, Tuple[Optional[type], Tuple[Hashable, ...]], Domain] = None)

Bases: flib.base.operator.Operator

Class for zero operators.

A zero operator (or zero morphism) maps all given arguments to the zero object (empty object) of a given target category.

Parameters:target – Optional target category of the operator. If provided, the target category must by given as a type, like int, float, str, set, tuple, list, dict or object. Then the returned operator maps all objects of any domain category to the zero object of the target category. By default the used zero object is None.
compose(*args, unpack: bool = False) → Callable[[...], Any]

Compose operators.

Parameters:*args – Operators, which shall be composed. If provided, any given operator is required to be a callable or None.
Returns:Composition of all arguments, that do not evaluate to False. If all arguments evaluate to False, the identity operator is returned.
create_aggregator(*args, domain: Union[type, None, Tuple[Optional[type], Tuple[Hashable, ...]], Domain] = None, target: type = <class 'tuple'>) → Callable[[Sequence[Any]], Any]

Creates an aggregation operator with specified variables.

Parameters:
  • *args – Optional variable definitions. If provided the operators given within the variable definitions are required to be valid aggregation functions
  • domain – Optional domain category of the operator. If provided, the category has to be given as a type. Supported types are object, subclasses of the class:Mapping class <collection.abs.Mapping> and subclasses of the Sequence class. The default domain is object.
  • target – Optional target category of the operator. If provided, the category has to be given as a type. Supported types are tuple and :dict:’dict’. If no target type is specified, the target category of the operator depends on the domain. In this case for the domain object, the target type is documented by the the builtin function attrgetter(), for other domains by the function itemgetter().

Return:

create_group_aggregator(*args, key: Union[Hashable, Tuple[Hashable, ...], None] = None, domain: Union[type, None, Tuple[Optional[type], Tuple[Hashable, ...]], Domain] = None, target: type = <class 'tuple'>, presorted: bool = False) → Callable[[Sequence[Any]], Any]

Creates a group aggregation operator.

Parameters:
  • *args – Optional variable definitions. If provided the operators given within the variable definitions are required to be valid aggregation functions. If not provided, the returned operator is the identity.
  • key – Optional grouping key. If provided, the grouping key can be a field identifier or a composite key, given by a tuple of field identifiers. Thereby the type and the concrete meaning of the field identifiers depends on the domain of the operator.
  • domain – Optional domain category of the operator. If provided, the category has to be given as a type. Supported types are object, subclasses of the class:Mapping class <collection.abs.Mapping> and subclasses of the Sequence class. The default domain is object.
  • target – Optional target category of the operator. If provided, the category has to be given as a type. Supported types are tuple and :dict:’dict’. If no target type is specified, the target category of the operator depends on the domain. In this case for the domain object, the target type is documented by the the builtin function attrgetter(), for other domains by the function itemgetter().
  • target – Optional target type of the operator. Supported types are tuple and :dict:’dict’. If no target type is specified, the target of the operator depends on the domain. In this caser for the domain object, the target type is documented by the the builtin function attrgetter(), for other domains by the function itemgetter().
  • presorted – The operation splits in three consecutive steps, where in the first step the input sequence is sorted by the given keys. Consequently if the sequences are already sorted, the first step is not required and can be omitted to increase the performance of the operator. By default the input sequences are assumed not to be presorted.

Returns:

create_grouper(*args, domain: Union[type, None, Tuple[Optional[type], Tuple[Hashable, ...]], Domain] = None, presorted: bool = False) → Callable[[Sequence[Any]], Any]

Create a grouping operator with fixed grouping keys.

Parameters:
  • *args – Optional grouping keys, which are used to group sequences of objects of given domain type. If provided, any grouping key is required to be a valid field identifier for the domain type.
  • domain – Optional domain like parameter, that specifies the type and (if required) the frame of the operator’s domain. The accepted parameter values are documented in the class Getter.
  • presorted – The grouping operation splits in two consecutive steps: In the first step the input sequence is sorted by the given keys. Thereupon in the second step the sorted sequence is partitioned in blocks, which are equal with respect to the keys. Consequently if the sequences are already sorted, the first step is not required and can be omitted to increase the performance of the operator. By default the input sequences are assumed not to be presorted.
Returns:

List of sequences containing objects of a given domain typr, which are equal with respect to given grouping keys.

create_setter(*args, domain: Union[type, None, Tuple[Optional[type], Tuple[Hashable, ...]], Domain] = <class 'object'>) → Callable[[...], Any]

Create a setter operator.

Parameters:
  • *args – Optional pairs containing field values. If provided, the pairs have to be given in the format (<field>, <value>), where <field> is a valid field identifier within the domain type and <value> an arbitrary object.
  • domain – Optional domain like parameter, that specifies the type and (if required) the frame of the operator’s domain. Supported domain types are object, subclasses of the class:Mapping class <collection.abs.Mapping> and subclasses of the Sequence class. If no domain type is specified (which is indicated by the default value None) the returned operator ir the zero operator.
create_sorter(*args, domain: Union[type, None, Tuple[Optional[type], Tuple[Hashable, ...]], Domain] = None, reverse: bool = False) → Callable[[Sequence[Any]], Sequence[Any]]

Create a sorter with fixed sorting keys.

Sorters are operators, that act on sequences of objects of a given category and change the order of the objects within the sequence.

Parameters:
  • *args – Optional sorting keys, which in hierarchically descending order are used to sort sequences of objects of given domain type. If provided, any sorting key is required to be a valid field identifier for the domain type.
  • domain – Optional domain like parameter, that specifies the type and (if required) the frame of the operator’s domain. The accepted parameter values are documented in the class Getter.
  • reverse – Optional boolean parameter. If set to True, then the sequence elements are sorted as if each comparison were reversed.
Returns:

Callable function which sorts a sequence of objects of a given domain by given sorting keys.

create_wrapper(**attrs) → Callable[[...], Any]

Create a function wrapper that adds attributes.

Parameters:**attrs – Arbitrary keyword arguments
Returns:Function wrapper for given function, with additional specified attributes.