pydent.marshaller.descriptors

Data descriptors that provide special behaviors when attributes are accessed.

Classes

CallbackAccessor(name, field, accessor, …)

A descriptor that uses a registered marshaller.fields.Callback to dynamically access the value of a callback by sending the instance to the callback field’s fullfill method if the descriptor is not yet set.

DataAccessor(name[, accessor, default])

A descriptor that will dynamically access an instance’s dictionary named by the accessor key.

MarshallingAccessor(name, field, accessor, …)

A generic Marshalling descriptor.

Placeholders

Accessor placeholders.

RelationshipAccessor(name, field, accessor, …)

The descriptor for a pydent.marshaller.fields.Relationship field.

Exceptions

MarshallingAttributeAccessError

Generic error that arises from while accessing an attribute.

class pydent.marshaller.descriptors.CallbackAccessor(name, field, accessor, deserialized_accessor, default=<Placeholders.CALLBACK: 3>)[source]

Bases: pydent.marshaller.descriptors.MarshallingAccessor

A descriptor that uses a registered marshaller.fields.Callback to dynamically access the value of a callback by sending the instance to the callback field’s fullfill method if the descriptor is not yet set.

If the descriptor is already set, return that value. Deleting the descriptor sets the value to the default Placeholders.CALLBACK, which will attempt to fullfill the descriptor once accessed.

class pydent.marshaller.descriptors.DataAccessor(name, accessor=None, default=<Placeholders.DATA: 1>)[source]

Bases: object

A descriptor that will dynamically access an instance’s dictionary named by the accessor key. If the key is not in the dictionary or the value received from the dictionary is a Placeholders.DATA enumerator, an AttributeError is raised.

Usage:

This may be used to assign dynamic properties to a class method as in the example below, or subclasses of the DataAccessor can be created to create ‘hooks’ when the descriptor is accessed, set, or delete.

model_class.x = DataAccessor('x')
model_class.y = DataAccessor('y')
instance = model_class()

instance.data # {'x': Placeholders.DATA, 'y': Placeholders.DATA}

# accessing the default values
try:
    instance.x
except AttributeError:
    print("x being tracked but is not set")

# setting and accessing a value
instance.x = 5
instance.data = {'x': 5, 'y': Placeholders.DATA}
instance.x  # returns 5

# raising AttributeError
try:
    instance.y
except AttributeError:
    print("y is not set")

# setting the value
instance.y = 10
assert instance.data == {'x': 5, 'y': 10}
assert instance.y == 10

# deleting the value
del instance.y
assert instance.data == {'x': 5, 'y': Placeholders.DATA}
try:
    instance.y
except AttributeError:
    print("y is not set")
class pydent.marshaller.descriptors.MarshallingAccessor(name, field, accessor, deserialized_accessor, default=<Placeholders.MARSHALL: 2>)[source]

Bases: pydent.marshaller.descriptors.DataAccessor

A generic Marshalling descriptor.

exception pydent.marshaller.descriptors.MarshallingAttributeAccessError[source]

Bases: pydent.marshaller.exceptions.MarshallerBaseException

Generic error that arises from while accessing an attribute.

with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

class pydent.marshaller.descriptors.Placeholders[source]

Bases: enum.Enum

Accessor placeholders.

Special behaviors can occur when the descriptor returns a value in the Placeholder class. For example, when the Placeholders.CALLBACK value is returned and cache=True, this indicates that the callback function needs to be called and the result cached.

CALLBACK = 3

CALLBACK accessor holder.

DATA = 1

DATA accessor holder.

DEFAULT = 4

DEFAULT accessor holder.

MARSHALL = 2

MARSHALL accessor holder.

class pydent.marshaller.descriptors.RelationshipAccessor(name, field, accessor, deserialized_accessor, default=<Placeholders.CALLBACK: 3>)[source]

Bases: pydent.marshaller.descriptors.CallbackAccessor

The descriptor for a pydent.marshaller.fields.Relationship field.