schrodinger.utils.funcchains module¶
This module provides a system for collecting functions into ordered groups called chains. The functions in a chain can be executed sequentially, with an optional callback after each function that can be used to terminate the processing of the chain. An object may have multiple chains of functions, each identified by its respective decorator.
To use, create an object that inherits FuncChainMixin, and use a FuncChainDecorator to mark methods in that object as belonging to a particular chain. For example:
my_startup_functions = FuncChainDecorator()
class Foo(FuncChainMixin):
def __init__(self):
self.processFuncChain('startup')
@my_startup_functions(order=1)
def initVariables(self):
...
@my_startup_functions(order=2)
def setupWorkspace(self):
...
Supply a callback to self.processFuncChain(result_callback=bar) that accepts the return value of each function and returns True to continue processing the chain or False to terminate processing of the chain.
- class schrodinger.utils.funcchains.FuncChainMarker(label=None)¶
Bases:
schrodinger.utils.funcgroups.FuncGroupMarker
- customizeFuncResult(func, result)¶
Override this method to customize the return value of a function in the chain. This can be used to cast or interpret the return value or use the return value to build a custom object.
- Parameters
func – The function that produced the result
result – The return value of that function
- __init__(label=None)¶
- Parameters
label (str) – An optional human-readable identifier to use in the repr
- class schrodinger.utils.funcchains.FuncChainMixin(*args, **kwargs)¶
Bases:
schrodinger.utils.funcgroups.FuncGroupMixin
- __init__(*args, **kwargs)¶
- processFuncChain(chain=None, result_callback=None)¶
Execute each function in the specified chain sequentially in order.
The result_callback is called after each function with the return value of that function. This can be used to respond to the return value (e.g. present information to the user, get user feedback, log the result, etc.)
The return value of the result_callback determines whether processing will proceeed to the next function.
- Parameters
chain (FuncChainDecorator) – which chain to process
result_callback – the callback that will get called with the result of each function in the chain
- Returns
a list of the results from the functions
- addFuncToGroup(func, group=None, order=None)¶
Adds a function to the specified chain. Typically used for adding functions that are not methods of this object.
The function may optionally be decorated with a FuncGroupMarker. If so, the default group and order will be determined by the decorator. Any group or order explicitly passed in to addFuncToGroup will take precedence over the decorator settings.
- Parameters
func – the function to add
group (FuncGroupMarker or None) – the group marker. If the function is decorated with a FuncGoupMarker, that group marker will be the default.
order (float or None) – the sorting order. If the function is decorated with a FuncGoupMarker, the order specified in the decorator will be the default.
- getAddedFuncs(group=None)¶
- getFuncGroup(group=None)¶
Retrieve the functions belonging to the specified group.
- Parameters
group (FuncGroupMarker) – the group marker
- Returns
the functions in the specified group, in order
- Return type
list