Skip to content

Commit e213341

Browse files
committed
Update ContextField
1 parent 8f691aa commit e213341

2 files changed

Lines changed: 436 additions & 33 deletions

File tree

src/flowmapper/context.py

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from collections.abc import Iterable
2-
from typing import Any
1+
from typing import Self, Any
2+
33

44
MISSING_VALUES = {
55
"",
@@ -11,71 +11,65 @@
1111
}
1212

1313

14-
class ContextField(Iterable):
15-
def __init__(self, original: Any, transformed: Any = None):
16-
self.original = original
17-
self.transformed = transformed or original
18-
self.normalized = self.normalize(self.transformed)
14+
class ContextField:
15+
def __init__(self, value: str | list[str] | tuple[str]):
16+
self.value = value
1917

20-
def normalize(self, value: Any) -> tuple[str, ...]:
18+
def normalize(self, obj: Any | None = None, mapping: dict | None = None) -> Self:
19+
value = obj or self.value
2120
if isinstance(value, (tuple, list)):
2221
intermediate = list(value)
2322
elif isinstance(value, str) and "/" in value:
2423
intermediate = list(value.split("/"))
2524
elif isinstance(value, str):
2625
intermediate = [value]
2726
else:
28-
raise ValueError(f"Can't understand input context {value}")
27+
raise ValueError(f"Can't understand input context {self.value}")
2928

3029
intermediate = [elem.lower().strip() for elem in intermediate]
3130

32-
if intermediate[-1] in MISSING_VALUES:
31+
while intermediate[-1] in MISSING_VALUES:
3332
intermediate = intermediate[:-1]
3433

35-
return tuple(intermediate)
34+
# TODO: Apply mapping
3635

37-
def export_as_string(self):
38-
if isinstance(self.original, str):
39-
return self.original
40-
elif isinstance(self.original, (list, tuple)):
41-
return "✂️".join(self.original)
42-
else:
43-
# Only reachable by manually changing `self.original`
44-
raise ValueError("Invalid context data")
36+
return type(self)(value=tuple(intermediate))
37+
38+
def export_as_string(self, join_character: str = "✂️"):
39+
if isinstance(self.value, (list, tuple)):
40+
return join_character.join(self.value)
41+
return self.value
4542

4643
def __iter__(self):
47-
return iter(self.normalized)
44+
return iter(self.value)
4845

49-
def __eq__(self, other):
46+
def __eq__(self, other: Any) -> bool:
5047
if self and other and isinstance(other, ContextField):
51-
return self.original and self.normalized == other.normalized
48+
return self.value == other.value
5249
else:
5350
try:
54-
normalized_other = self.normalize(other)
55-
return (self.normalized == normalized_other) or (
56-
self.original == normalized_other
57-
)
51+
return self.value == self.normalize(other).value
5852
except ValueError:
5953
return False
6054

6155
def __repr__(self):
62-
return f"ContextField: '{self.original}' -> '{self.normalized}'"
56+
return f"ContextField: {self.value}"
6357

6458
def __bool__(self):
65-
return bool(self.normalized)
59+
return bool(self.value)
6660

6761
def __hash__(self):
68-
return hash(self.normalized)
62+
return hash(self.value)
6963

70-
def __contains__(self, other):
71-
"""This context is more generic than the `other` context.
64+
def __contains__(self, other: Any) -> bool:
65+
"""`self` context is more generic than the `other` context.
7266
7367
```python
74-
Context("a/b/c") in Context("a/b")
68+
Context("a/b") in Context("a/b/c")
7569
>>> True
7670
```
7771
7872
"""
7973
if not isinstance(other, ContextField):
8074
return False
81-
return self.normalized == other.normalized[: len(self.normalized)]
75+
return self.value == other.value[: len(self.value)]

0 commit comments

Comments
 (0)