Selfie
Loading...
Searching...
No Matches
SnapshotValue.py
Go to the documentation of this file.
1
from
abc
import
ABC, abstractmethod
2
from
typing
import
Union
3
4
from
.LineReader
import
_to_unix
5
6
7
class
SnapshotValue
(ABC):
8
@property
9
def
is_binary
(self) -> bool:
10
return
isinstance(self, SnapshotValueBinary)
11
12
@abstractmethod
13
def
value_binary
(self) -> bytes:
14
pass
15
16
@abstractmethod
17
def
value_string
(self) -> str:
18
pass
19
20
@staticmethod
21
def
of
(data: Union[bytes, str,
"SnapshotValue"
]) ->
"SnapshotValue"
:
22
if
isinstance(data, bytes):
23
return
SnapshotValueBinary
(data)
24
elif
isinstance(data, str):
25
return
SnapshotValueString
(_to_unix(data))
26
elif
isinstance(data, SnapshotValue):
27
return
data
28
else
:
29
raise
TypeError(
"Unsupported type for Snapshot creation"
)
30
31
32
class
SnapshotValueBinary
(
SnapshotValue
):
33
def
__init__
(self, value: bytes):
34
self.
_value
= value
35
36
def
value_binary
(self) -> bytes:
37
return
self.
_value
38
39
def
value_string
(self) -> str:
40
raise
NotImplementedError(
"This is a binary value."
)
41
42
def
__eq__
(self, other: object) -> bool:
43
if
isinstance(other, SnapshotValueBinary):
44
return
self.
value_binary
value_binary
() == other.value_binary()
45
return
False
46
47
def
__hash__
(self) -> int:
48
return
hash(self.
_value
)
49
50
51
class
SnapshotValueString
(
SnapshotValue
):
52
def
__init__
(self, value: str):
53
self.
_value
= value
54
55
def
value_binary
(self) -> bytes:
56
raise
NotImplementedError(
"This is a string value."
)
57
58
def
value_string
(self) -> str:
59
return
self.
_value
60
61
def
__eq__
(self, other: object) -> bool:
62
if
isinstance(other, SnapshotValueString):
63
return
self.
value_string
value_string
() == other.value_string()
64
return
False
65
66
def
__hash__
(self) -> int:
67
return
hash(self.
_value
)
selfie_lib.SnapshotValue.SnapshotValueBinary
Definition
SnapshotValue.py:32
selfie_lib.SnapshotValue.SnapshotValueBinary.__hash__
int __hash__(self)
Definition
SnapshotValue.py:47
selfie_lib.SnapshotValue.SnapshotValueBinary.__init__
__init__(self, bytes value)
Definition
SnapshotValue.py:33
selfie_lib.SnapshotValue.SnapshotValueBinary.__eq__
bool __eq__(self, object other)
Definition
SnapshotValue.py:42
selfie_lib.SnapshotValue.SnapshotValueBinary.value_string
str value_string(self)
Definition
SnapshotValue.py:39
selfie_lib.SnapshotValue.SnapshotValueBinary._value
_value
Definition
SnapshotValue.py:34
selfie_lib.SnapshotValue.SnapshotValueBinary.value_binary
bytes value_binary(self)
Definition
SnapshotValue.py:36
selfie_lib.SnapshotValue.SnapshotValueString
Definition
SnapshotValue.py:51
selfie_lib.SnapshotValue.SnapshotValueString._value
_value
Definition
SnapshotValue.py:53
selfie_lib.SnapshotValue.SnapshotValueString.__eq__
bool __eq__(self, object other)
Definition
SnapshotValue.py:61
selfie_lib.SnapshotValue.SnapshotValueString.value_string
str value_string(self)
Definition
SnapshotValue.py:58
selfie_lib.SnapshotValue.SnapshotValueString.__init__
__init__(self, str value)
Definition
SnapshotValue.py:52
selfie_lib.SnapshotValue.SnapshotValueString.value_binary
bytes value_binary(self)
Definition
SnapshotValue.py:55
selfie_lib.SnapshotValue.SnapshotValueString.__hash__
int __hash__(self)
Definition
SnapshotValue.py:66
selfie_lib.SnapshotValue.SnapshotValue
Definition
SnapshotValue.py:7
selfie_lib.SnapshotValue.SnapshotValue.of
"SnapshotValue" of(Union[bytes, str, "SnapshotValue"] data)
Definition
SnapshotValue.py:21
selfie_lib.SnapshotValue.SnapshotValue.value_string
str value_string(self)
Definition
SnapshotValue.py:17
selfie_lib.SnapshotValue.SnapshotValue.value_binary
bytes value_binary(self)
Definition
SnapshotValue.py:13
selfie_lib.SnapshotValue.SnapshotValue.is_binary
bool is_binary(self)
Definition
SnapshotValue.py:9
selfie-lib
selfie_lib
SnapshotValue.py
Generated by
1.9.8