lisa.utils#

Miscellaneous utilities that don’t fit anywhere else.

Also used as a home for everything that would create cyclic dependency issues between modules if they were hosted in their “logical” module. This is mostly done for secondary utilities that are not used often.

Globals

LISA_CACHE_HOME

Base folder used for caching files.

LISA_HOME

The detected location of your LISA installation.

LISA_HOST_ABI

ABI of the machine that imported that module.

TASK_COMM_MAX_LEN

Value of TASK_COMM_LEN - 1 macro in the kernel, to account for \0 terminator.

Classes

ArtifactPath

Path to a folder that can be used to store artifacts of a function. This must be a clean folder, already created on disk.

ComposedContextManager

Compose context managers together.

DirCache

Provide a folder-based cache.

ExekallTaggable

Allows tagging the objects produced in exekall expressions ID.

FrozenDict

Read-only mapping that is therefore hashable.

HideExekallID

Hide the subclasses in the simplified ID format of exekall.

LazyMapping

Lazy Mapping dict-like class for elements evaluated on the fly.

Loggable

A simple class for uniformly named loggers.

PartialInit

Allow partial initialization of instances with curry-like behaviour for the constructor.

Serializable

A helper class for YAML serialization/deserialization.

SerializeViaConstructor

Base class providing serialization to objects that typically cannot due to unpicklable attributes.

SimpleHash

Base class providing a basic implementation of __eq__ and __hash__: two instances are equal if their __dict__ and __class__ attributes are equal.

UnboundMethodType

Dummy class to be used to check if a function is a method defined in a class or not::.

UnknownTagPlaceholder

bothmethod

Decorator to allow a method to be used both as an instance method and a classmethod.

instancemethod

Decorator providing a hybrid of a normal method and a classmethod:.

mappable

Decorator that allows the decorated function to be mapped on an iterable::.

Functions

DelegateToAttr()

Implement delegation of attribute lookup to attribute named attr on instances of the classes specified by attr_classes.

add()

Same as sum() but works on any object that defines __add__.

annotations_from_signature()

Build a PEP484 __annotations__ dictionary from a inspect.Signature.

chain_cm()

Chain the context managers returned by the given callables.

checksum()

Compute a checksum on a given file-like object.

compose()

Compose multiple functions such that compose(f, g)(x) == g(f(x)).

consume()

Advance the iterator n-steps ahead. If n is None, consume entirely.

curry()

Currify the given function such that f(x, y) == curry(f)(x)(y).

deduplicate()

Deduplicate items in the given sequence and return a list. :param seq: Sequence to deduplicate :type Seq: collections.abc.Sequence.

delegate_getattr()

Somewhat equivalent to x.<delegate_to>.<attr>.

deprecate()

Mark a class, method, function etc as deprecated and update its docstring.

destroyablecontextmanager()

Similar to contextlib.contextmanager() but treats all cases of yield as an exception.

dispatch_kwargs()

Dispatch the provided kwargs mapping to the funcs functions, based on their signature.

docstring_update()

Create a class to inherit from in order to add a snippet of doc at the end of the docstring of all direct and indirect subclasses::.

ffill()

Forward fill an iterator with the last selected value.

filter_values()

Yield value from iterable unless they are in values.

fixedpoint()

Find the fixed point of a function f with the initial parameter init.

fold()

Fold the given function over xs, with init initial accumulator value.

foldr()

Right-associative version of fold().

get_call_site()

Get the location of the source that called that function.

get_common_ancestor()

Pick the most derived common ancestor between the classes, assuming single inheritance.

get_common_prefix()

Return the common prefix of the passed iterables as an iterator.

get_doc_url()

Return an URL to the documentation about the given object.

get_nested_key()

Get a key in a nested mapping.

get_obj_name()

Get a prettily-formated name for the object given as parameter.

get_parent_namespace()

Return the enclosing namespace of obj (a class or a module).

get_short_doc()

