Skip to content

Planka Base Model

CLASS DESCRIPTION
PlankaModel

Base Planka object interface

PlankaModel

PlankaModel(schema: Schema, session: Planka)

Base Planka object interface

METHOD DESCRIPTION
__eq__
__getitem__
__hash__
__repr__
__setitem__
__str__
copy

Create a deepcopy of the model and its associated schema.

diff

Get a schema diff between two model schemas.

json
ATTRIBUTE DESCRIPTION
__formatter__

Formatter func that allows overriding str behavior for models

TYPE: ModelFormatter[Self]

client

current_id

current_role

endpoints

id

TYPE: str

schema

TYPE: Schema

session

Source code in src/plankapy/v2/models/_base.py
30
31
32
33
34
35
36
def __init__(self, schema: Schema, session: Planka) -> None:
    self._schema = schema
    self.session = session
    self.endpoints = session.endpoints
    self.client = session.client
    self.current_role = session.current_role
    self.current_id = session.current_id

__formatter__ class-attribute instance-attribute

__formatter__: ModelFormatter[Self] = DEFAULT_FORMATTER

Formatter func that allows overriding str behavior for models

client instance-attribute

client = client

current_id instance-attribute

current_id = current_id

current_role instance-attribute

current_role = current_role

endpoints instance-attribute

endpoints = endpoints

id property

id: str

schema property writable

schema: Schema

session instance-attribute

session = session

__eq__

__eq__(other: object) -> bool
Source code in src/plankapy/v2/models/_base.py
52
53
54
55
56
57
58
59
60
61
62
def __eq__(self, other: object) -> bool:
    if isinstance(other, PlankaModel):
        try:
            return (
                self.id == other.id 
                and self.__class__ == other.__class__ # type: ignore
            )
        except AttributeError: # handle no id case (Config)
            return False
    else:
        return super().__eq__(other)

__getitem__

__getitem__(key: str) -> Any
Source code in src/plankapy/v2/models/_base.py
69
70
71
def __getitem__(self, key: str) -> Any:
    # Allow direct access to model schema cache
    return self.schema[key]

__hash__

__hash__() -> int
Source code in src/plankapy/v2/models/_base.py
64
65
66
67
def __hash__(self) -> int:
    if 'id' not in self.schema:
        raise AttributeError(f'{self.__class__.__name__} does not have a hashable id attribute')
    return int(self.id)

__repr__

__repr__() -> str
Source code in src/plankapy/v2/models/_base.py
122
123
def __repr__(self) -> str:
    return f'{self.__class__.__name__}({self.schema})'          

__setitem__

__setitem__(key: Any, val: Any) -> Any
Source code in src/plankapy/v2/models/_base.py
73
74
75
76
77
78
def __setitem__(self, key: Any, val: Any) -> Any:
    # Don't allow writes to the cached values
    raise TypeError(
        f'Model attributes are read only. '
        'To update use associated property'
    )

__str__

__str__() -> str
Source code in src/plankapy/v2/models/_base.py
119
120
def __str__(self) -> str:
    return self.__class__.__formatter__(self)

copy

copy() -> Self

Create a deepcopy of the model and its associated schema.

Note

Since the endpoints for both instances of the Model are the same, any calls to update will restore the state and bring both copies into sync. copies like this are meant more for comparing changes when running a sync or update/assignemnt operation.

Example:

    >>> card_copy = card.copy()
    >>> card.name = 'Updated Name'
    >>> card_copy.name
    'Original Name'
    >>> card.name
    'Updated Name'
    >>> # This update may have had side effects
    >>> print(card_copy.diff(card))
    {'name': ('Original Name', 'Updated Name'), 'updatedAt': ('...2:00pm', '...2:45pm'), ...}

Source code in src/plankapy/v2/models/_base.py
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
def copy(self) -> Self:
    """Create a deepcopy of the model and its associated schema.

    Note:
        Since the endpoints for both instances of the Model are the same, any 
        calls to update will restore the state and bring both copies into sync. 
        copies like this are meant more for comparing changes when running a sync 
        or update/assignemnt operation.

    Example:
    ```python
        >>> card_copy = card.copy()
        >>> card.name = 'Updated Name'
        >>> card_copy.name
        'Original Name'
        >>> card.name
        'Updated Name'
        >>> # This update may have had side effects
        >>> print(card_copy.diff(card))
        {'name': ('Original Name', 'Updated Name'), 'updatedAt': ('...2:00pm', '...2:45pm'), ...}
    ```
    """
    return copy.deepcopy(self)

diff

diff(other: PlankaModel[Schema]) -> Diff

Get a schema diff between two model schemas.

Note

Only matching keys are diffed. Any schema keys that are not in the source schema will not be checked in the target schema

Source code in src/plankapy/v2/models/_base.py
104
105
106
107
108
109
110
111
112
113
114
115
116
117
def diff(self, other: PlankaModel[Schema]) -> Diff:
    """Get a schema diff between two model schemas.

    Note:
        Only matching keys are diffed. Any schema keys that are not in the source schema 
        will not be checked in the target schema
    """
    return {
        k: (source, delta) 
        for k, source in self.schema
        if k in other.schema
        and (delta := other.schema[k]) 
        and delta != source
    }

json

json(**kwargs: Any) -> str
Source code in src/plankapy/v2/models/_base.py
125
126
def json(self, **kwargs: Any) -> str:
    return json.dumps(self.schema, **kwargs)