Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /spraakbanken_karp-backend /src /karp /foundation /entity.py
| import datetime | |
| from typing import Optional, Union | |
| from karp.foundation.errors import ConsistencyError, DiscardedEntityError | |
| from karp.foundation.timings import monotonic_utc_now | |
| from karp.foundation.value_objects import UniqueId | |
| class Entity: | |
| def __init__( | |
| self, | |
| id: UniqueId, # noqa: A002 | |
| last_modified: Optional[float] = None, | |
| last_modified_by: Optional[str] = None, | |
| discarded: bool = False, | |
| *, | |
| version: int, | |
| ) -> None: | |
| self._id = id | |
| self._discarded = discarded | |
| self._last_modified = self._ensure_timestamp(last_modified) | |
| self._last_modified_by = "Unknown user" if last_modified_by is None else last_modified_by | |
| self._version = version | |
| def id(self): | |
| """A unique identifier for the entity.""" | |
| return self._id | |
| def entity_id(self): | |
| """A unique identifier for the entity.""" | |
| return self._id | |
| def discarded(self) -> bool: | |
| """True if this entity is marked as deleted, otherwise False.""" | |
| return self._discarded | |
| def last_modified(self): | |
| """The time this entity was last modified.""" | |
| return self._last_modified | |
| def last_modified_by(self): | |
| """The time this entity was last modified.""" | |
| return self._last_modified_by | |
| def _ensure_timestamp(self, timestamp: Optional[Union[float, datetime.datetime, str]]) -> float: | |
| if isinstance(timestamp, datetime.datetime): | |
| return timestamp.timestamp() | |
| elif isinstance(timestamp, str): | |
| return datetime.datetime.fromisoformat(timestamp).timestamp() | |
| return monotonic_utc_now() if timestamp is None else timestamp | |
| def _check_not_discarded(self): | |
| if self._discarded: | |
| raise DiscardedEntityError(f"Attempt to use {self!r}, entity_id = {self.entity_id}") | |
| def _increment_version(self): | |
| self._version += 1 | |
| def _validate_version(self, version: Optional[int]) -> None: | |
| if version is not None and version != self.version: | |
| raise ConsistencyError(f"Entity version mismatch: {version} != {self.version}") | |
| def version(self) -> int: | |
| """An integer version for the entity.""" | |
| return self._version | |
Xet Storage Details
- Size:
- 2.34 kB
- Xet hash:
- 9fcf80d4c3a890292da893488235767a6b04b5e142ce9849d127c2f1c17bd605
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.