memote.utils

Utility functions used by memote and its tests.

Module Contents

Functions

register_with(registry) Register a passed in object.
annotate(title, format_type, message=None, data=None, metric=1.0) Annotate a test case with info that should be displayed in the reports.
get_ids(iterable) Retrieve the identifier of a number of objects.
get_ids_and_bounds(iterable) Retrieve the identifier and bounds of a number of objects.
truncate(sequence) Create a potentially shortened text display of a list.
log_json_incompatible_types(obj) Log types that are not JSON compatible.
show_versions() Print formatted dependency information to stdout.
jsonify(obj, pretty=False) Turn a nested object into a (compressed) JSON string.
is_modified(path, commit) Test whether a given file was present and modified in a specific commit.
stdout_notifications(notifications) Print each entry of errors and warnings to stdout.
memote.utils.wrapper[source]
memote.utils.register_with(registry)[source]

Register a passed in object.

Intended to be used as a decorator on model building functions with a dict as a registry.

Examples

REGISTRY = dict()
@register_with(REGISTRY)
def build_empty(base):
    return base
memote.utils.annotate(title, format_type, message=None, data=None, metric=1.0)[source]

Annotate a test case with info that should be displayed in the reports.

Parameters:
title : str

A human-readable descriptive title of the test case.

format_type : str

A string that determines how the result data is formatted in the report. It is expected not to be None.

  • ‘number’ : ‘data’ is a single number which can be an integer or float and should be represented as such.
  • ‘count’ : ‘data’ is a list, set or tuple. Choosing ‘count’ will display the length of that list e.g. number of metabolites without formula.
  • ‘percent’ : Instead of ‘data’ the content of ‘metric’ ought to be displayed e.g. percentage of metabolites without charge. ‘metric’ is expected to be a floating point number.
  • ‘raw’ : ‘data’ is ought to be displayed “as is” without formatting. This option is appropriate for single strings or a boolean output.
message : str

A short written explanation that states and possibly explains the test result.

data

Raw data which the test case generates and assesses. Can be of the following types: list, set, tuple, string, float, integer, and boolean.

metric: float

A value x in the range of 0 <= x <= 1 which represents the fraction of ‘data’ to the total in the model. For example, if ‘data’ are all metabolites without formula, ‘metric’ should be the fraction of metabolites without formula from the total of metabolites in the model.

Returns:
function

The decorated function, now extended by the attribute ‘annotation’.

Notes

Adds “annotation” attribute to the function object, which stores values for predefined keys as a dictionary.

memote.utils.get_ids(iterable)[source]

Retrieve the identifier of a number of objects.

memote.utils.get_ids_and_bounds(iterable)[source]

Retrieve the identifier and bounds of a number of objects.

memote.utils.truncate(sequence)[source]

Create a potentially shortened text display of a list.

Parameters:
sequence : list

An indexable sequence of elements.

Returns:
str

The list as a formatted string.

memote.utils.log_json_incompatible_types(obj)[source]

Log types that are not JSON compatible.

Explore a nested dictionary structure and log types that are not JSON compatible.

Parameters:
obj : dict

A potentially nested dictionary.

memote.utils.show_versions()[source]

Print formatted dependency information to stdout.

memote.utils.jsonify(obj, pretty=False)[source]

Turn a nested object into a (compressed) JSON string.

Parameters:
obj : dict

Any kind of dictionary structure.

pretty : bool, optional

Whether to format the resulting JSON in a more legible way ( default False).

memote.utils.is_modified(path, commit)[source]

Test whether a given file was present and modified in a specific commit.

Parameters:
path : str

The path of a file to be checked.

commit : git.Commit

A git commit object.

Returns:
bool

Whether or not the given path is among the modified files and was not deleted in this commit.

memote.utils.stdout_notifications(notifications)[source]

Print each entry of errors and warnings to stdout.

Parameters:
notifications: dict

A simple dictionary structure containing a list of errors and warnings.