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

1from functools import lru_cache 1abcdefghij

2from importlib.resources import files 1abcdefghij

3 

4 

5@lru_cache(maxsize=1) 1abcdefghij

6def get_bcp47_language_subtags() -> tuple[str, ...]: 1abcdefghij

7 """ 

8 Get registered BCP 47 language subtags. 

9 

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 ) 

19 

20 return tuple( 

21 stripped_line.lower() for line in lines if not line.startswith('#') and (stripped_line := line.strip()) 

22 ) 

23 

24 

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. 

29 

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 ) 

39 

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 

45 

46 return extlang_to_prefix 

47 

48 

49@lru_cache(maxsize=1) 1abcdefghij

50def get_bcp47_script_subtags() -> tuple[str, ...]: 1abcdefghij

51 """ 

52 Get registered BCP 47 script subtags. 

53 

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 ) 

63 

64 return tuple( 

65 stripped_line.lower() for line in lines if not line.startswith('#') and (stripped_line := line.strip()) 

66 ) 

67 

68 

69@lru_cache(maxsize=1) 1abcdefghij

70def get_bcp47_region_subtags() -> tuple[str, ...]: 1abcdefghij

71 """ 

72 Get registered BCP 47 region subtags. 

73 

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 ) 

83 

84 return tuple( 

85 stripped_line.lower() for line in lines if not line.startswith('#') and (stripped_line := line.strip()) 

86 ) 

87 

88 

89@lru_cache(maxsize=1) 1abcdefghij

90def get_bcp47_variant_subtags() -> tuple[str, ...]: 1abcdefghij

91 """ 

92 Get registered BCP 47 variant subtags. 

93 

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 ) 

103 

104 return tuple( 

105 stripped_line.lower() for line in lines if not line.startswith('#') and (stripped_line := line.strip()) 

106 ) 

107 

108 

109@lru_cache(maxsize=1) 1abcdefghij

110def get_bcp47_grandfathered_tag_mapping() -> dict[str, str]: 1abcdefghij

111 """ 

112 Get registered grandfathered BCP 47 tags. 

113 

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 ) 

123 

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 

128 

129 return tag_mapping 

130 

131 

132@lru_cache(maxsize=1) 1abcdefghij

133def get_bcp47_redundant_tag_mapping() -> dict[str, str]: 1abcdefghij

134 """ 

135 Get registered redundant BCP 47 tags. 

136 

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 ) 

146 

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 

151 

152 return tag_mapping 

153 

154 

155@lru_cache(maxsize=1) 1abcdefghij

156def get_iso3166_alpha2_codes() -> tuple[str, ...]: 1abcdefghij

157 """ 

158 Get ISO 3166-1 alpha-2 country codes. 

159 

160 Returns: 

161 tuple[str, ...]: The ISO 3166-1 alpha-2 country codes in uppercase. 

162 

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 ) 

172 

173 return tuple(line for line in lines if not line.startswith('#') and (_line := line.strip().upper())) 1abcdefghij

174 

175 

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. 

180 

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. 

184 

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 ) 

194 

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

202 

203 return alpha2_to_alpha3, alpha3_to_alpha2 1abcdefghij

204 

205 

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. 

210 

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. 

214 

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 ) 

224 

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

232 

233 return alpha2_to_numeric, numeric_to_alpha2 1abcdefghij

234 

235 

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. 

240 

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. 

244 

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 ) 

254 

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

262 

263 return alpha2_to_phone_code, phone_code_to_alpha2 1abcdefghij

264 

265 

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. 

270 

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. 

274 

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 ) 

284 

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

291 

292 alpha2_to_tld[alpha2] = tld 1abcdefghij

293 tld_to_alpha2[tld] = alpha2 1abcdefghij

294 

295 return alpha2_to_tld, tld_to_alpha2 1abcdefghij

296 

297 

298@lru_cache(maxsize=1) 1abcdefghij

299def get_iso3166_alpha3_codes() -> tuple[str, ...]: 1abcdefghij

300 """ 

301 Get ISO 3166-1 alpha-3 country codes. 

302 

303 Returns: 

304 tuple[str, ...]: The ISO 3166-1 alpha-3 country codes in uppercase. 

305 

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 ) 

315 

316 return tuple(line for line in lines if not line.startswith('#') and (_line := line.strip().upper())) 1abcdefghij

317 

318 

319@lru_cache(maxsize=1) 1abcdefghij

320def get_iso3166_numeric_codes() -> tuple[int, ...]: 1abcdefghij

321 """ 

322 Get ISO 3166-1 numeric country codes. 

323 

324 Returns: 

325 tuple[int, ...]: The ISO 3166-1 numeric country codes. 

326 

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 ) 

336 

337 return tuple(int(line) for line in lines if not line.startswith('#') and (_line := line.strip())) 1abcdefghij