pydent.marshaller.fields.Callback

class pydent.marshaller.fields.Callback(callback, callback_args=None, callback_kwargs=None, cache=False, data_key=None, many=None, allow_none=None, always_dump=False)[source]

Bases: pydent.marshaller.fields.Field

Make a callback when called.

A Callback field initializer.

Parameters
  • callback (callable|basestring) – name of the callback function or a callable. If a name, the name should exist as a function in the owner instance. Invalid callback signatures are captures on class creation.

  • callback_args (tuple) – a tuple of arguments to use in the callback. If any of the callback arguments or values of the callback kwargs are callable, the owner will be passed to the callable. The owner instance will replace any arguments that are Callback.SElF

  • callback_kwargs (dict) – a dictionary of kwargs to use in the callback

  • cache (bool) – whether to cache the result using setattr on the owner instance. This will initialize the serialization and deserialization procedures detailed in the corresponding field/descriptor.

  • many (bool) – whether to treat serializations and deserializations as a per-item basis in a list

  • allow_none (bool) – whether to return None if None is received in deserialization or serializaiton methods

  • data_key (Optional[str]) – the data_key, or attribute name, of this field. Calling this as an attribute to the registered nested should return this field’s descriptor.

  • always_dump (bool) – if True, this field will be serialized by default

__init__(callback, callback_args=None, callback_kwargs=None, cache=False, data_key=None, many=None, allow_none=None, always_dump=False)[source]

A Callback field initializer.

Parameters
  • callback (callable|basestring) – name of the callback function or a callable. If a name, the name should exist as a function in the owner instance. Invalid callback signatures are captures on class creation.

  • callback_args (tuple) – a tuple of arguments to use in the callback. If any of the callback arguments or values of the callback kwargs are callable, the owner will be passed to the callable. The owner instance will replace any arguments that are Callback.SElF

  • callback_kwargs (dict) – a dictionary of kwargs to use in the callback

  • cache (bool) – whether to cache the result using setattr on the owner instance. This will initialize the serialization and deserialization procedures detailed in the corresponding field/descriptor.

  • many (bool) – whether to treat serializations and deserializations as a per-item basis in a list

  • allow_none (bool) – whether to return None if None is received in deserialization or serializaiton methods

  • data_key (Optional[str]) – the data_key, or attribute name, of this field. Calling this as an attribute to the registered nested should return this field’s descriptor.

  • always_dump (bool) – if True, this field will be serialized by default

Methods

__init__(callback[, callback_args, …])

A Callback field initializer.

cache_result(owner, val)

deserialize(*args, **kwargs)

fullfill(owner[, cache, extra_args, …])

Calls the callback function using the owner object.

get_callback_args(owner[, extra_args])

Processes the callback args.

get_callback_kwargs(owner, extra_kwargs)

Processes the callback kwargs.

register(name, objtype)

Registers the field to a nested class.

serialize(owner, data)

rtype

Union[dict, None, List]

set_data_key(key)

Attributes

SELF

ACCESSOR

alias of pydent.marshaller.descriptors.CallbackAccessor

_Callback__FLAGS

alias of Callback.__FLAGS

fullfill(owner, cache=None, extra_args=None, extra_kwargs=None)[source]

Calls the callback function using the owner object. A Callback.SELF arg value will be replaced to be equivalent to the owner instance model.

Parameters
  • owner – the owning object

  • cache (Optional[bool]) – if True, will cache the return in the deserialized data. On next call, the cached result will be returned.

  • extra_args (Optional[tuple]) – extra args to pass to the callback function

  • extra_kwargs (Optional[dict]) – extra kwargs to pass to the callback function

Returns

function result

Return type

any

get_callback_args(owner, extra_args=None)[source]

Processes the callback args.

Return type

List[Any]

get_callback_kwargs(owner, extra_kwargs)[source]

Processes the callback kwargs.

Return type

dict

register(name, objtype)

Registers the field to a nested class. Instantiates the corresponding descriptor (i.e. accessor)

Parameters
  • name (str) – name of the field

  • objtype (Type) – the nested class to register the field to

Returns

None

Return type

None