-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_types.py
63 lines (49 loc) · 1.52 KB
/
data_types.py
1
2
3
4
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
"""
This file contains all custom data types used across the application
"""
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import datetime, timezone
from enum import Enum, auto
from typing import Any
from uuid import UUID
class ChangeEvent: # pylint: disable=too-few-public-methods
"""
The base class to encapsulate any configuration/system change. The type of
change is determined by inheritance.
"""
@property
def change(self) -> dict[str, Any] | UUID | None:
"""
Returns
-------
dict or UUID or None
The changed data.
"""
return self.__change
def __init__(self, change: dict[str, Any] | UUID | None) -> None:
self.__change = change
@dataclass(frozen=True)
class DataEvent:
"""
The base class to encapsulate any data event.
"""
timestamp: float = field(init=False)
sender: UUID
sid: int
topic: str
value: Any
unit: str
def __post_init__(self):
# A slightly clumsy approach to setting the timestamp property, because this is frozen. Taken from:
# https://docs.python.org/3/library/dataclasses.html#frozen-instances
object.__setattr__(self, "timestamp", datetime.now(timezone.utc).timestamp())
def __str__(self):
return f"Data event from {self.sender}: {self.value} {self.unit}"
class ChangeType(Enum):
"""
The type of changes sent out by the database.
"""
ADD = auto()
REMOVE = auto()
UPDATE = auto()