fumus.queries.query

Classes

Query

Abstraction over a sequence of elements supporting sequential aggregate operations

Module Contents

class fumus.queries.query.Query(iterable)[source]

Bases: fumus.queries.itertools_mixin.ItertoolsMixin

Abstraction over a sequence of elements supporting sequential aggregate operations

_iterable
_is_consumed = False
_on_close_handler = None
__iter__()[source]
classmethod of(*iterable)[source]

Creates Query from args

classmethod of_nullable(iterable)[source]

Creates Query from args if iterable is not None; otherwise returns empty Query

classmethod empty()[source]

Creates empty Query

classmethod iterate(seed, operation, condition=None)[source]

Creates infinite ordered Query

classmethod generate(supplier)[source]

Creates infinite unordered Query with values generated by given supplier function

classmethod constant(element)[source]

Creates infinite Query with given value

classmethod from_range(*range_list: int)[source]

Creates Query from start (inclusive) to stop (exclusive) by an incremental step

classmethod _(range_obj: range)[source]

Creates Query range object

property iterable
concat(*queries)[source]

Concatenates several queries together or adds new queries/collections to the current one

prepend(iterable)[source]

Prepends iterable to current query

filter(predicate)[source]

Filters values in query based on given predicate function

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

flatten()[source]

Converts a Query of multidimensional collection into a one-dimensional

peek(operation)[source]

Performs the provided operation on each element of the query without consuming it

distinct()[source]

Returns a query with the distinct elements of the current one

count()[source]

Returns the count of elements in the query

sum()[source]

Sums the elements of the query

average()[source]

Returns the average value of elements in the query

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

head(count)[source]

Alias for ‘limit’

tail(count)[source]

Returns a query with the last n elements, or fewer if the underlying iterator ends sooner

take_while(predicate)[source]

Returns a query that yields elements based on a predicate

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

any_match(predicate)[source]

Returns whether any elements of the query match the given predicate

all_match(predicate)[source]

Returns whether all elements of the query match the given predicate

none_match(predicate)[source]

Returns whether no elements of the query match the given predicate

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

for_each(operation)[source]

Performs an action for each element of this query

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_list()[source]

Returns a list of the elements of the current query

to_tuple()[source]

Returns a tuple of the elements of the current query

to_set()[source]

Returns a set of the elements of the current query

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

_unpack_dict_item(item)[source]
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)

_group_by(classifier=None)[source]
quantify(predicate=bool)[source]

Count how many of the elements are Truthy or evaluate to True based on a given predicate

close()[source]

Closes the query, causing the provided close handler to be called

on_close(handler)[source]

Returns an equivalent query with an additional close handler

__repr__()[source]
_join(delimiter=', ')[source]
take_nth(idx, default=None)[source]

Returns Optional with the nth element of the query or a default value

all_equal(key=None)[source]

Returns True if all elements of the query are equal to each other