Flow¶
- @silent¶
Ignore all real exceptions (descendants of
Exception
). Handy for cleaning data such as user input:brand_id = silent(int)(request.GET['brand_id']) ids = keep(silent(int), request.GET.getlist('id'))
And in data import/transform:
get_greeting = compose(silent(string.lower), re_finder(r'(\w+)!')) map(get_greeting, ['a!', ' B!', 'c.']) # -> ['a', 'b', None]
Note
Avoid silencing non-primitive functions, use
@ignore()
instead and even then be careful not to swallow exceptions unintentionally.
- @ignore(errors, default=None)¶
Same as
@silent
, but able to specifyerrors
to catch anddefault
to return in case of error caught.errors
can either be exception class or a tuple of them.
- suppress(*errors)¶
A context manager which suppresses given exceptions under its scope:
with suppress(HttpError): # Assume this request can fail, and we are ok with it make_http_request()
- nullcontext(enter_result=None)¶
A noop context manager that returns
enter_result
from__enter__
:ctx = nullcontext() if threads: ctx = op_thread_lock with ctx: # ... do stuff
- @once¶
- @once_per_args¶
- @once_per(*argnames)¶
Call function only once, once for every combination of values of its arguments or once for every combination of given arguments. Thread safe. Handy for various initialization purposes:
# Global initialization @once def initialize_cache(): conn = some.Connection(...) # ... set up everything # Per argument initialization @once_per_args def initialize_language(lang): conf = load_language_conf(lang) # ... set up language # Setup each class once class SomeManager(Manager): @once_per('cls') def _initialize_class(self, cls): pre_save.connect(self._pre_save, sender=cls) # ... set up signals, no dups
- raiser(exception_or_class=Exception, *args, **kwargs)¶
Constructs function that raises given exception with given arguments on any invocation. You may pass a string instead of exception as a shortcut:
mocker.patch('mod.Class.propname', property(raiser("Shouldn't be called")))
This will raise an
Exception
with a corresponding message.
- @reraise(errors, into)¶
Intercepts any error of
errors
classes and reraises it asinto
error. Can be used as decorator or a context manager:with reraise(json.JSONDecodeError, SuspiciousOperation('Invalid JSON')): return json.loads(text)
into
can also be a callable to transform the error before reraising:@reraise(requests.RequestsError, lambda e: MyAPIError(error_desc(e))) def api_call(...): # ...
- @retry(tries, errors=Exception, timeout=0, filter_errors=None)¶
Every call of the decorated function is tried up to
tries
times. The first attempt counts as a try. Retries occur when any subclass oferrors
is raised, where``errors`` is an exception class or a list/tuple of exception classes. There will be a delay intimeout
seconds between tries.A common use is to wrap some unreliable action:
@retry(3, errors=HttpError) def download_image(url): # ... make http request return image
Errors to retry may addtionally be filtered with
filter_errors
when classes are not specific enough:@retry(3, errors=HttpError, filter_errors=lambda e: e.status_code >= 500) def download_image(url): # ...
You can pass a callable as
timeout
to achieve exponential delays or other complex behavior:@retry(3, errors=HttpError, timeout=lambda a: 2 ** a) def download_image(url): # ... make http request return image
- fallback(*approaches)¶
Tries several approaches until one works. Each approach is either callable or a tuple
(callable, errors)
, where errors is an exception class or a tuple of classes, which signal to fall back to next approach. Iferrors
is not supplied then fall back is done for anyException
:fallback( (partial(send_mail, ADMIN_EMAIL, message), SMTPException), partial(log.error, message), # Handle any Exception (raiser(FeedbackError, "Failed"), ()) # Handle nothing )
- limit_error_rate(fails, timeout, exception=ErrorRateExceeded)¶
If function fails to complete
fails
times in a row, calls to it will be intercepted fortimeout
withexception
raised instead. A clean way to short-circuit function taking too long to fail:@limit_error_rate(fails=5, timeout=60, exception=RequestError('Temporary unavailable')) def do_request(query): # ... make a http request return data
Can be combined with
ignore()
to silently stop trying for a while:@ignore(ErrorRateExceeded, default={'id': None, 'name': 'Unknown'}) @limit_error_rate(fails=5, timeout=60) def get_user(id): # ... make a http request return data
- throttle(period)¶
Only runs a decorated function once in a
period
:@throttle(60) def process_beat(pk, progress): Model.objects.filter(pk=pk).update(beat=timezone.now(), progress=progress) # Processing something, update progress info no more often then once a minute for i in ...: process_beat(pk, i / n) # ... do actual processing
- @collecting¶
Transforms generator or other iterator returning function into list returning one.
Handy to prevent quirky iterator-returning properties:
@property @collecting def path_up(self): node = self while node: yield node node = node.parent
Also makes list constructing functions beautifully yielding.
- @joining(sep)¶
Wraps common python idiom “collect then join” into a decorator. Transforms generator or alike into function, returning string of joined results. Automatically converts all elements to separator type for convenience.
Goes well with generators with some ad-hoc logic within:
@joining(', ') def car_desc(self): yield self.year_made if self.engine_volume: yield '%s cc' % self.engine_volume if self.transmission: yield self.get_transmission_display() if self.gear: yield self.get_gear_display() # ...
Use
bytes
separator to get bytes result:@joining(b' ') def car_desc(self): yield self.year_made # ...
See also
str_join()
.
- @post_processing(func)¶
Passes decorated function result through
func
. This is the generalization of@collecting
and@joining()
. Could save you writing a decorator or serve as an extended comprehension:@post_processing(dict) def make_cond(request): if request.GET['new']: yield 'year__gt', 2000 for key, value in request.GET.items(): if value == '': continue # ...
- @wrap_with(ctx)¶
Turns a context manager into a decorator:
@wrap_with(threading.Lock()) def protected_func(...): # ...
Makes infinite iterator of values:
start, start + step, start + 2*step, ...
Cycles passed sequence indefinitely
yielding its elements one by one.
Makes an iterator yielding item for n times
or indefinitely if n is omitted.
Takes a function of no args, presumably with side effects,
and returns an infinite (or length n) iterator of calls to it.
Returns an infinite iterator of
x, f(x), f(f(x)), ...
Lists all matches of regex in s.
Iterates over matches of regex in s.
Returns the first item in the sequence.
Returns
None
if the sequence is empty.
Returns second item in the sequence.
Returns
None
if there are less than two items in it.
Returns the last item in the sequence.
Returns
None
if the sequence is empty.
Returns nth item in the sequence
or
None
if no such item exists.
Finds first item in seq passing pred
or first that is true if pred is omitted.
Returns a list of first n items in the sequence,
or all items if there are fewer than n.
Skips first n items in the sequence,
yields the rest.
Skips first item in the sequence, yields the rest.
Yields all elements of the sequence but last.
Yields seq items as long as they pass pred.
Skips elements of seq while pred passes
and then yields the rest.
lsplit_at(n, seq)
Splits the sequence at given position,
returning a tuple of its start and tail.
lsplit_by(pred, seq)
Splits the start of the sequence,
consisting of items passing pred,
from the rest of it.
lmap(f, *seqs)
Extended versions of map() and list(map())
lmapcat(f, *seqs)
Maps given sequence(s) and concatenates the results.
lkeep([f, ]*seqs)
Maps seq with f and filters out falsy results.
Simply removes falsy values in one argument version.
lpluck(key, mappings)
Yields or lists values for key in each mapping.
lpluck_attr(attr, objects)
Yields or lists values of attr of each object.
linvoke(objects, name, *args, **kwargs)
Yields or lists results of the given method call
for each object in objects.
Wrap a property accessors with ctx.
lfilter(pred, seq)
Extended versions of filter() and list(filter()).
lremove(pred, seq)
Removes items from seq passing given predicate.
ldistinct(seq, key=identity)
Removes items having same key from seq.
Preserves order.
lwhere(mappings, **cond)
Selects mappings containing all pairs in cond.
lwithout(seq, *items)
Returns sequence without items,
preserves order.
lcat(seqs)
Concatenates passed sequences.
lconcat(*seqs)
Concatenates several sequences.
lflatten(seq, follow=is_seqcont)
Flattens arbitrary nested sequence,
dives into when
follow(item)
is truthy.
Yields first item of each sequence,
then second one and so on.
Yields items of seq separated by sep.
List version of zip()
lchunks(n, [step, ]seq)
Chunks seq into parts of length n or less.
Skips step items between chunks.
lpartition(n, [step, ]seq)
Partitions seq into parts of length n.
Skips step items between parts.
Non-fitting tail is ignored.
lpartition_by(f, seq)
Partition seq into continuous chunks
with constant value of f.
lsplit(pred, seq)
Splits seq items which pass pred
from the ones that don't.
Counts numbers of occurrences of values of f
on elements of seq.
Counts repetitions of each value in seq.
Groups items of seq by
f(item)
.
Groups elements of seq by multiple keys.
Groups values of
(key, value)
pairs by keys.
Consumes the given iterator and returns its length.
lreductions(f, seq[, acc])
Constructs intermediate reductions of seq by f.
lsums(seq[, acc])
Returns a sequence of partial sums of seq.
Checks if all items in seq pass pred.
Checks if any item in seq passes pred.
Checks if none of the items in seq pass pred.
Checks if exactly one item in seq passes pred.
Yields all pairs of neighboring items in seq.
Yields each item from seq with the one preceding it.
Yields each item from seq with the next one.
Yields tuples of corresponding values of given dicts.
Yields tuples like
(key, val1, val2, ...)
for each common key in all given dicts.
ltree_leaves(root, follow=is_seqcont, children=iter)
Lists or iterates over tree leaves.
ltree_nodes(root, follow=is_seqcont, children=iter)
Lists or iterates over tree nodes.
Merges several collections of same type into one:
dicts, sets, lists, tuples, iterators or strings
For dicts later values take precedence.
Merges several dicts combining values with given function.
Joins several collections of same type into one.
Same as merge() but accepts sequence of collections.
Joins several dicts combining values with given function.
Maps coll with f, but preserves collection type.
Walks keys 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.
Filters elements of coll by pred
constructing a collection of same type.
Select part of coll with keys passing pred.
Works with dicts and collections of pairs.
Select part of coll with values passing pred.
Works with dicts and collections of pairs.
Removes falsy values from given collection.
These are targeted specifically at them.
Flip passed dict swapping its keys and values.
Creates a dict with keys mapped to the corresponding vals.
Yields values of the given collection.
Yields
(key, value)
pairs of the given collection.
Leaves only given keys in mapping.
Removes given keys from mapping.
Returns an empty collection of the same type as coll.
Returns a value at path in the given nested collection.
Returns a value at path in the given nested collection.
Ignores
TypeError
s.
Creates a copy of coll with the value set at path.
Creates a copy of coll with a value updated at path.
Creates a copy of coll with path removed.
Tests whether path exists in a nested coll.
Returns its argument.
Creates a function accepting any args, but always returning x.
Like partial() but returns a real function.
Returns partial application of func.
Partially applies last arguments to func.
Creates a function, which conditionally applies action or default.
Creates a function calling its argument with passed arguments.
Creates a function finding regex in passed string.
Creates a predicate testing passed strings with regex.
Constructs a complementary predicate.
Creates a version of func returning its partial applications
until sufficient arguments are passed.
Curries func into a chain of one argument functions.
Arguments are passed from left to right.
Curries func from right to left.
Composes passed functions.
Composes fs, calling them from left to right.
ljuxt(*fs)
Constructs a juxtaposition of the given functions.
Result returns a list or an iterator of results of fs.
Constructs a predicate,
which holds when all fs hold.
Constructs a predicate,
which holds when any of fs holds.
Constructs a predicate,
which holds when none of fs hold.
Constructs a predicate,
which holds when exactly one of fs holds.
Constructs a function, which calls fs one by one
and returns first truthy result.
Checks if all elements in the collection are different.
Creates a function checking if its argument
is of any of given types.
Checks whether value is an iterator.
Checks whether value is a mapping.
Checks whether value is a set.
Checks whether value is a list.
Checks whether value is a tuple.
Checks whether value is a
Sequence
.
Checks whether value is a mapping.
Checks whether value is a list or a tuple,
which are both sequences and collections.
Checks whether value is a list, a tuple or an iterator,
which are both sequences and containers.
Checks whether value is iterable.
Transforms a flat wrapper into a decorator.
An utility to pass function metadata
from wrapped function to a wrapper.
Get the object wrapped by func.
Let function execute only once.
Noop all subsequent calls.
Call function only once for every combination
of the given arguments.
Call function only once for every combination
of values of its arguments.
Transforms a generator into list returning function.
Joins decorated function results with sep.
Post processes decorated function result with func.
Only runs a decorated function once per period.
Turn context manager into a decorator.
A noop context manager.
Tries decorated function up to tries times.
Retries only on specified errors.
Alters function to ignore all exceptions.
Alters function to ignore errors,
returning default instead.
The context manager suppressing errors in its block.
If function fails to complete fails times in a row,
calls to it will be blocked for timeout seconds.
Tries several approaches until one works.
Each approach has a form of
(callable, errors)
.
Constructs function that raises the given exception
with given arguments on any invocation.
Intercepts errors and reraises them as into exception.
Prints x and then returns it.
@print_calls(errors=True, stack=True)
Logs or prints all function calls,
including arguments, results and raised exceptions.
@print_enters
Logs or prints on each enter to a function.
@print_exits(errors=True, stack=True)
Logs or prints on each exit from a function.
@print_errors(label=None, stack=True)
Logs or prints all errors within a function or block.
@print_durations(label=None)
Times each function call or block execution.
print_iter_durations(seq, label=None)
Times processing of each item in seq.
Memoizes a decorated function results.
Caches a function results for timeout seconds.
Creates a property caching its result.
Creates a read-only property caching its result.
Creates a cached function with prefilled memory.
Creates a cached function with prefilled memory.
Ignores memory misses, returning
None
.
Matches regex against the given string,
returns the match in the simplest possible form.
Tests whether regex matches against s.
Cuts prefix from given string if it's present.
Cuts suffix from given string if it's present.
Joins the given sequence with sep.
Forces stringification of seq items.
Monkey-patches class or module.
Creates an object setting itself up on first use.
Checks if x is None.
Checks if x is not None.
Increments its argument by 1.
Decrements its argument by 1.
Checks if x is even.
Checks if x is odd.