cenit 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.
cenit/__init__.py ADDED
@@ -0,0 +1,169 @@
1
+ """Versioned CENIT compensation codes.
2
+
3
+ Each release bundles one immutable snapshot of the official Banrep table. The
4
+ data is compiled into the package, so lookups are pure, offline and
5
+ deterministic. Publishing a new package version is how new data is released.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ from collections.abc import Callable, Mapping
11
+ from datetime import datetime
12
+ from typing import TypeVar, overload
13
+
14
+ from rapidfuzz import distance, fuzz
15
+
16
+ from . import _data
17
+ from ._types import (
18
+ AccountNumberLengthScope,
19
+ AccountType,
20
+ EntityType,
21
+ Participant,
22
+ Source,
23
+ Totals,
24
+ normalize_name,
25
+ )
26
+ from .view import footnote_definitions
27
+
28
+ _MISSING = object()
29
+ _T = TypeVar("_T")
30
+
31
+
32
+ class ParticipantNotFound(KeyError):
33
+ """Raised when a lookup finds no matching participant."""
34
+
35
+
36
+ __all__ = [
37
+ "AccountNumberLengthScope",
38
+ "AccountType",
39
+ "EntityType",
40
+ "Participant",
41
+ "ParticipantNotFound",
42
+ "all_records",
43
+ "footnote_definitions",
44
+ "footnotes",
45
+ "get_by_code",
46
+ "get_by_name",
47
+ "list_debit_capable_participants",
48
+ "list_participants_by_entity_type",
49
+ "source",
50
+ "totals",
51
+ "version"
52
+ ]
53
+
54
+
55
+ def _code_text(code: str | int) -> str:
56
+ if isinstance(code, bool):
57
+ raise TypeError("code must be a string or integer, not bool")
58
+ if isinstance(code, int):
59
+ return str(code)
60
+ if isinstance(code, str):
61
+ return code.strip()
62
+ raise TypeError("code must be a string or integer")
63
+
64
+
65
+ def _jaro_winkler(left: str, right: str) -> float:
66
+ return distance.JaroWinkler.similarity(left, right) * 100
67
+
68
+
69
+ def _best_name_match(
70
+ key: str, scorer: Callable[[str, str], float], cutoff: float
71
+ ) -> Participant | None:
72
+ best_score = -1.0
73
+ best_row = 0
74
+ best: Participant | None = None
75
+ for normalized, record in _data.CODES_BY_NAME.items():
76
+ score = scorer(key, normalized)
77
+ if score < cutoff:
78
+ continue
79
+ if score > best_score or (score == best_score and record.row_number < best_row):
80
+ best_score = score
81
+ best_row = record.row_number
82
+ best = record
83
+ return best
84
+
85
+
86
+ @overload
87
+ def get_by_code(code: str | int) -> Participant: ...
88
+
89
+
90
+ @overload
91
+ def get_by_code(code: str | int, default: _T) -> Participant | _T: ...
92
+
93
+
94
+ def get_by_code(code: str | int, default: object = _MISSING) -> Participant | object:
95
+ """Return the record for a CENIT code, raising unless a default is given."""
96
+ record = _data.CODES_BY_CENIT_CODE.get(_code_text(code))
97
+ if record is None:
98
+ if default is not _MISSING:
99
+ return default
100
+ raise ParticipantNotFound(f"unknown CENIT code: {code!r}")
101
+ return record
102
+
103
+
104
+ @overload
105
+ def get_by_name(name: str) -> Participant: ...
106
+
107
+
108
+ @overload
109
+ def get_by_name(name: str, default: _T) -> Participant | _T: ...
110
+
111
+
112
+ def get_by_name(name: str, default: object = _MISSING) -> Participant | object:
113
+ """Return the closest normalized name match, raising unless a default is given."""
114
+ key = normalize_name(name)
115
+ record = _data.CODES_BY_NAME.get(key)
116
+ if record is None and key:
117
+ record = _best_name_match(key, fuzz.WRatio, 90)
118
+ if record is None and len(key) >= 4:
119
+ record = _best_name_match(key, _jaro_winkler, 69)
120
+ if record is None:
121
+ if default is not _MISSING:
122
+ return default
123
+ raise ParticipantNotFound(f"unknown participant name: {name!r}")
124
+ return record
125
+
126
+
127
+ def all_records() -> tuple[Participant, ...]:
128
+ """Return every record in table order."""
129
+ return _data.RECORDS
130
+
131
+
132
+ def footnotes() -> Mapping[str, str]:
133
+ """Return the official footnote markers and their English meanings."""
134
+ return _data.FOOTNOTES
135
+
136
+
137
+ def list_participants_by_entity_type(entity_type: str | EntityType) -> tuple[Participant, ...]:
138
+ """Return every participant with the given English entity type."""
139
+ try:
140
+ wanted = EntityType(entity_type)
141
+ except ValueError:
142
+ raise ValueError(f"unknown entity type: {entity_type!r}") from None
143
+ return tuple(record for record in _data.RECORDS if wanted in record.entity_types)
144
+
145
+
146
+ def list_debit_capable_participants() -> tuple[Participant, ...]:
147
+ """Return every participant that supports debit transactions."""
148
+ return tuple(record for record in _data.RECORDS if record.supports_debit_transactions)
149
+
150
+
151
+ def totals() -> Totals:
152
+ """Return the totals row bundled with this release."""
153
+ return _data.TOTALS
154
+
155
+
156
+ def version() -> str:
157
+ """Return the package version that defines this dataset snapshot."""
158
+ return _data.VERSION
159
+
160
+
161
+ def source() -> Source:
162
+ """Return the provenance of this dataset snapshot."""
163
+ return Source(
164
+ url=_data.SOURCE_URL,
165
+ sha256=_data.SOURCE_SHA256,
166
+ document_date=_data.SOURCE_DATE,
167
+ fetched_at=datetime.fromisoformat(_data.SOURCE_FETCHED_AT),
168
+ schema_version=_data.SCHEMA_VERSION,
169
+ )