corpy.util

Small utility functions.

corpy.util.head(collection, first_n=None)

Inspect collection, truncated if too long.

If first_n=None, an appropriate value is determined based on the type of the collection.

corpy.util.cmp(lhs, rhs, test='__eq__')

Wrap assert statement to automatically raise an informative error.

corpy.util.clean_env(*, blacklist=None, whitelist=None, keep_dunder=True, keep_callables=True, keep_sunder=False)

Run a block of code in a sanitized global environment.

This is useful e.g. for testing answers in student assignments, because it will ensure that functions which accidentally capture global variables instead of using arguments fail.

By default, global variables starting with a double underscore and callables are kept. A whitelist of additional variables to keep can also be provided. The environment is restored afterwards.