pg.functor

Accessible via pg.functor, pg.symbolic.functor.

functor(args=None, returns=None, base_class=None, **kwargs)[source]

Function/Decorator for creating symbolic function from regular function.

Example:

# Create a symbolic function without specifying the
# validation rules for arguments.
@pg.functor
def foo(x, y):
  return x + y

f = foo(1, 2)
assert f() == 3

# Create a symbolic function with specifying the
# the validation rules for argument 'a', 'args', and 'kwargs'.
@pg.functor([
  ('a', pg.typing.Int()),
  ('b', pg.typing.Float()),
  ('args', pg.List(pg.typing.Int())),
  (pg.typing.StrKey(), pg.typing.Int())
])
def bar(a, b, c, *args, **kwargs):
  return a * b / c + sum(args) + sum(kwargs.values())

See pg.Functor for more details on symbolic function.

Parameters:
  • args – A list of tuples that defines the schema for function arguments. Please see functor_class for detailed explanation of args.

  • returns – Optional value spec for return value.

  • base_class – Optional base class derived from symbolic.Functor. If None, returning functor will inherit from symbolic.Functor.

  • **kwargs – Keyword arguments for infrequently used options: Acceptable keywords are: * serialization_key: An optional string to be used as the serialization key for the class during sym_jsonify. If None, cls.__type_name__ will be used. This is introduced for scenarios when we want to relocate a class, before the downstream can recognize the new location, we need the class to serialize it using previous key. * additional_keys: An optional list of strings as additional keys to deserialize an object of the registered class. This can be useful when we need to relocate or rename the registered class while being able to load existing serialized JSON values.

Returns:

A function that converts a regular function into a symbolic function.