pg.boilerplate_class

Accessible via pg.boilerplate_class, pg.symbolic.boilerplate_class.

boilerplate_class(cls_name, value, init_arg_list=None, **kwargs)[source]

Create a boilerplate class using a symbolic object.

As the name indicates, a boilerplate class is a class that can be used as a boilerplate to create object.

Implementation-wise it’s a class that extends the type of input value, while setting the default values of its (inherited) schema using the value from input.

An analogy to boilerplate class is prebound function. :rtype: Type[pg.Object]

For example:

# A regular function: correspond to a pg.Object subclass.
def f(a, b, c)
  return a + b + c

# A partially bound function: correspond to a boilerplate class created
# from a partially bound object.
def g(c):
  return f(1, 2, c)

# A fully bound function: correspond to a boilerplate class created from
# a fully bound object.
def h():
  return f(1, 2, 3)

Boilerplate class can be created with a value that is fully bound (like function h above), or partially bound (like function g above). Since boilerplate class extends the type of the input, we can rebind members of its instances as we modify the input.

Here are a few examples:

@pg.members([
  ('a', pg.typing.Str(), 'Field A.'),
  ('b', pg.typing.Int(), 'Field B.'),
])
class A(pg.Object):
  pass

A1 = pg.boilerplate_class('A1', A.partial(a='foo'))
assert A1(b=1) == A(a='foo', b=1)

A2 = pg.boilerplate_class('A2', A(a='bar', b=2))
assert A2() == A(a='bar', b=2)
Parameters:
  • cls_name – Name of the boilerplate class.

  • value – Value that is used as the default value of the boilerplate class.

  • init_arg_list – An optional list of strings as __init__ positional arguments names.

  • **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 class which extends the input value’s type, with its schema’s default

values set from the input value.

Raises:

TypeError – Keyword argumment provided is not supported.