Skip to content

Model Extensions

ExtendedBaseModel

Bases: BaseModel

Source code in catena/models/extensions.py
 5
 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
36
37
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
72
73
74
75
76
77
78
79
class ExtendedBaseModel(BaseModel):

    @no_type_check
    def __setitem__(self, name: str, value: Any):
        """
        Dunder method for allowing setting attributes dictionary style

        Args:
            name: name of attribute to set
            value: value to set attribute to
        """
        self.__setattr__(name, value)

    @no_type_check
    def __eq__(self, other):
        """
        Dunder method for allowing comparison of Options instances. If
        two instances are the same will return True
        Args:
            other: other instance of Options to compare with
        """

        # do not compare against unrelated types
        if not isinstance(other, Options):
            return NotImplemented
        return self.dict() == other.dict()


    def setdefault(self, keyname: str, value: Any):
        """
        Analogue to `dict.setdefault(keyname, value)` for Pydantic models
        Will set the value of the corresponding attribute, `keyname` if it has not been set

        Args:
            keyname: name of attribute to set default value for
            value: value to set if attribute has not been set to a non-default value already
        """
        if keyname not in self.__fields_set__:
            self[keyname] = value
            return value
        else:
            return self.dict().get(keyname)


    def pop(self, keyname: str, value: Any=None):
        """
        Analogue to `dict.pop(keyname, value)` for Pydantic models

        Will remove the attribute from the model and return it's value if it has been set,
        otherwise it will return `value`
        Args:
            keyname: name of attribute to remove and return it's value
            value: value to return if `keyname` attribute has not been set to a non-default value

        """
        _val = self.dict().pop(keyname, value)
        delattr(self, keyname)

        if _val is None:
            return value
        else:
            return _val


    def get(self, keyname: str, value: Any=None):
        """
        Analogue to `dict.get(keyname, value)` for Pydantic models
        Args:
            keyname: name of attribute for which to return the corresponding value
            value: value to return if `keyname` attribute has not been set to a non-default value
        """
        if keyname not in self.__fields__set__:
            return value
        else:
            return self.dict().get(keyname)

__eq__(other)

Dunder method for allowing comparison of Options instances. If two instances are the same will return True

Parameters:

Name Type Description Default
other

other instance of Options to compare with

required
Source code in catena/models/extensions.py
18
19
20
21
22
23
24
25
26
27
28
29
30
@no_type_check
def __eq__(self, other):
    """
    Dunder method for allowing comparison of Options instances. If
    two instances are the same will return True
    Args:
        other: other instance of Options to compare with
    """

    # do not compare against unrelated types
    if not isinstance(other, Options):
        return NotImplemented
    return self.dict() == other.dict()

__setitem__(name, value)

Dunder method for allowing setting attributes dictionary style

Parameters:

Name Type Description Default
name str

name of attribute to set

required
value Any

value to set attribute to

required
Source code in catena/models/extensions.py
 7
 8
 9
10
11
12
13
14
15
16
@no_type_check
def __setitem__(self, name: str, value: Any):
    """
    Dunder method for allowing setting attributes dictionary style

    Args:
        name: name of attribute to set
        value: value to set attribute to
    """
    self.__setattr__(name, value)

get(keyname, value=None)

Analogue to dict.get(keyname, value) for Pydantic models

Parameters:

Name Type Description Default
keyname str

name of attribute for which to return the corresponding value

required
value Any

value to return if keyname attribute has not been set to a non-default value

None
Source code in catena/models/extensions.py
69
70
71
72
73
74
75
76
77
78
79
def get(self, keyname: str, value: Any=None):
    """
    Analogue to `dict.get(keyname, value)` for Pydantic models
    Args:
        keyname: name of attribute for which to return the corresponding value
        value: value to return if `keyname` attribute has not been set to a non-default value
    """
    if keyname not in self.__fields__set__:
        return value
    else:
        return self.dict().get(keyname)

pop(keyname, value=None)

Analogue to dict.pop(keyname, value) for Pydantic models

Will remove the attribute from the model and return it's value if it has been set, otherwise it will return value

Parameters:

Name Type Description Default
keyname str

name of attribute to remove and return it's value

required
value Any

value to return if keyname attribute has not been set to a non-default value

None
Source code in catena/models/extensions.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def pop(self, keyname: str, value: Any=None):
    """
    Analogue to `dict.pop(keyname, value)` for Pydantic models

    Will remove the attribute from the model and return it's value if it has been set,
    otherwise it will return `value`
    Args:
        keyname: name of attribute to remove and return it's value
        value: value to return if `keyname` attribute has not been set to a non-default value

    """
    _val = self.dict().pop(keyname, value)
    delattr(self, keyname)

    if _val is None:
        return value
    else:
        return _val

setdefault(keyname, value)

Analogue to dict.setdefault(keyname, value) for Pydantic models Will set the value of the corresponding attribute, keyname if it has not been set

Parameters:

Name Type Description Default
keyname str

name of attribute to set default value for

required
value Any

value to set if attribute has not been set to a non-default value already

required
Source code in catena/models/extensions.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def setdefault(self, keyname: str, value: Any):
    """
    Analogue to `dict.setdefault(keyname, value)` for Pydantic models
    Will set the value of the corresponding attribute, `keyname` if it has not been set

    Args:
        keyname: name of attribute to set default value for
        value: value to set if attribute has not been set to a non-default value already
    """
    if keyname not in self.__fields_set__:
        self[keyname] = value
        return value
    else:
        return self.dict().get(keyname)