User-defined Functions
This is a simple example showing how to register a function with the object library of the openclean engine.
[1]:
# Crete an in-memory instance of the openclean engine.
from openclean.engine.base import DB
db = DB()
[2]:
# Register user-defined function.
from openclean.engine.object.function import Int
@db.register.eval(name='add_number', parameters=[Int('n')])
def add_n(value, n=10):
return value + n
[3]:
# Get serialized listing of descriptors for all registered functions.
db.register.functions().to_listing()
[3]:
[{'name': 'add_number',
'namespace': None,
'columns': 1,
'columnLabels': ['value'],
'outputs': 1,
'parameters': [{'dtype': 'int',
'name': 'n',
'index': 0,
'isRequired': False}]}]
[4]:
# The decorated function is a function handle, but it can be used
# like a regular function.
type(add_n)
[4]:
openclean.engine.object.function.FunctionHandle
[5]:
add_n.__name__
[5]:
'add_n'
[6]:
add_n(3, n=4)
[6]:
7
[7]:
add_n(3)
[7]:
13