Type testing¶
- isa(*types)¶
Returns function checking if its argument is of any of given
types
.Split labels from ids:
labels, ids = lsplit(isa(str), values)
- is_mapping(value)¶
- is_set(value)¶
- is_list(value)¶
- is_tuple(value)¶
- is_seq(value)¶
- is_iter(value)¶
These functions check if value is
Mapping
,Set
,list
,tuple
,Sequence
or iterator respectively.
- is_seqcoll(value)¶
Checks if
value
is a list or a tuple, which are both sequences and collections.
- is_seqcont(value)¶
Checks if
value
is a list, a tuple or an iterator, which are sequential containers. It can be used to distinguish between value and multiple values in dual-interface functions:def add_to_selection(view, region): if is_seqcont(region): # A sequence of regions view.sel().add_all(region) else: view.sel().add(region)
- iterable(value)¶
Tests if
value
is iterable.
count(start=0, step=1)
Makes infinite iterator of values:
Makes infinite iterator of values:
start, start + step, start + 2*step, ...
cycle(seq)
Cycles passed sequence indefinitely
yielding its elements one by one.
Cycles passed sequence indefinitely
yielding its elements one by one.
repeat(item[, n])
Makes an iterator yielding item for n times
or indefinitely if n is omitted.
Makes an iterator yielding item for n times
or indefinitely if n is omitted.
repeatedly(f[, n])
Takes a function of no args, presumably with side effects,
and returns an infinite (or length n) iterator of calls to it.
Takes a function of no args, presumably with side effects,
and returns an infinite (or length n) iterator of calls to it.
iterate(f, x)
Returns an infinite iterator of
Returns an infinite iterator of
x, f(x), f(f(x)), ...
re_all(regex, s, flags=0)
Lists all matches of regex in s.
Lists all matches of regex in s.
re_iter(regex, s, flags=0)
Iterates over matches of regex in s.
Iterates over matches of regex in s.
first(seq)
Returns the first item in the sequence.
Returns
Returns the first item in the sequence.
Returns
None
if the sequence is empty.
second(seq)
Returns second item in the sequence.
Returns
Returns second item in the sequence.
Returns
None
if there are less than two items in it.
last(seq)
Returns the last item in the sequence.
Returns
Returns the last item in the sequence.
Returns
None
if the sequence is empty.
nth(n, seq)
Returns nth item in the sequence
or
Returns nth item in the sequence
or
None
if no such item exists.
some([pred, ]seq)
Finds first item in seq passing pred
or first that is true if pred is omitted.
Finds first item in seq passing pred
or first that is true if pred is omitted.
take(n, seq)
Returns a list of first n items in the sequence,
or all items if there are fewer than n.
Returns a list of first n items in the sequence,
or all items if there are fewer than n.
drop(n, seq)
Skips first n items in the sequence,
yields the rest.
Skips first n items in the sequence,
yields the rest.
rest(seq)
Skips first item in the sequence, yields the rest.
Skips first item in the sequence, yields the rest.
butlast(seq)
Yields all elements of the sequence but last.
Yields all elements of the sequence but last.
takewhile([pred, ]seq)
Yields seq items as long as they pass pred.
Yields seq items as long as they pass pred.
dropwhile([pred, ]seq)
Skips elements of seq while pred passes
and then yields the rest.
Skips elements of seq while pred passes
and then yields the rest.
split_at(n, seq)
lsplit_at(n, seq)
Splits the sequence at given position,
returning a tuple of its start and tail.
lsplit_at(n, seq)
Splits the sequence at given position,
returning a tuple of its start and tail.
split_by(pred, seq)
lsplit_by(pred, seq)
Splits the start of the sequence,
consisting of items passing pred,
from the rest of it.
lsplit_by(pred, seq)
Splits the start of the sequence,
consisting of items passing pred,
from the rest of it.
map(f, *seqs)
lmap(f, *seqs)
Extended versions of map() and list(map())
lmap(f, *seqs)
Extended versions of map() and list(map())
mapcat(f, *seqs)
lmapcat(f, *seqs)
Maps given sequence(s) and concatenates the results.
lmapcat(f, *seqs)
Maps given sequence(s) and concatenates the results.
keep([f, ]*seqs)
lkeep([f, ]*seqs)
Maps seq with f and filters out falsy results.
Simply removes falsy values in one argument version.
lkeep([f, ]*seqs)
Maps seq with f and filters out falsy results.
Simply removes falsy values in one argument version.
pluck(key, mappings)
lpluck(key, mappings)
Yields or lists values for key in each mapping.
lpluck(key, mappings)
Yields or lists values for key in each mapping.
pluck_attr(attr, objects)
lpluck_attr(attr, objects)
Yields or lists values of attr of each object.
lpluck_attr(attr, objects)
Yields or lists values of attr of each object.
invoke(objects, name, *args, **kwargs)
linvoke(objects, name, *args, **kwargs)
Yields or lists results of the given method call
for each object in objects.
linvoke(objects, name, *args, **kwargs)
Yields or lists results of the given method call
for each object in objects.
@wrap_prop(ctx)
Wrap a property accessors with ctx.
Wrap a property accessors with ctx.
filter(pred, seq)
lfilter(pred, seq)
Extended versions of filter() and list(filter()).
lfilter(pred, seq)
Extended versions of filter() and list(filter()).
remove(pred, seq)
lremove(pred, seq)
Removes items from seq passing given predicate.
lremove(pred, seq)
Removes items from seq passing given predicate.
distinct(seq, key=identity)
ldistinct(seq, key=identity)
Removes items having same key from seq.
Preserves order.
ldistinct(seq, key=identity)
Removes items having same key from seq.
Preserves order.
where(mappings, **cond)
lwhere(mappings, **cond)
Selects mappings containing all pairs in cond.
lwhere(mappings, **cond)
Selects mappings containing all pairs in cond.
without(seq, *items)
lwithout(seq, *items)
Returns sequence without items,
preserves order.
lwithout(seq, *items)
Returns sequence without items,
preserves order.
cat(seqs)
lcat(seqs)
Concatenates passed sequences.
lcat(seqs)
Concatenates passed sequences.
concat(*seqs)
lconcat(*seqs)
Concatenates several sequences.
lconcat(*seqs)
Concatenates several sequences.
flatten(seq, follow=is_seqcont)
lflatten(seq, follow=is_seqcont)
Flattens arbitrary nested sequence,
dives into when
lflatten(seq, follow=is_seqcont)
Flattens arbitrary nested sequence,
dives into when
follow(item)
is truthy.
interleave(*seqs)
Yields first item of each sequence,
then second one and so on.
Yields first item of each sequence,
then second one and so on.
interpose(sep, seq)
Yields items of seq separated by sep.
Yields items of seq separated by sep.
lzip(*seqs)
List version of zip()
List version of zip()
chunks(n, [step, ]seq)
lchunks(n, [step, ]seq)
Chunks seq into parts of length n or less.
Skips step items between chunks.
lchunks(n, [step, ]seq)
Chunks seq into parts of length n or less.
Skips step items between chunks.
partition(n, [step, ]seq)
lpartition(n, [step, ]seq)
Partitions seq into parts of length n.
Skips step items between parts.
Non-fitting tail is ignored.
lpartition(n, [step, ]seq)
Partitions seq into parts of length n.
Skips step items between parts.
Non-fitting tail is ignored.
partition_by(f, seq)
lpartition_by(f, seq)
Partition seq into continuous chunks
with constant value of f.
lpartition_by(f, seq)
Partition seq into continuous chunks
with constant value of f.
split(pred, seq)
lsplit(pred, seq)
Splits seq items which pass pred
from the ones that don't.
lsplit(pred, seq)
Splits seq items which pass pred
from the ones that don't.
count_by(f, seq)
Counts numbers of occurrences of values of f
on elements of seq.
Counts numbers of occurrences of values of f
on elements of seq.
count_reps(seq)
Counts repetitions of each value in seq.
Counts repetitions of each value in seq.
group_by(f, seq)
Groups items of seq by
Groups items of seq by
f(item)
.
group_by_keys(get_keys, seq)
Groups elements of seq by multiple keys.
Groups elements of seq by multiple keys.
group_values(seq)
Groups values of
Groups values of
(key, value)
pairs by keys.
ilen(seq)
Consumes the given iterator and returns its length.
Consumes the given iterator and returns its length.
reductions(f, seq[, acc])
lreductions(f, seq[, acc])
Constructs intermediate reductions of seq by f.
lreductions(f, seq[, acc])
Constructs intermediate reductions of seq by f.
sums(seq[, acc])
lsums(seq[, acc])
Returns a sequence of partial sums of seq.
lsums(seq[, acc])
Returns a sequence of partial sums of seq.
all([pred, ]seq)
Checks if all items in seq pass pred.
Checks if all items in seq pass pred.
any([pred, ]seq)
Checks if any item in seq passes pred.
Checks if any item in seq passes pred.
none([pred, ]seq)
Checks if none of the items in seq pass pred.
Checks if none of the items in seq pass pred.
one([pred, ]seq)
Checks if exactly one item in seq passes pred.
Checks if exactly one item in seq passes pred.
pairwise(seq)
Yields all pairs of neighboring items in seq.
Yields all pairs of neighboring items in seq.
with_prev(seq, fill=None)
Yields each item from seq with the one preceding it.
Yields each item from seq with the one preceding it.
with_next(seq, fill=None)
Yields each item from seq with the next one.
Yields each item from seq with the next one.
zip_values(*dicts)
Yields tuples of corresponding values of given dicts.
Yields tuples of corresponding values of given dicts.
zip_dicts(*dicts)
Yields tuples like
for each common key in all given dicts.
Yields tuples like
(key, val1, val2, ...)
for each common key in all given dicts.
tree_leaves(root, follow=is_seqcont, children=iter)
ltree_leaves(root, follow=is_seqcont, children=iter)
Lists or iterates over tree leaves.
ltree_leaves(root, follow=is_seqcont, children=iter)
Lists or iterates over tree leaves.
tree_nodes(root, follow=is_seqcont, children=iter)
ltree_nodes(root, follow=is_seqcont, children=iter)
Lists or iterates over tree nodes.
ltree_nodes(root, follow=is_seqcont, children=iter)
Lists or iterates over tree nodes.
merge(*colls)
Merges several collections of same type into one:
dicts, sets, lists, tuples, iterators or strings
For dicts later values take precedence.
Merges several collections of same type into one:
dicts, sets, lists, tuples, iterators or strings
For dicts later values take precedence.
merge_with(f, *dicts)
Merges several dicts combining values with given function.
Merges several dicts combining values with given function.
join(colls)
Joins several collections of same type into one.
Same as merge() but accepts sequence of collections.
Joins several collections of same type into one.
Same as merge() but accepts sequence of collections.
join_with(f, *dicts)
Joins several dicts combining values with given function.
Joins several dicts combining values with given function.
walk(f, coll)
Maps coll with f, but preserves collection type.
Maps coll with f, but preserves collection type.
walk_keys(f, coll)
Walks keys of coll, mapping them with f.
Works with dicts and collections of pairs.
Walks keys of coll, mapping them with f.
Works with dicts and collections of pairs.
walk_values(f, coll)
Walks values of coll, mapping them with f.
Works with dicts and collections of pairs.
Walks values of coll, mapping them with f.
Works with dicts and collections of pairs.
select(pred, coll)
Filters elements of coll by pred
constructing a collection of same type.
Filters elements of coll by pred
constructing a collection of same type.
select_keys(pred, coll)
Select part of coll with keys passing pred.
Works with dicts and collections of pairs.
Select part of coll with keys passing pred.
Works with dicts and collections of pairs.
select_values(pred, coll)
Select part of coll with values passing pred.
Works with dicts and collections of pairs.
Select part of coll with values passing pred.
Works with dicts and collections of pairs.
compact(coll)
Removes falsy values from given collection.
Removes falsy values from given collection.
All collections functions work with dicts.
These are targeted specifically at them.
These are targeted specifically at them.
flip(mapping)
Flip passed dict swapping its keys and values.
Flip passed dict swapping its keys and values.
zipdict(keys, vals)
Creates a dict with keys mapped to the corresponding vals.
Creates a dict with keys mapped to the corresponding vals.
itervalues(coll)
Yields values of the given collection.
Yields values of the given collection.
iteritems(coll)
Yields
Yields
(key, value)
pairs of the given collection.
project(mapping, keys)
Leaves only given keys in mapping.
Leaves only given keys in mapping.
omit(mapping, keys)
Removes given keys from mapping.
Removes given keys from mapping.
empty(coll)
Returns an empty collection of the same type as coll.
Returns an empty collection of the same type as coll.
get_in(coll, path, default=None)
Returns a value at path in the given nested collection.
Returns a value at path in the given nested collection.
get_lax(coll, path, default=None)
Returns a value at path in the given nested collection.
Ignores
Returns a value at path in the given nested collection.
Ignores
TypeError
s.
set_in(coll, path, value)
Creates a copy of coll with the value set at path.
Creates a copy of coll with the value set at path.
update_in(coll, path, update, default=None)
Creates a copy of coll with a value updated at path.
Creates a copy of coll with a value updated at path.
del_in(coll, path)
Creates a copy of coll with path removed.
Creates a copy of coll with path removed.
has_path(coll, path)
Tests whether path exists in a nested coll.
Tests whether path exists in a nested coll.
Most of functions in this section support extended semantics.
identity(x)
Returns its argument.
Returns its argument.
constantly(x)
Creates a function accepting any args, but always returning x.
Creates a function accepting any args, but always returning x.
func_partial(func, *args, **kwargs)
Like partial() but returns a real function.
Like partial() but returns a real function.
partial(func, *args, **kwargs)
Returns partial application of func.
Returns partial application of func.
rpartial(func, *args)
Partially applies last arguments to func.
Partially applies last arguments to func.
iffy([pred, ]action[, default=identity])
Creates a function, which conditionally applies action or default.
Creates a function, which conditionally applies action or default.
caller(*args, **kwargs)
Creates a function calling its argument with passed arguments.
Creates a function calling its argument with passed arguments.
re_finder(regex, flags=0)
Creates a function finding regex in passed string.
Creates a function finding regex in passed string.
re_tester(regex, flags=0)
Creates a predicate testing passed strings with regex.
Creates a predicate testing passed strings with regex.
complement(pred)
Constructs a complementary predicate.
Constructs a complementary predicate.
autocurry(func)
Creates a version of func returning its partial applications
until sufficient arguments are passed.
Creates a version of func returning its partial applications
until sufficient arguments are passed.
curry(func[, n])
Curries func into a chain of one argument functions.
Arguments are passed from left to right.
Curries func into a chain of one argument functions.
Arguments are passed from left to right.
rcurry(func[, n])
Curries func from right to left.
Curries func from right to left.
compose(*fs)
Composes passed functions.
Composes passed functions.
rcompose(*fs)
Composes fs, calling them from left to right.
Composes fs, calling them from left to right.
juxt(*fs)
ljuxt(*fs)
Constructs a juxtaposition of the given functions.
Result returns a list or an iterator of results of fs.
ljuxt(*fs)
Constructs a juxtaposition of the given functions.
Result returns a list or an iterator of results of fs.
all_fn(*fs)
Constructs a predicate,
which holds when all fs hold.
Constructs a predicate,
which holds when all fs hold.
any_fn(*fs)
Constructs a predicate,
which holds when any of fs holds.
Constructs a predicate,
which holds when any of fs holds.
none_fn(*fs)
Constructs a predicate,
which holds when none of fs hold.
Constructs a predicate,
which holds when none of fs hold.
one_fn(*fs)
Constructs a predicate,
which holds when exactly one of fs holds.
Constructs a predicate,
which holds when exactly one of fs holds.
some_fn(*fs)
Constructs a function, which calls fs one by one
and returns first truthy result.
Constructs a function, which calls fs one by one
and returns first truthy result.
is_distinct(*fs)
Checks if all elements in the collection are different.
Checks if all elements in the collection are different.
isa(*types)
Creates a function checking if its argument
is of any of given types.
Creates a function checking if its argument
is of any of given types.
is_iter(value)
Checks whether value is an iterator.
Checks whether value is an iterator.
is_mapping(value)
Checks whether value is a mapping.
Checks whether value is a mapping.
is_set(value)
Checks whether value is a set.
Checks whether value is a set.
is_list(value)
Checks whether value is a list.
Checks whether value is a list.
is_tuple(value)
Checks whether value is a tuple.
Checks whether value is a tuple.
is_seq(value)
Checks whether value is a
Checks whether value is a
Sequence
.
is_mapping(value)
Checks whether value is a mapping.
Checks whether value is a mapping.
is_seqcoll(value)
Checks whether value is a list or a tuple,
which are both sequences and collections.
Checks whether value is a list or a tuple,
which are both sequences and collections.
is_seqcont(value)
Checks whether value is a list, a tuple or an iterator,
which are both sequences and containers.
Checks whether value is a list, a tuple or an iterator,
which are both sequences and containers.
iterable(value)
Checks whether value is iterable.
Checks whether value is iterable.
@decorator
Transforms a flat wrapper into a decorator.
Transforms a flat wrapper into a decorator.
@wraps
An utility to pass function metadata
from wrapped function to a wrapper.
An utility to pass function metadata
from wrapped function to a wrapper.
unwrap(func)
Get the object wrapped by func.
Get the object wrapped by func.
@once
Let function execute only once.
Noop all subsequent calls.
Let function execute only once.
Noop all subsequent calls.
@once_per(*argnames)
Call function only once for every combination
of the given arguments.
Call function only once for every combination
of the given arguments.
@once_per_args
Call function only once for every combination
of values of its arguments.
Call function only once for every combination
of values of its arguments.
@collecting
Transforms a generator into list returning function.
Transforms a generator into list returning function.
@joining(sep)
Joins decorated function results with sep.
Joins decorated function results with sep.
@post_processing(func)
Post processes decorated function result with func.
Post processes decorated function result with func.
@throttle(period)
Only runs a decorated function once per period.
Only runs a decorated function once per period.
@wrap_with(ctx)
Turn context manager into a decorator.
Turn context manager into a decorator.
nullcontext(enter_result=None)
A noop context manager.
A noop context manager.
@retry(tries, errors=Exception, timeout=0, filter_errors=None)
Tries decorated function up to tries times.
Retries only on specified errors.
Tries decorated function up to tries times.
Retries only on specified errors.
@silent
Alters function to ignore all exceptions.
Alters function to ignore all exceptions.
@ignore(errors, default=None)
Alters function to ignore errors,
returning default instead.
Alters function to ignore errors,
returning default instead.
suppress(*errors)
The context manager suppressing errors in its block.
The context manager suppressing errors in its block.
@limit_error_rate(fails, timeout, ...)
If function fails to complete fails times in a row,
calls to it will be blocked for timeout seconds.
If function fails to complete fails times in a row,
calls to it will be blocked for timeout seconds.
fallback(*approaches)
Tries several approaches until one works.
Each approach has a form of
Tries several approaches until one works.
Each approach has a form of
(callable, errors)
.
raiser(exception=Exception, *args, **kwargs)
Constructs function that raises the given exception
with given arguments on any invocation.
Constructs function that raises the given exception
with given arguments on any invocation.
@reraise(errors, into)
Intercepts errors and reraises them as into exception.
Intercepts errors and reraises them as into exception.
tap(x, label=None)
Prints x and then returns it.
Prints x and then returns it.
@log_calls(print_func, errors=True, stack=True)
@print_calls(errors=True, stack=True)
Logs or prints all function calls,
including arguments, results and raised exceptions.
@print_calls(errors=True, stack=True)
Logs or prints all function calls,
including arguments, results and raised exceptions.
@log_enters(print_func)
@print_enters
Logs or prints on each enter to a function.
@print_enters
Logs or prints on each enter to a function.
@log_exits(print_func, errors=True, stack=True)
@print_exits(errors=True, stack=True)
Logs or prints on each exit from a function.
@print_exits(errors=True, stack=True)
Logs or prints on each exit from a function.
@log_errors(print_func, label=None, stack=True)
@print_errors(label=None, stack=True)
Logs or prints all errors within a function or block.
@print_errors(label=None, stack=True)
Logs or prints all errors within a function or block.
@log_durations(print_func, label=None)
@print_durations(label=None)
Times each function call or block execution.
@print_durations(label=None)
Times each function call or block execution.
log_iter_durations(seq, print_func, label=None)
print_iter_durations(seq, label=None)
Times processing of each item in seq.
print_iter_durations(seq, label=None)
Times processing of each item in seq.
@memoize(*, key_func=None)
Memoizes a decorated function results.
Memoizes a decorated function results.
@cache(timeout, *, key_func=None)
Caches a function results for timeout seconds.
Caches a function results for timeout seconds.
@cached_property
Creates a property caching its result.
Creates a property caching its result.
@cached_readonly
Creates a read-only property caching its result.
Creates a read-only property caching its result.
@make_lookuper
Creates a cached function with prefilled memory.
Creates a cached function with prefilled memory.
@silent_lookuper
Creates a cached function with prefilled memory.
Ignores memory misses, returning
Creates a cached function with prefilled memory.
Ignores memory misses, returning
None
.
re_find(regex, s, flags=0)
Matches regex against the given string,
returns the match in the simplest possible form.
Matches regex against the given string,
returns the match in the simplest possible form.
re_test(regex, s, flags=0)
Tests whether regex matches against s.
Tests whether regex matches against s.
cut_prefix(s, prefix)
Cuts prefix from given string if it's present.
Cuts prefix from given string if it's present.
cut_suffix(s, suffix)
Cuts suffix from given string if it's present.
Cuts suffix from given string if it's present.
str_join([sep="", ]seq)
Joins the given sequence with sep.
Forces stringification of seq items.
Joins the given sequence with sep.
Forces stringification of seq items.
@monkey(cls_or_module, name=None)
Monkey-patches class or module.
Monkey-patches class or module.
class LazyObject(init)
Creates an object setting itself up on first use.
Creates an object setting itself up on first use.
isnone(x)
Checks if x is None.
Checks if x is None.
notnone(x)
Checks if x is not None.
Checks if x is not None.
inc(x)
Increments its argument by 1.
Increments its argument by 1.
dec(x)
Decrements its argument by 1.
Decrements its argument by 1.
even(x)
Checks if x is even.
Checks if x is even.
odd(x)
Checks if x is odd.
Checks if x is odd.