Skip to content

attribute_adapter.py

AttributeAdapter

Base class for adapter objects for user-defined attribute types.

Source code in datajoint/attribute_adapter.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class AttributeAdapter:
    """
    Base class for adapter objects for user-defined attribute types.
    """

    @property
    def attribute_type(self):
        """
        :return: a supported DataJoint attribute type to use; e.g. "longblob", "blob@store"
        """
        raise NotImplementedError("Undefined attribute adapter")

    def get(self, value):
        """
        convert value retrieved from the the attribute in a table into the adapted type

        :param value: value from the database

        :return: object of the adapted type
        """
        raise NotImplementedError("Undefined attribute adapter")

    def put(self, obj):
        """
        convert an object of the adapted type into a value that DataJoint can store in a table attribute

        :param obj: an object of the adapted type
        :return: value to store in the database
        """
        raise NotImplementedError("Undefined attribute adapter")

attribute_type() property

:return: a supported DataJoint attribute type to use; e.g. "longblob", "blob@store"

Source code in datajoint/attribute_adapter.py
11
12
13
14
15
16
@property
def attribute_type(self):
    """
    :return: a supported DataJoint attribute type to use; e.g. "longblob", "blob@store"
    """
    raise NotImplementedError("Undefined attribute adapter")

get(value)

convert value retrieved from the the attribute in a table into the adapted type

:param value: value from the database

:return: object of the adapted type

Source code in datajoint/attribute_adapter.py
18
19
20
21
22
23
24
25
26
def get(self, value):
    """
    convert value retrieved from the the attribute in a table into the adapted type

    :param value: value from the database

    :return: object of the adapted type
    """
    raise NotImplementedError("Undefined attribute adapter")

put(obj)

convert an object of the adapted type into a value that DataJoint can store in a table attribute

:param obj: an object of the adapted type :return: value to store in the database

Source code in datajoint/attribute_adapter.py
28
29
30
31
32
33
34
35
def put(self, obj):
    """
    convert an object of the adapted type into a value that DataJoint can store in a table attribute

    :param obj: an object of the adapted type
    :return: value to store in the database
    """
    raise NotImplementedError("Undefined attribute adapter")

get_adapter(context, adapter_name)

Extract the AttributeAdapter object by its name from the context and validate.

Source code in datajoint/attribute_adapter.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def get_adapter(context, adapter_name):
    """
    Extract the AttributeAdapter object by its name from the context and validate.
    """
    if not _support_adapted_types():
        raise DataJointError("Support for Adapted Attribute types is disabled.")
    adapter_name = adapter_name.lstrip("<").rstrip(">")
    try:
        adapter = (
            context[adapter_name]
            if adapter_name in context
            else type_plugins[adapter_name]["object"].load()
        )
    except KeyError:
        raise DataJointError(
            "Attribute adapter '{adapter_name}' is not defined.".format(
                adapter_name=adapter_name
            )
        )
    if not isinstance(adapter, AttributeAdapter):
        raise DataJointError(
            "Attribute adapter '{adapter_name}' must be an instance of datajoint.AttributeAdapter".format(
                adapter_name=adapter_name
            )
        )
    if not isinstance(adapter.attribute_type, str) or not re.match(
        r"^\w", adapter.attribute_type
    ):
        raise DataJointError(
            "Invalid attribute type {type} in attribute adapter '{adapter_name}'".format(
                type=adapter.attribute_type, adapter_name=adapter_name
            )
        )
    return adapter