Coverage for value_object_pattern/usables/identifiers/world/utils/__init__.py: 69.53%
100 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-11-21 14:31 +0000
« prev ^ index » next coverage.py v7.15.4, created at 2026-11-21 14:31 +0000
1from functools import lru_cache 1abcdefghij
2from importlib.resources import files 1abcdefghij
5@lru_cache(maxsize=1) 1abcdefghij
6def get_bcp47_language_subtags() -> tuple[str, ...]: 1abcdefghij
7 """
8 Get registered BCP 47 language subtags.
10 Returns:
11 tuple[str, ...]: Registered language subtags in lowercase.
12 """
13 lines = (
14 files('value_object_pattern.usables.identifiers.world.utils')
15 .joinpath('bcp47_language_subtags.txt')
16 .read_text(encoding='utf-8')
17 .splitlines()
18 )
20 return tuple(
21 stripped_line.lower() for line in lines if not line.startswith('#') and (stripped_line := line.strip())
22 )
25@lru_cache(maxsize=1) 1abcdefghij
26def get_bcp47_extlang_to_prefix_mapping() -> dict[str, str]: 1abcdefghij
27 """
28 Get registered BCP 47 extended-language subtags and their prefixes.
30 Returns:
31 dict[str, str]: Extended-language subtags mapped to their primary-language prefixes.
32 """
33 lines = (
34 files('value_object_pattern.usables.identifiers.world.utils')
35 .joinpath('bcp47_extlang_subtags.txt')
36 .read_text(encoding='utf-8')
37 .splitlines()
38 )
40 extlang_to_prefix = {}
41 for line in lines:
42 if not line.startswith('#') and (stripped_line := line.strip().lower()):
43 extlang, prefix = stripped_line.split(maxsplit=1)
44 extlang_to_prefix[extlang] = prefix
46 return extlang_to_prefix
49@lru_cache(maxsize=1) 1abcdefghij
50def get_bcp47_script_subtags() -> tuple[str, ...]: 1abcdefghij
51 """
52 Get registered BCP 47 script subtags.
54 Returns:
55 tuple[str, ...]: Registered script subtags in lowercase.
56 """
57 lines = (
58 files('value_object_pattern.usables.identifiers.world.utils')
59 .joinpath('bcp47_script_subtags.txt')
60 .read_text(encoding='utf-8')
61 .splitlines()
62 )
64 return tuple(
65 stripped_line.lower() for line in lines if not line.startswith('#') and (stripped_line := line.strip())
66 )
69@lru_cache(maxsize=1) 1abcdefghij
70def get_bcp47_region_subtags() -> tuple[str, ...]: 1abcdefghij
71 """
72 Get registered BCP 47 region subtags.
74 Returns:
75 tuple[str, ...]: Registered region subtags in lowercase.
76 """
77 lines = (
78 files('value_object_pattern.usables.identifiers.world.utils')
79 .joinpath('bcp47_region_subtags.txt')
80 .read_text(encoding='utf-8')
81 .splitlines()
82 )
84 return tuple(
85 stripped_line.lower() for line in lines if not line.startswith('#') and (stripped_line := line.strip())
86 )
89@lru_cache(maxsize=1) 1abcdefghij
90def get_bcp47_variant_subtags() -> tuple[str, ...]: 1abcdefghij
91 """
92 Get registered BCP 47 variant subtags.
94 Returns:
95 tuple[str, ...]: Registered variant subtags in lowercase.
96 """
97 lines = (
98 files('value_object_pattern.usables.identifiers.world.utils')
99 .joinpath('bcp47_variant_subtags.txt')
100 .read_text(encoding='utf-8')
101 .splitlines()
102 )
104 return tuple(
105 stripped_line.lower() for line in lines if not line.startswith('#') and (stripped_line := line.strip())
106 )
109@lru_cache(maxsize=1) 1abcdefghij
110def get_bcp47_grandfathered_tag_mapping() -> dict[str, str]: 1abcdefghij
111 """
112 Get registered grandfathered BCP 47 tags.
114 Returns:
115 dict[str, str]: Lowercase tags mapped to their registered casing.
116 """
117 lines = (
118 files('value_object_pattern.usables.identifiers.world.utils')
119 .joinpath('bcp47_grandfathered_tags.txt')
120 .read_text(encoding='utf-8')
121 .splitlines()
122 )
124 tag_mapping = {}
125 for line in lines:
126 if not line.startswith('#') and (stripped_line := line.strip()):
127 tag_mapping[stripped_line.lower()] = stripped_line
129 return tag_mapping
132@lru_cache(maxsize=1) 1abcdefghij
133def get_bcp47_redundant_tag_mapping() -> dict[str, str]: 1abcdefghij
134 """
135 Get registered redundant BCP 47 tags.
137 Returns:
138 dict[str, str]: Lowercase tags mapped to their registered casing.
139 """
140 lines = (
141 files('value_object_pattern.usables.identifiers.world.utils')
142 .joinpath('bcp47_redundant_tags.txt')
143 .read_text(encoding='utf-8')
144 .splitlines()
145 )
147 tag_mapping = {}
148 for line in lines:
149 if not line.startswith('#') and (stripped_line := line.strip()):
150 tag_mapping[stripped_line.lower()] = stripped_line
152 return tag_mapping
155@lru_cache(maxsize=1) 1abcdefghij
156def get_iso3166_alpha2_codes() -> tuple[str, ...]: 1abcdefghij
157 """
158 Get ISO 3166-1 alpha-2 country codes.
160 Returns:
161 tuple[str, ...]: The ISO 3166-1 alpha-2 country codes in uppercase.
163 References:
164 ISO 3166-1 alpha-2 codes: https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes
165 """
166 lines = ( 1abcdefghij
167 files('value_object_pattern.usables.identifiers.world.utils')
168 .joinpath('iso3166_alpha2_codes.txt')
169 .read_text(encoding='utf-8')
170 .splitlines()
171 )
173 return tuple(line for line in lines if not line.startswith('#') and (_line := line.strip().upper())) 1abcdefghij
176@lru_cache(maxsize=1) 1abcdefghij
177def get_iso3166_alpha2_to_alpha3_mapping() -> tuple[dict[str, str], dict[str, str]]: 1abcdefghij
178 """
179 Get a mapping of ISO 3166-1 alpha-2 codes to alpha-3 codes and vice versa.
181 Returns:
182 tuple[dict[str, str], dict[str, str]]: A tuple containing two dictionaries, one for alpha-2 to alpha-3 mapping
183 and another for alpha-3 to alpha-2 mapping.
185 References:
186 ISO 3166-1 alpha-2 to alpha-3 mapping: https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes
187 """
188 lines = ( 1abcdefghij
189 files('value_object_pattern.usables.identifiers.world.utils')
190 .joinpath('iso3166_alpha2_to_alpha3_mapping.txt')
191 .read_text(encoding='utf-8')
192 .splitlines()
193 )
195 alpha2_to_alpha3 = {} 1abcdefghij
196 alpha3_to_alpha2 = {} 1abcdefghij
197 for line in lines: 1abcdefghij
198 if not line.startswith('#') and (stripped_line := line.strip()): 1abcdefghij
199 alpha2, alpha3 = stripped_line.upper().split(sep=', ') 1abcdefghij
200 alpha2_to_alpha3[alpha2] = alpha3 1abcdefghij
201 alpha3_to_alpha2[alpha3] = alpha2 1abcdefghij
203 return alpha2_to_alpha3, alpha3_to_alpha2 1abcdefghij
206@lru_cache(maxsize=1) 1abcdefghij
207def get_iso3166_alpha2_to_numeric_mapping() -> tuple[dict[str, int], dict[int, str]]: 1abcdefghij
208 """
209 Get a mapping of ISO 3166-1 alpha-2 codes to numeric codes and vice versa.
211 Returns:
212 tuple[dict[str, int], dict[int, str]]: A tuple containing two dictionaries, one for alpha-2 to numeric mapping
213 and another for numeric to alpha-2 mapping.
215 References:
216 ISO 3166-1 alpha-2 to numeric mapping: https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes
217 """
218 lines = ( 1abcdefghij
219 files('value_object_pattern.usables.identifiers.world.utils')
220 .joinpath('iso3166_alpha2_to_numeric_mapping.txt')
221 .read_text(encoding='utf-8')
222 .splitlines()
223 )
225 alpha2_to_numeric = {} 1abcdefghij
226 numeric_to_alpha2 = {} 1abcdefghij
227 for line in lines: 1abcdefghij
228 if not line.startswith('#') and (stripped_line := line.strip()): 1abcdefghij
229 alpha2, numeric = stripped_line.upper().split(sep=', ') 1abcdefghij
230 alpha2_to_numeric[alpha2] = int(numeric) 1abcdefghij
231 numeric_to_alpha2[int(numeric)] = alpha2 1abcdefghij
233 return alpha2_to_numeric, numeric_to_alpha2 1abcdefghij
236@lru_cache(maxsize=1) 1abcdefghij
237def get_iso3166_alpha2_to_phone_code_mapping() -> tuple[dict[str, str], dict[str, str]]: 1abcdefghij
238 """
239 Get a mapping of ISO 3166-1 alpha-2 codes to phone codes and vice versa.
241 Returns:
242 tuple[dict[str, str], dict[str, str]]: A tuple containing two dictionaries, one for alpha-2 to phone code
243 mapping and another for phone code to alpha-2 mapping.
245 References:
246 ISO 3166-1 alpha-2 to phone code mapping: https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes
247 """
248 lines = ( 1abcdefghij
249 files('value_object_pattern.usables.identifiers.world.utils')
250 .joinpath('iso3166_alpha2_to_phone_code_mapping.txt')
251 .read_text(encoding='utf-8')
252 .splitlines()
253 )
255 alpha2_to_phone_code = {} 1abcdefghij
256 phone_code_to_alpha2 = {} 1abcdefghij
257 for line in lines: 1abcdefghij
258 if not line.startswith('#') and (stripped_line := line.strip()): 1abcdefghij
259 alpha2, phone_code = stripped_line.upper().split(sep=', ') 1abcdefghij
260 alpha2_to_phone_code[alpha2] = phone_code 1abcdefghij
261 phone_code_to_alpha2[phone_code] = alpha2 1abcdefghij
263 return alpha2_to_phone_code, phone_code_to_alpha2 1abcdefghij
266@lru_cache(maxsize=1) 1abcdefghij
267def get_iso3166_alpha2_to_tld_mapping() -> tuple[dict[str, str], dict[str, str]]: 1abcdefghij
268 """
269 Get a mapping of ISO 3166-1 alpha-2 codes to TLDs and vice versa.
271 Returns:
272 tuple[dict[str, str], dict[str, str]]: A tuple containing two dictionaries, one for alpha-2 to TLD mapping
273 and another for TLD to alpha-2 mapping.
275 References:
276 ISO 3166-1 alpha-2 to TLD mapping: https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes
277 """
278 lines = ( 1abcdefghij
279 files('value_object_pattern.usables.identifiers.world.utils')
280 .joinpath('iso3166_alpha2_to_tld_mapping.txt')
281 .read_text(encoding='utf-8')
282 .splitlines()
283 )
285 alpha2_to_tld = {} 1abcdefghij
286 tld_to_alpha2 = {} 1abcdefghij
287 for line in lines: 1abcdefghij
288 if not line.startswith('#') and (stripped_line := line.strip()): 1abcdefghij
289 alpha2, tld = stripped_line.split(sep=', ') 1abcdefghij
290 alpha2, tld = alpha2.upper(), tld.lower() 1abcdefghij
292 alpha2_to_tld[alpha2] = tld 1abcdefghij
293 tld_to_alpha2[tld] = alpha2 1abcdefghij
295 return alpha2_to_tld, tld_to_alpha2 1abcdefghij
298@lru_cache(maxsize=1) 1abcdefghij
299def get_iso3166_alpha3_codes() -> tuple[str, ...]: 1abcdefghij
300 """
301 Get ISO 3166-1 alpha-3 country codes.
303 Returns:
304 tuple[str, ...]: The ISO 3166-1 alpha-3 country codes in uppercase.
306 References:
307 ISO 3166-1 alpha-3 codes: https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes
308 """
309 lines = ( 1abcdefghij
310 files('value_object_pattern.usables.identifiers.world.utils')
311 .joinpath('iso3166_alpha3_codes.txt')
312 .read_text(encoding='utf-8')
313 .splitlines()
314 )
316 return tuple(line for line in lines if not line.startswith('#') and (_line := line.strip().upper())) 1abcdefghij
319@lru_cache(maxsize=1) 1abcdefghij
320def get_iso3166_numeric_codes() -> tuple[int, ...]: 1abcdefghij
321 """
322 Get ISO 3166-1 numeric country codes.
324 Returns:
325 tuple[int, ...]: The ISO 3166-1 numeric country codes.
327 References:
328 ISO 3166-1 numeric codes: https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes
329 """
330 lines = ( 1abcdefghij
331 files('value_object_pattern.usables.identifiers.world.utils')
332 .joinpath('iso3166_numeric_codes.txt')
333 .read_text(encoding='utf-8')
334 .splitlines()
335 )
337 return tuple(int(line) for line in lines if not line.startswith('#') and (_line := line.strip())) 1abcdefghij