Selfie
Loading...
Searching...
No Matches
selfie_lib.Snapshot.Snapshot Class Reference

Public Member Functions

 __init__ (self, SnapshotValue subject, ArrayMap[str, SnapshotValue] facet_data=ArrayMap.empty())
 
SnapshotValue subject (self)
 
ArrayMap[str, SnapshotValuefacets (self)
 
bool __eq__ (self, object other)
 
int __hash__ (self)
 
"Snapshot" plus_facet (self, str key, Union[bytes, str, SnapshotValue] value)
 
"Snapshot" plus_or_replace (self, str key, Union[bytes, str, SnapshotValue] value)
 
Union[SnapshotValue, None] subject_or_facet_maybe (self, str key)
 
SnapshotValue subject_or_facet (self, str key)
 
Iterator[tuple[str, SnapshotValue]] items (self)
 
str __repr__ (self)
 

Static Public Member Functions

"Snapshot" of (Union[bytes, str, SnapshotValue] data)
 
"Snapshot" of_items (Iterator[tuple[str, SnapshotValue]] items)
 

Protected Attributes

 _subject
 
 _facet_data
 

Detailed Description

Definition at line 9 of file Snapshot.py.

Constructor & Destructor Documentation

◆ __init__()

selfie_lib.Snapshot.Snapshot.__init__ (   self,
SnapshotValue  subject,
ArrayMap[str, SnapshotValue]   facet_data = ArrayMap.empty() 
)

Definition at line 10 of file Snapshot.py.

14 ):
15 self._subject = subject
16 self._facet_data = facet_data
17

Member Function Documentation

◆ __eq__()

bool selfie_lib.Snapshot.Snapshot.__eq__ (   self,
object  other 
)

Definition at line 26 of file Snapshot.py.

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

◆ __hash__()

int selfie_lib.Snapshot.Snapshot.__hash__ (   self)

Definition at line 31 of file Snapshot.py.

31 def __hash__(self) -> int:
32 return hash((self._subject, tuple(self._facet_data.items())))
33

◆ __repr__()

str selfie_lib.Snapshot.Snapshot.__repr__ (   self)

Definition at line 92 of file Snapshot.py.

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)

◆ facets()

ArrayMap[str, SnapshotValue] selfie_lib.Snapshot.Snapshot.facets (   self)

Definition at line 23 of file Snapshot.py.

23 def facets(self) -> ArrayMap[str, SnapshotValue]:
24 return self._facet_data
25

◆ items()

Iterator[tuple[str, SnapshotValue]] selfie_lib.Snapshot.Snapshot.items (   self)

Definition at line 88 of file Snapshot.py.

88 def items(self) -> Iterator[tuple[str, SnapshotValue]]:
89 yield ("", self._subject)
90 yield from self._facet_data.items()
91

◆ of()

"Snapshot" selfie_lib.Snapshot.Snapshot.of ( Union[bytes, str, SnapshotValue data)
static

Definition at line 67 of file Snapshot.py.

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

◆ of_items()

"Snapshot" selfie_lib.Snapshot.Snapshot.of_items ( Iterator[tuple[str, SnapshotValue]]  items)
static

Definition at line 73 of file Snapshot.py.

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

◆ plus_facet()

"Snapshot" selfie_lib.Snapshot.Snapshot.plus_facet (   self,
str  key,
Union[bytes, str, SnapshotValue]   value 
)

Definition at line 34 of file Snapshot.py.

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

◆ plus_or_replace()

"Snapshot" selfie_lib.Snapshot.Snapshot.plus_or_replace (   self,
str  key,
Union[bytes, str, SnapshotValue]   value 
)

Definition at line 44 of file Snapshot.py.

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

◆ subject()

SnapshotValue selfie_lib.Snapshot.Snapshot.subject (   self)

Definition at line 19 of file Snapshot.py.

19 def subject(self) -> SnapshotValue:
20 return self._subject
21

◆ subject_or_facet()

SnapshotValue selfie_lib.Snapshot.Snapshot.subject_or_facet (   self,
str  key 
)

Definition at line 60 of file Snapshot.py.

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

◆ subject_or_facet_maybe()

Union[SnapshotValue, None] selfie_lib.Snapshot.Snapshot.subject_or_facet_maybe (   self,
str  key 
)

Definition at line 57 of file Snapshot.py.

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

Member Data Documentation

◆ _facet_data

selfie_lib.Snapshot.Snapshot._facet_data
protected

Definition at line 16 of file Snapshot.py.

◆ _subject

selfie_lib.Snapshot.Snapshot._subject
protected

Definition at line 15 of file Snapshot.py.


The documentation for this class was generated from the following file: