Skip to main content

Lazily evaluate function dependency graph

Project description

lazydep

Evaluates some set of functions, according to tome dependency graph structure

There's pretty much 1 function that is worth noting:

lazydep.resolve(graph, state, functions, request=None)

Arguments

  • graph: dictionary, mapping function names to the names of parameters required for their execution
  • state: dictionary, initial input to functions. the remainder of intermediary parameters will be bootstrapped
  • functions: list of functions in any order, or dictionary mapping function names to functions
  • request: name of desired function evaluation. If request is specified, only the desired function call will be returned. If left empty, all functions specified will be returned, in the same form as state object.

Returns

dictionary, or result of 'request' function

Example

# Each key here corresponds to a function name
# Once evaluated, a function's return value is stored as a key as its name
depGraph = {

	# Once we specify seed, all else will be inferred
    'f1': 'seed',
    'f2': 'f1',
    'f3': ['f1','f2'],

	# Function unused. If we only request 'f3', these will never be evaluated
    'f4': 'f3',
    'f5': 'f6',
    'f6': 'seed'
}



# Functions will receive parameters according to the dependency graph
# Keyword args only, positional args not supported
def f1(seed):
    return seed % 10

def f2(f1):
    return [f1]*f1

def f3(f1, f2):
    return (f1,sum(f2))




# The initial information, some set of named parameters.
istate = {'seed': 42}


res = lazydep.resolve(

	# How are inputs and outputs reordered?
	graph = depGraph,

	# What information are we working with?
	state = istate,

	# What functions are allowed?
	functions = [f1,f2,f3],

	# Which function output are we requesting?
	# If request == None, we will return the aggregate state, after running all functions
	request = 'f3',
)

assert( res == (2,4) )

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lazydep-1.1.tar.gz (4.3 kB view hashes)

Uploaded Source

Built Distribution

lazydep-1.1-py2.py3-none-any.whl (3.1 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page