Skip to content

Commit e977f9d

Browse files
committed
Allow grouping keys to be specified for v2 remap
1 parent e5f947c commit e977f9d

1 file changed

Lines changed: 19 additions & 4 deletions

File tree

scripts/remap.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,33 @@
22
import sys
33
from pathlib import Path
44
from typing import Any
5+
from typing import Literal
6+
from typing import TypedDict
57

68
from boltons.iterutils import bucketize
79

810

11+
class EntryV1(TypedDict):
12+
name: str
13+
short_name: str
14+
bic: str
15+
bank_code: str
16+
primary: bool
17+
checksum_algo: str
18+
19+
920
def convert_to_v2(
10-
data: list[dict[str, Any]],
21+
data: list[EntryV1],
1122
expand_from: str = "bank_codes",
1223
expand_into: str = "bank_code",
24+
groupby: list[Literal["name", "short_name", "bic", "bank_code"]] | None = None,
1325
) -> dict[str, Any]:
14-
buckets = bucketize(
15-
data, lambda b: f"{b['bic']}::{b['name'].upper()}::{b.get('checksum_algo', '')}"
16-
)
26+
groupby = groupby or ["bic", "name"]
27+
28+
def make_key(e: EntryV1) -> str:
29+
return "::".join(e[group] for group in groupby) + "::" + e.get("checksum_algo", "")
30+
31+
buckets = bucketize(data, make_key)
1732

1833
entries: list[dict[str, Any]] = []
1934
for _, banks in buckets.items():

0 commit comments

Comments
 (0)