Get the short documentation paragraph at the beginning of docstrings.

get_sphinx_role()

Return the reStructuredText Sphinx role of a given object.

get_subclasses()

Get all indirect subclasses of the class.

group_by_value()

Group a mapping by its values.

groupby()

Equivalent of itertools.groupby(), with a pre-sorting so it works as expected.

grouper()

Collect data into fixed-length chunks or blocks.

guess_format()

Guess the file format from a path, using the mime types database.

ignore_exceps()

Wrap a context manager and handle exceptions raised in __enter__() and __exit__().

import_all_submodules()

Import all submodules of a given package.

is_link_dead()

Check if link is dead. If dead, returns a truthy value, otherwise a falsy one.

is_monotonic()

Return True if the given sequence is monotonic, False otherwise.

is_running_ipython()

Returns True if running in IPython console or Jupyter notebook, False otherwise.

is_running_sphinx()

Returns True if the module is imported when Sphinx is running, False otherwise.

kwargs_dispatcher()

Decorate a function so that it acts as an argument dispatcher between multiple other functions.

kwargs_forwarded_to()

Similar to functools.wraps(), except that it will only fixup the signature.

loopify()

Try to factor an iterable into a prefix that is repeated a number of times.

lru_memoized()

Decorator to memoize the result of a callable, based on functools.lru_cache().

measure_time()

Context manager to measure time in seconds.

memoized()

Decorator to memoize the result of a callable, based on functools.lru_cache().

mp_spawn_pool()

Create a context manager wrapping multiprocessing.pool.Pool using the spawn method, which is safe even in multithreaded applications.

namedtuple()

Same as collections.namedtuple(), with collections.abc.Mapping behaviour.

newtype()

Make a new class inheriting from cls with the given name.

non_recursive_property()

Create a property that raises an AttributeError if it is re-entered.

optional_kwargs()

Decorator used to allow another decorator to both take keyword parameters when called, and none when not called::.

order_as()

Reorder the iterable of items to match the sequence in order_as. Items present in items and not in order_as will be appended at the end, in appearance order.

resolve_dotted_name()

Resolve a dotted name, importing all modules necessary.

set_nested_key()

Set a key in a nested mapping.

setup_logging()

Initialize logging used for all the LISA modules.

show_doc()

Show the online LISA documentation about the given object.

sig_bind()

Similar to inspect.Signature.bind() but expands variable keyword arguments so that the resulting dictionary can be used directly in a function call.

sphinx_nitpick_ignore()

Set of objects to ignore without warning when cross referencing in Sphinx.

sphinx_register_nitpick_ignore()

Register an object with a name that cannot be resolved and therefore cross referenced by Sphinx.

split_paragraphs()

Split string into a list of paragraphs.

subprocess_detailed_excep()

Context manager that will replace subprocess.CalledProcessError by a subclass that shows more details.

subprocess_log()

Similar to subprocess.check_output() but merges stdout and stderr and logs them using the logging module as it goes.

take()

Yield the first n items of an iterator, if n positive, or last items otherwise.

unzip_into()

Unzip a given iterator into n variables.

update_params_from()

Decorator to update the signature of the decorated function using annotation and default values from the specified f function.

update_wrapper_doc()

Equivalent to functools.wraps() that updates the signature by taking into account the wrapper’s extra keyword-only parameters and the given description.

value_range()

Equivalent to builtin range function, but works for floats as well.

Exceptions

ContextManagerDestroyed

Exception raised in context managers created by destroyablecontextmanager() when no exception was raised during yield per say but the context manager was destroyed without calling __exit__.

ContextManagerExcep

Exception raised when an exception was raised during yield in a context manager created with destroyablecontextmanager().

ContextManagerExit

Dummy exception raised in the generator wrapped by destroyablecontextmanager() when anything else than GeneratorExit happened during yield.

ContextManagerNoExcep

Exception raised when no exception was raised during yield in a context manager created with destroyablecontextmanager().