-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAttribute.py
57 lines (45 loc) · 1.62 KB
/
Attribute.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
class Attribute:
def __init__(self, attribute_literal: str) -> None:
self._attribute_literal = attribute_literal
@property
def has_key(self) -> bool:
return hasattr(self, "_key_as_str")
@property
def attribute_literal(self) -> str:
return self._attribute_literal
@property
def value(self) -> str:
return self._value_as_str
def dump(self):
print(f"{self.attribute_literal=}")
print(f"{self.value=}")
print(f"{self.value_literal=}")
if self.has_key:
print(f"{self.key=}")
print(f"{self.key_literal=}")
class AttributeWithKey(Attribute):
def __init__(
self, attribute_literal: str, key_literal: str, value_literal: str
) -> None:
super().__init__(attribute_literal)
if value_literal[0] in ["'", '"'] and value_literal[0] == value_literal[-1]:
self._value_as_str = value_literal[1:-1]
else:
self._value_as_str = value_literal
self.key_literal = key_literal
self._key_as_str = key_literal.lower()
self.value_literal = value_literal
@property
def key(self) -> str:
return self._key_as_str
class AttributeWithoutKey(Attribute):
def __init__(self, attribute_literal: str) -> None:
super().__init__(attribute_literal)
if (
attribute_literal[0] in ["'", '"']
and attribute_literal[0] == attribute_literal[-1]
):
self._value_as_str = attribute_literal[1:-1]
else:
self._value_as_str = attribute_literal
self.value_literal = attribute_literal