Selfie
Loading...
Searching...
No Matches
Snapshot.py
Go to the documentation of this file.
1from collections.abc import Iterator
2from typing import Union
3
4from .ArrayMap import ArrayMap
5from .LineReader import _to_unix
6from .SnapshotValue import SnapshotValue
7
8
9class Snapshot:
11 self,
12 subject: SnapshotValue,
13 facet_data: ArrayMap[str, SnapshotValue] = ArrayMap.empty(), # noqa: B008
14 ):
15 self._subject = subject
16 self._facet_data = facet_data
17
18 @property
19 def subject(self) -> SnapshotValue:
20 return self._subject
21
22 @property
23 def facets(self) -> ArrayMap[str, SnapshotValue]:
24 return self._facet_data
25
26 def __eq__(self, other: object) -> bool:
27 if not isinstance(other, Snapshot):
28 return NotImplemented
29 return self._subject == other._subject and self._facet_data == other._facet_data
30
31 def __hash__(self) -> int:
32 return hash((self._subject, tuple(self._facet_data.items())))
33
35 self, key: str, value: Union[bytes, str, SnapshotValue]
36 ) -> "Snapshot":
37 if key == "":
38 raise ValueError("The empty string is reserved for the subject.")
39 return Snapshot(
40 self._subject,
41 self._facet_data.plus(_to_unix(key), SnapshotValue.of(value)),
42 )
43
45 self, key: str, value: Union[bytes, str, SnapshotValue]
46 ) -> "Snapshot":
47 if key == "":
48 return Snapshot(SnapshotValue.of(value), self._facet_data)
49 else:
50 return Snapshot(
51 self._subject,
52 self._facet_data.plus_or_noop_or_replace(
53 _to_unix(key), SnapshotValue.of(value)
54 ),
55 )
56
57 def subject_or_facet_maybe(self, key: str) -> Union[SnapshotValue, None]:
58 return self._subject if key == "" else self._facet_data.get(key)
59
60 def subject_or_facet(self, key: str) -> SnapshotValue:
61 value = self.subject_or_facet_maybe(key)
62 if value is None:
63 raise KeyError(f"'{key}' not found in snapshot.")
64 return value
65
66 @staticmethod
67 def of(data: Union[bytes, str, SnapshotValue]) -> "Snapshot":
68 if not isinstance(data, SnapshotValue):
69 data = SnapshotValue.of(data)
70 return Snapshot(data, ArrayMap.empty())
71
72 @staticmethod
73 def of_items(items: Iterator[tuple[str, SnapshotValue]]) -> "Snapshot":
74 subject = None
75 facets = ArrayMap.empty()
76 for entry in items:
77 (key, value) = entry
78 if key == "":
79 if subject is not None:
80 raise ValueError(
81 "Duplicate root snapshot value.\n first: ${subject}\n second: ${value}"
82 )
83 subject = value
84 else:
85 facets = facets.plus(key, value)
86 return Snapshot(subject if subject else SnapshotValue.of(""), facets)
87
88 def items(self) -> Iterator[tuple[str, SnapshotValue]]:
89 yield ("", self._subject)
90 yield from self._facet_data.items()
91
92 def __repr__(self) -> str:
93 pieces = [f"Snapshot.of({self.subject.value_string()!r})"]
94 for e in self.facets.items():
95 pieces.append(f"\n .plus_facet({e[0]!r}, {e[1].value_string()!r})") # noqa: PERF401
96 return "".join(pieces)
"Snapshot" plus_or_replace(self, str key, Union[bytes, str, SnapshotValue] value)
Definition Snapshot.py:46
SnapshotValue subject(self)
Definition Snapshot.py:19
Iterator[tuple[str, SnapshotValue]] items(self)
Definition Snapshot.py:88
SnapshotValue subject_or_facet(self, str key)
Definition Snapshot.py:60
__init__(self, SnapshotValue subject, ArrayMap[str, SnapshotValue] facet_data=ArrayMap.empty())
Definition Snapshot.py:14
"Snapshot" of(Union[bytes, str, SnapshotValue] data)
Definition Snapshot.py:67
ArrayMap[str, SnapshotValue] facets(self)
Definition Snapshot.py:23
"Snapshot" of_items(Iterator[tuple[str, SnapshotValue]] items)
Definition Snapshot.py:73
bool __eq__(self, object other)
Definition Snapshot.py:26
"Snapshot" plus_facet(self, str key, Union[bytes, str, SnapshotValue] value)
Definition Snapshot.py:36
Union[SnapshotValue, None] subject_or_facet_maybe(self, str key)
Definition Snapshot.py:57