fn_graph package

Module contents

class fn_graph.Composer(*, _functions=None, _parameters=None, _cache=None, _tests=None, _source_map=None)

Bases: object

A function composer is responsible for orchestrating the composition and execution of graph of functions.

Pure free functions are added to the composer, the names of the function arguments are used to determine how those functions are wired up into a directed acyclic graph.

ancestor_dag(outputs)

A dag of all the ancestors of the given outputs, i.e. the functions that must be calculated to for the given outputs.

cache(backend=None) → fn_graph.Composer

Create a new composer with a given cache backend.

By default this is a SimpleCache.

cache_clear()

Clear the cache

cache_graphviz(outputs=(), **kwargs)

Display a graphviz with the cache invalidated nodes highlighted.

cache_invalidate(*nodes)

Invalidate the cache for all nodes affected by the given nodes (the descendants).

calculate(outputs, perform_checks=True, intermediates=False, progress_callback=None)
call(output)

A convenience method to calculate a single output

check(outputs=None)

Returns a generator of errors if there are any errors in the function graph.

dag()

Generates the DAG representing the function graph.

Return type:a networkx.DiGraph instance with function names as nodes
development_cache(name, cache_dir=None) → fn_graph.Composer

Create a new composer with a development cache setup

functions()

Dictionary of the functions

get_source(key)

Returns the source code that defines this function.

graphviz(*, hide_parameters=False, expand_links=False, flatten=False, highlight=None, filter=None, extra_node_styles=None)

Generates a graphviz.DiGraph that is suitable for display.

This requires graphviz to be installed.

The output can be directly viewed in a Jupyter notebook.

Create a symlink between an argument name and a function output. This is a convenience method. For example:

f.link(my_unknown_argument=”my_real_function”)

is the same as

f.update(my_unknown_argument= lambda my_real_function: my_real_function)

parameters()

Dictionary of the parameters of the form {key: (type, value)}

precalculate(outputs)

Create a new Composer where the results of the given functions have been pre-calculated.

Parameters:outputs – list of the names of the functions to pre-calculate
Return type:A composer
raw_function(name)

Access a raw function in the composer by name. Returns None if not found.

run_tests()

Run all the composer tests.

Returns:A generator of ComposerTestResults(name, passed, exception).
set_source_map(source_map)

Source maps allow you to override the code returned by get source.

This is rarely used, and only in esoteric circumstances.

subgraph(function_names)

Given a collection of function names this will create a new composer that only consists of those nodes.

update(*args, **kwargs) → fn_graph.Composer

Add functions to the composer.

Parameters:args – Positional arguments use the __name__ of the function as the reference
in the graph.
kwargs: Keyword arguments use the key as the name of the function in the graph.
Returns:A new composer with the functions added.
update_from(*composers) → fn_graph.Composer

Create a new composer with all the functions from this composer as well as the the passed composers.

Parameters:composers – The composers to take functions from
Returns:A new Composer with all the input composers functions added.
update_namespaces(**namespaces) → fn_graph.Composer

Given a group of keyword named composers, create a series of functions namespaced by the keywords and drawn from the composers’ functions.

Parameters:namespaces – Composers that will be added at the namespace that corresponds to the arguments key
Returns:A new Composer with all the input composers functions added as namespaces.
update_parameters(**parameters) → fn_graph.Composer

Allows you to pass static parameters to the graph, they will be exposed as callables.

update_tests(**tests) → fn_graph.Composer

Adds tests to the composer.

A test is a function that should check a property of the calculations results and raise an exception if they are not met.

The work exactly the same way as functions in terms of resolution of arguments. They are run with the run_test method.

update_without_prefix(prefix: str, *functions, **kwargs) → fn_graph.Composer

Given a prefix and a list of (named) functions, this adds the functions to the composer but first strips the prefix from their name. This is very useful to stop name shadowing.

Parameters:
  • prefix – The prefix to strip off the function names
  • functions – functions to add while stripping the prefix
  • kwargs – named functions to add
Returns:

A new composer with the functions added

update_without_suffix(suffix: str, *functions, **kwargs) → fn_graph.Composer

Given a suffix and a list of (named) functions, this adds the functions to the composer but first strips the suffix from their name. This is very useful to stop name shadowing.

Parameters:
  • suffix – The suffix to strip off the function names
  • functions – functions to add while stripping the suffix
  • kwargs – named functions to add
Returns:

A new composer with the functions added

fn_graph.ComposerTestResult

The results of Composer.run_tests()

alias of fn_graph.TestResult