tha-map-runner 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tha_map_runner/__init__.py +7 -0
- tha_map_runner/errors.py +2 -0
- tha_map_runner/mapper.py +69 -0
- tha_map_runner/paths.py +8 -0
- tha_map_runner-0.1.0.dist-info/METADATA +135 -0
- tha_map_runner-0.1.0.dist-info/RECORD +8 -0
- tha_map_runner-0.1.0.dist-info/WHEEL +4 -0
- tha_map_runner-0.1.0.dist-info/licenses/LICENSE +21 -0
tha_map_runner/errors.py
ADDED
tha_map_runner/mapper.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import warnings
|
|
2
|
+
|
|
3
|
+
from .errors import MapperError
|
|
4
|
+
from .paths import resolve_path
|
|
5
|
+
|
|
6
|
+
_ON_NO_MATCH = {"skip", "error", "blank"}
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def enrich_rows(
|
|
10
|
+
rows: list[dict],
|
|
11
|
+
source: list[dict],
|
|
12
|
+
mapping: dict[str, str],
|
|
13
|
+
row_key: str,
|
|
14
|
+
source_key: str,
|
|
15
|
+
*,
|
|
16
|
+
on_no_match: str = "skip",
|
|
17
|
+
allow_empty_source: bool = False,
|
|
18
|
+
skip_statuses: list[str] | None = None,
|
|
19
|
+
) -> list[dict]:
|
|
20
|
+
if on_no_match not in _ON_NO_MATCH:
|
|
21
|
+
raise MapperError(f"on_no_match must be one of {sorted(_ON_NO_MATCH)}, got {on_no_match!r}")
|
|
22
|
+
|
|
23
|
+
statuses_to_skip = set(skip_statuses if skip_statuses is not None else ["error", "warning"])
|
|
24
|
+
|
|
25
|
+
if not source:
|
|
26
|
+
if allow_empty_source:
|
|
27
|
+
return [row.copy() for row in rows]
|
|
28
|
+
raise MapperError("source is empty — pass allow_empty_source=True to allow this")
|
|
29
|
+
|
|
30
|
+
index: dict[object, dict] = {}
|
|
31
|
+
for item in source:
|
|
32
|
+
key = item.get(source_key)
|
|
33
|
+
if key in index:
|
|
34
|
+
warnings.warn(
|
|
35
|
+
f"Duplicate {source_key!r} value {key!r} in source; using last occurrence",
|
|
36
|
+
stacklevel=2,
|
|
37
|
+
)
|
|
38
|
+
index[key] = item
|
|
39
|
+
|
|
40
|
+
output: list[dict] = []
|
|
41
|
+
for row in rows:
|
|
42
|
+
if row.get("row status") in statuses_to_skip:
|
|
43
|
+
output.append(row.copy())
|
|
44
|
+
continue
|
|
45
|
+
|
|
46
|
+
key_val = row.get(row_key)
|
|
47
|
+
match = index.get(key_val)
|
|
48
|
+
|
|
49
|
+
if match is None:
|
|
50
|
+
new_row = row.copy()
|
|
51
|
+
if on_no_match == "error":
|
|
52
|
+
new_row["row status"] = "error"
|
|
53
|
+
new_row["message"] = f"No match for {row_key}={key_val!r}"
|
|
54
|
+
for field in mapping:
|
|
55
|
+
new_row[field] = ""
|
|
56
|
+
elif on_no_match == "blank":
|
|
57
|
+
for field in mapping:
|
|
58
|
+
new_row[field] = ""
|
|
59
|
+
output.append(new_row)
|
|
60
|
+
continue
|
|
61
|
+
|
|
62
|
+
new_row = row.copy()
|
|
63
|
+
for field, path in mapping.items():
|
|
64
|
+
value = resolve_path(match, path)
|
|
65
|
+
new_row[field] = "" if value is None else value
|
|
66
|
+
|
|
67
|
+
output.append(new_row)
|
|
68
|
+
|
|
69
|
+
return output
|
tha_map_runner/paths.py
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tha-map-runner
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Join JSON responses into CSV-style rows with dotted-path projection — a Tabular Helper API.
|
|
5
|
+
Project-URL: Homepage, https://github.com/tha-guy-nate/tha-map-runner
|
|
6
|
+
Project-URL: Issues, https://github.com/tha-guy-nate/tha-map-runner/issues
|
|
7
|
+
Author: Nate Wright
|
|
8
|
+
License: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Topic :: Utilities
|
|
18
|
+
Requires-Python: >=3.10
|
|
19
|
+
Provides-Extra: dev
|
|
20
|
+
Requires-Dist: mypy>=1.10; extra == 'dev'
|
|
21
|
+
Requires-Dist: pytest>=8; extra == 'dev'
|
|
22
|
+
Requires-Dist: ruff>=0.5; extra == 'dev'
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
|
|
25
|
+
# tha-map-runner
|
|
26
|
+
|
|
27
|
+
[](https://github.com/tha-guy-nate/tha-map-runner/actions/workflows/ci.yml)
|
|
28
|
+
|
|
29
|
+
A small Python library that joins a list of row dicts with a lookup source on a key, projecting values into flat row columns via a mapping config.
|
|
30
|
+
|
|
31
|
+
Think "left join between rows and a lookup source, with dotted-path projection on the source side."
|
|
32
|
+
|
|
33
|
+
## Install
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pip install tha-map-runner
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Quick start
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
from tha_map_runner import enrich_rows
|
|
43
|
+
|
|
44
|
+
rows = [
|
|
45
|
+
{"Org BK": "school-001", "Start Date": "08/15"},
|
|
46
|
+
{"Org BK": "school-002", "Start Date": "08/16"},
|
|
47
|
+
]
|
|
48
|
+
|
|
49
|
+
api_response = [
|
|
50
|
+
{"sourcedId": "school-001", "name": "Lincoln Elementary", "parent": {"sourcedId": "dist-A"}},
|
|
51
|
+
{"sourcedId": "school-002", "name": "Roosevelt Middle", "parent": {"sourcedId": "dist-A"}},
|
|
52
|
+
]
|
|
53
|
+
|
|
54
|
+
enriched = enrich_rows(
|
|
55
|
+
rows=rows,
|
|
56
|
+
source=api_response,
|
|
57
|
+
mapping={
|
|
58
|
+
"Org Name": "name",
|
|
59
|
+
"Parent BK": "parent.sourcedId",
|
|
60
|
+
},
|
|
61
|
+
row_key="Org BK",
|
|
62
|
+
source_key="sourcedId",
|
|
63
|
+
)
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## How it works
|
|
67
|
+
|
|
68
|
+
1. Builds an index of `source` on `source_key` — O(n+m), no nested loops
|
|
69
|
+
2. For each row, looks up a match by `row[row_key]`
|
|
70
|
+
3. Walks dotted paths (`"parent.sourcedId"`) into the matched source entry
|
|
71
|
+
4. Projects resolved values into new columns on a copy of the row
|
|
72
|
+
5. Returns a new list — input is never mutated
|
|
73
|
+
|
|
74
|
+
Rows whose `row status` is in `skip_statuses` are passed through unchanged.
|
|
75
|
+
|
|
76
|
+
## API
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
enrich_rows(
|
|
80
|
+
rows, # list of row dicts
|
|
81
|
+
source, # list of dicts to join against
|
|
82
|
+
mapping, # {"output_column": "dotted.path"} — callable values planned
|
|
83
|
+
row_key, # column name in rows to match on
|
|
84
|
+
source_key, # field in source to match on
|
|
85
|
+
*,
|
|
86
|
+
on_no_match="skip", # "skip" | "error" | "blank"
|
|
87
|
+
allow_empty_source=False, # if True, empty source is not an error
|
|
88
|
+
skip_statuses=["error", "warning"],# rows with these statuses are passed through
|
|
89
|
+
) -> list[dict]
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### `on_no_match`
|
|
93
|
+
|
|
94
|
+
| Value | Behaviour |
|
|
95
|
+
|---|---|
|
|
96
|
+
| `"skip"` | Row is returned unchanged — no new columns added |
|
|
97
|
+
| `"error"` | `row status="error"`, `message=...`, mapping columns set to `""` |
|
|
98
|
+
| `"blank"` | Mapping columns set to `""`, row status untouched |
|
|
99
|
+
|
|
100
|
+
### `skip_statuses`
|
|
101
|
+
|
|
102
|
+
By default, rows already marked `row status="error"` or `row status="warning"` are passed through without processing. Override with any list:
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
enrich_rows(..., skip_statuses=["error"]) # only skip errors
|
|
106
|
+
enrich_rows(..., skip_statuses=["error", "pending"]) # custom statuses
|
|
107
|
+
enrich_rows(..., skip_statuses=[]) # process every row regardless
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Composing with `tha-csv-runner`
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
from tha_csv_runner import ThaCSV
|
|
114
|
+
from tha_map_runner import enrich_rows
|
|
115
|
+
import requests
|
|
116
|
+
|
|
117
|
+
runner = ThaCSV()
|
|
118
|
+
runner.read("Step 1 of 2", "input.csv", ["Org BK"])
|
|
119
|
+
|
|
120
|
+
api_response = requests.get(api_url).json()
|
|
121
|
+
|
|
122
|
+
runner.rows = enrich_rows(
|
|
123
|
+
rows=runner.rows,
|
|
124
|
+
source=api_response,
|
|
125
|
+
mapping={"Org Name": "name", "District": "parent.sourcedId"},
|
|
126
|
+
row_key="Org BK",
|
|
127
|
+
source_key="sourcedId",
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
runner.write("Step 2 of 2", "output.csv")
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## License
|
|
134
|
+
|
|
135
|
+
MIT
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
tha_map_runner/__init__.py,sha256=pt7XYPQBdwuNQ-dSJ8AjldTHuqncbyFhSaNxjnDQfIE,220
|
|
2
|
+
tha_map_runner/errors.py,sha256=te2Nm0mL_RP3rg-HAccZYRcKlfSF7qkY_JUTXMTPuio,81
|
|
3
|
+
tha_map_runner/mapper.py,sha256=uB4Ftfb2Kktt6wtO1Z86PJ3zAe3dYeIFqF_bkRbApwY,2073
|
|
4
|
+
tha_map_runner/paths.py,sha256=UG-NoqwcfZUFaQahdLf7fINX-avXk-b7o4cigWL0bGI,264
|
|
5
|
+
tha_map_runner-0.1.0.dist-info/METADATA,sha256=0lLOjYQP5Ib9klmKMfeZ2nwaeM5qwvvbelE1gAlGNns,4290
|
|
6
|
+
tha_map_runner-0.1.0.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
|
|
7
|
+
tha_map_runner-0.1.0.dist-info/licenses/LICENSE,sha256=bCtVwn7MJmnj7wfasPJG_OXozVSo1RGg1FXERKSv6ps,1070
|
|
8
|
+
tha_map_runner-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nathan Wright
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|