Skip to content

Commit 9c7a818

Browse files
committed
Big progress on refactoring
1 parent e213341 commit 9c7a818

54 files changed

Lines changed: 2608 additions & 1617 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/flowmapper/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55
"Flow",
66
"Flowmap",
77
"flowmapper",
8-
"OutputFormat",
8+
"Match",
9+
"MatchCondition",
10+
"NormalizedFlow",
911
"UnitField",
1012
)
1113

1214
__version__ = "0.4.2"
1315

1416
from flowmapper.cas import CASField
1517
from flowmapper.context import ContextField
16-
from flowmapper.flow import Flow
18+
from flowmapper.domain import Flow, Match, MatchCondition, NormalizedFlow
1719
from flowmapper.flowmap import Flowmap
18-
from flowmapper.main import OutputFormat, flowmapper
20+
from flowmapper.main import flowmapper
1921
from flowmapper.unit import UnitField

src/flowmapper/cas.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1+
import re
12
from collections import UserString
23
from functools import cached_property
3-
import re
4-
54

6-
valid_cas = re.compile(r"^\s*[0-9]{3,7}-[0-9]{2}-[0-9]{1}\s*$")
5+
valid_cas = re.compile(r"^\s*[0-9]{2,7}-[0-9]{2}-[0-9]{1}\s*$")
76

87

98
class CASField(UserString):
109
def __init__(self, string: str):
1110
if not isinstance(string, (str, UserString)):
12-
raise TypeError(f"CASField takes only `str`, but got {type(string)} for {string}")
13-
if not valid_cas.search(string):
14-
raise ValueError(f"Given input is not valid CAS formatting: {string}")
15-
super().__init__(string)
11+
raise TypeError(
12+
f"CASField takes only `str`, but got {type(string)} for {string}"
13+
)
14+
if not valid_cas.search(str(string)):
15+
raise ValueError(f"Given input is not valid CAS formatting: '{string}'")
16+
super().__init__(str(string))
1617

1718
@staticmethod
1819
def from_string(string: str | None) -> "CASField | None":
1920
"""Returns `None` if CAS number is invalid"""
20-
if string is None:
21+
if string is None or not isinstance(string, (str, UserString)):
2122
return None
2223
new_cas = CASField(string.strip().lstrip("0").strip())
2324
if not new_cas.valid():
@@ -52,5 +53,6 @@ def check_digit_expected(self):
5253
return result
5354

5455
def valid(self):
55-
return self.digits[-1] == self.check_digit_expected
56-
56+
return (self.digits[-1] == self.check_digit_expected) and bool(
57+
valid_cas.search(self.data)
58+
)

src/flowmapper/cli.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import importlib.metadata
22
from pathlib import Path
3-
4-
import typer
53
from typing import Annotated
4+
65
import structlog
6+
import typer
77

8-
from flowmapper.extraction import ecospold2_biosphere_extractor, simapro_csv_biosphere_extractor
9-
from flowmapper.main import OutputFormat, flowmapper
8+
from flowmapper.extraction import (
9+
ecospold2_biosphere_extractor,
10+
simapro_csv_biosphere_extractor,
11+
)
12+
from flowmapper.main import flowmapper
1013

1114
try:
1215
from pyinstrument import Profiler
@@ -44,10 +47,6 @@ def map(
4447
output_dir: Annotated[
4548
Path, typer.Option(help="Directory to save mapping and diagnostics files")
4649
] = Path("."),
47-
format: Annotated[
48-
OutputFormat,
49-
typer.Option(help="Mapping file output format", case_sensitive=False),
50-
] = "randonneur",
5150
default_transformations: Annotated[
5251
bool, typer.Option(help="Include default context and unit transformations?")
5352
] = True,
@@ -92,7 +91,7 @@ def map(
9291
"location": "location",
9392
},
9493
}
95-
94+
9695
if profile:
9796
if Profiler is None:
9897
raise ImportError("`pyinstrument` not installed")
@@ -106,9 +105,14 @@ def map(
106105
mapping_target=generic_mapping,
107106
source_id=source.stem,
108107
target_id=target.stem,
109-
contributors=[{"title": "flowmapper", "roles": ["author"], "path": "https://github.com/cmutel/flowmapper"}],
108+
contributors=[
109+
{
110+
"title": "flowmapper",
111+
"roles": ["author"],
112+
"path": "https://github.com/cmutel/flowmapper",
113+
}
114+
],
110115
output_dir=output_dir,
111-
format=format,
112116
default_transformations=default_transformations,
113117
transformations=transformations,
114118
unmatched_source=unmatched_source,

src/flowmapper/context.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
from typing import Self, Any
2-
1+
from typing import Any, Self
32

43
MISSING_VALUES = {
54
"",
@@ -28,13 +27,18 @@ def normalize(self, obj: Any | None = None, mapping: dict | None = None) -> Self
2827

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

31-
while intermediate[-1] in MISSING_VALUES:
30+
while intermediate and intermediate[-1] in MISSING_VALUES:
31+
if len(intermediate) == 1:
32+
break
3233
intermediate = intermediate[:-1]
3334

34-
# TODO: Apply mapping
35-
3635
return type(self)(value=tuple(intermediate))
3736

37+
def as_tuple(self) -> tuple | str:
38+
if isinstance(self.value, str):
39+
return self.value
40+
return tuple(self.value)
41+
3842
def export_as_string(self, join_character: str = "✂️"):
3943
if isinstance(self.value, (list, tuple)):
4044
return join_character.join(self.value)
@@ -53,7 +57,7 @@ def __eq__(self, other: Any) -> bool:
5357
return False
5458

5559
def __repr__(self):
56-
return f"ContextField: {self.value}"
60+
return str(self.value)
5761

5862
def __bool__(self):
5963
return bool(self.value)

0 commit comments

Comments
 (0)