5.1. spy

This module exposes spy’s core API.

See also

spy.decorators
Function decorators for use with spy fragments

5.1.1. Constants

spy.DROP

A signaling object: when returned from a fragment, the fragment will not yield any value.

5.1.2. Exceptions

exception spy.CaughtException[source]
print_traceback()[source]

Print the (formatted) traceback captured when the exception was raised.

5.1.3. Functions

class spy.catch

A context manager. Exceptions raised in the context will be subject to spy’s traceback formatting and wrapped in a CaughtException. If these are not caught, spy uses an exception hook to force them to be formatted properly. If you opt to catch CaughtException instead, you can use its print_traceback() method to print the formatted traceback without exiting.

class spy.chain(seq)[source]

Construct a chain of fragments from seq.

Parameters:seq (sequence) – Fragments to chain together
__call__(data)

Alias for apply().

apply(data)[source]

Feed data into the fragment chain, and return an iterator over the resulting data.

classmethod auto_fragments(seq)[source]

Like the regular constructor, but for each element in seq, apply fragment() to it if it isn’t already a fragment.

Items in seq must be either regular functions (not generators) or fragments.

run_to_exhaustion(data)[source]

Call apply(), then iterate until the chain runs out of data.

spy.collect(context)[source]

Return an iterator of the elements being processed by the current fragment. Can be used to write a fragment that consumes multiple items.

@spy.fragment[source]

Given a callable func, return a fragment that calls func to process data. func must take at least one positional argument, a single value to process and return.

Optionally it can take another argument, called context. If it does, a context object will be passed to it on each invocation. This object has no documented public functionality; its purpose is to be passed to spy API functions that require it (namely collect()).

spy.many(ita)[source]

Return a signaling object that instructs spy to yield values from ita from the current fragment, instead of yielding only one value.