text
stringlengths 1
93.6k
|
|---|
raise TypeError('Task function must be a callable object')
|
if (len(task['outputs']) > 1 and
|
not inspect.isgeneratorfunction(task['fn'])):
|
raise TypeError('Multiple outputs are only supported with \
|
generator functions')
|
if inspect.isgeneratorfunction(task['fn']):
|
if task['outputs'][0] == '*':
|
raise TypeError('Generator functions cannot be used for tasks with \
|
output specification "*"')
|
return Task(**task)
|
def input_parser(key, data):
|
"""Used to pass in full workspace if an input is defined as '*'"""
|
if key == '*':
|
return copy.copy(data)
|
else:
|
return data[key]
|
def run_task(task, workspace):
|
"""
|
Runs the task and updates the workspace with results.
|
Parameters
|
----------
|
task - dict
|
Task Description
|
Examples:
|
{'task': task_func, 'inputs': ['a', 'b'], 'outputs': 'c'}
|
{'task': task_func, 'inputs': '*', 'outputs': '*'}
|
{'task': task_func, 'inputs': ['*','a'], 'outputs': 'b'}
|
Returns a new workspace with results
|
"""
|
data = copy.copy(workspace)
|
task = validate_task(task)
|
# Prepare input to task
|
inputs = [input_parser(key, data) for key in task.inputs]
|
if inspect.isgeneratorfunction(task.fn):
|
# Multiple output task
|
# Assuming number of outputs are equal to number of return values
|
data.update(zip(task.outputs, task.fn(*inputs)))
|
else:
|
# Single output task
|
results = task.fn(*inputs)
|
if task.outputs[0] != '*':
|
results = {task.outputs[0]: results}
|
elif not isinstance(results, dict):
|
raise TypeError('Result should be a dict for output type *')
|
data.update(results)
|
return data
|
def run_hook(name, workspace, hooks):
|
"""Runs all hooks added under the give name.
|
Parameters
|
----------
|
name - str
|
Name of the hook to invoke
|
workspace - dict
|
Workspace that the hook functions operate on
|
hooks - dict of lists
|
Mapping with hook names and callback functions
|
"""
|
data = copy.copy(workspace)
|
for hook_listener in hooks.get(name, []):
|
# Hook functions may mutate the data and returns nothing
|
hook_listener(data)
|
return data
|
Task = collections.namedtuple('Task', ['fn', 'inputs', 'outputs'])
|
Hook = str # For now
|
class Workflow(object):
|
def __init__(self, task_list=None, description='simplepipe Workflow'):
|
if task_list is None:
|
self.tasks = []
|
else:
|
self.tasks = task_list
|
self.hooks = {}
|
self.__doc__ = description
|
def add_task(self, fn, inputs=None, outputs=None):
|
"""
|
Adds a task to the workflow.
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.