fumus.queries.query
Classes
Abstraction over a sequence of elements supporting sequential aggregate operations |
Module Contents
- class fumus.queries.query.Query(iterable)[source]
Bases:
fumus.queries.itertools_mixin.ItertoolsMixinAbstraction over a sequence of elements supporting sequential aggregate operations
- _iterable
- _is_consumed = False
- _on_close_handler = None
- classmethod of_nullable(iterable)[source]
Creates Query from args if iterable is not None; otherwise returns empty Query
- classmethod generate(supplier)[source]
Creates infinite unordered Query with values generated by given supplier function
- classmethod from_range(*range_list: int)[source]
Creates Query from start (inclusive) to stop (exclusive) by an incremental step
- property iterable
- concat(*queries)[source]
Concatenates several queries together or adds new queries/collections to the current one
- map(mapper)[source]
Returns a query consisting of the results of applying the given function to the elements of this query
- filter_map(mapper, *, discard_falsy=False)[source]
Filters out all None or falsy values and applies mapper function to the elements of the query
- flat_map(mapper)[source]
Maps each element of the query and yields the elements of the produced iterators
- peek(operation)[source]
Performs the provided operation on each element of the query without consuming it
- skip(count)[source]
Discards the first n elements of the query and returns a new query with the remaining ones
- limit(count)[source]
Returns a query with the first n elements, or fewer if the underlying iterator ends sooner
- tail(count)[source]
Returns a query with the last n elements, or fewer if the underlying iterator ends sooner
- drop_while(predicate)[source]
Returns a query that skips elements based on a predicate and yields the remaining ones
- take_first(default=None)[source]
Returns Optional with the first element of the query or a default value
- take_last(default=None)[source]
Returns Optional with the last element of the query or a default value
- sort(comparator=None, *, reverse=False)[source]
Sorts the elements of the current query according to natural order or based on the given comparator. If ‘reverse’ flag is True, the elements are sorted in descending order
- reverse(comparator=None)[source]
Sorts the elements of the current query in descending order. Alias for ‘sort(comparator, reverse=True)’
- find_first(predicate=None)[source]
Searches for an element of the query that satisfies a predicate. Returns an Optional with the first found value, if any, or None
- find_any(predicate=None)[source]
Searches for an element of the query that satisfies a predicate. Returns an Optional with some of the found values, if any, or None
- min(comparator=None, default=None)[source]
Returns the minimum element of the query according to the given comparator
- max(comparator=None, default=None)[source]
Returns the maximum element of the query according to the given comparator
- enumerate(start=0)[source]
Returns each element of the Query preceded by his corresponding index (by default starting from 0 if not specified otherwise)
- reduce(accumulator, identity=None)[source]
Reduces the elements to a single one, by repeatedly applying a reducing operation. Returns Optional with the result, if any, or None
- compare_with(other, comparator=None)[source]
Compares current query with another one based on a given comparator
- collect(collection_type, dict_collector=None, dict_merger=None, str_delimiter=', ')[source]
Returns a collection from the query.
In case of dict: The ‘dict_collector’ function receives an element from the query and returns a (key, value) pair or a DictItem specifying how the dict should be constructed.
The ‘dict_merger’ functions indicates in the case of a collision (duplicate keys), which entry should be kept. E.g. lambda old, new: new
In case of str: Concatenates the elements of the Query, separated by the specified ‘str_delimiter’
- to_dict(collector=None, merger=None)[source]
Returns a dict of the elements of the current query.
The ‘collector’ function receives an element from the query and returns a (key, value) pair or a DictItem specifying how the dict should be constructed.
The ‘merger’ functions indicates in the case of a collision (duplicate keys), which entry should be kept. E.g. lambda old, new: new
- to_string(delimiter=', ')[source]
Concatenates the elements of the Query, separated by the specified delimiter
- group_by(classifier=None, collector=None)[source]
Performs a “group by” operation on the elements of the query according to a classification function. Returns the results in a dict built using collector function (optionally provided by the user or via a default one)
- quantify(predicate=bool)[source]
Count how many of the elements are Truthy or evaluate to True based on a given predicate