diff --git a/src/biip/gs1/_application_identifiers.py b/src/biip/gs1/_application_identifiers.py index 148434b..81b60a1 100644 --- a/src/biip/gs1/_application_identifiers.py +++ b/src/biip/gs1/_application_identifiers.py @@ -89,12 +89,13 @@ def __str__(self) -> str: return f"({self.ai})" -def _load_application_identifiers(): - with resources.open_text(__package__, '_application_identifiers.json') as f: - data = json.load(f) - return { - entry['ai']: GS1ApplicationIdentifier(**entry) - for entry in data - } - -_GS1_APPLICATION_IDENTIFIERS = _load_application_identifiers() +_GS1_APPLICATION_IDENTIFIERS_FILE = ( + resources.files("biip") / "gs1" / "_application_identifiers.json" +) +_GS1_APPLICATION_IDENTIFIERS = { + entry.ai: entry + for entry in [ + GS1ApplicationIdentifier(**kwargs) + for kwargs in json.loads(_GS1_APPLICATION_IDENTIFIERS_FILE.read_text()) + ] +} diff --git a/src/biip/gs1/_prefixes.py b/src/biip/gs1/_prefixes.py index 0270984..0be3750 100644 --- a/src/biip/gs1/_prefixes.py +++ b/src/biip/gs1/_prefixes.py @@ -142,22 +142,15 @@ class _GS1PrefixRange: usage: str -def _load_prefix_ranges(): - with resources.open_text(__package__, "_prefix_ranges.json") as f: - data = json.load(f) - return [_GS1PrefixRange(**kwargs) for kwargs in data] - - -_GS1_PREFIX_RANGES = _load_prefix_ranges() - - -def _load_company_prefix_trie(): - with resources.open_binary( - __package__, "_company_prefix_trie.json.lzma" - ) as compressed_file: - decompressed_data = lzma.decompress(compressed_file.read()) - data = json.loads(decompressed_data.decode("utf-8")) - return data - - -_GS1_COMPANY_PREFIX_TRIE: _TrieNode = _load_company_prefix_trie() +_GS1_PREFIX_RANGES_FILE = resources.files("biip") / "gs1" / "_prefix_ranges.json" +_GS1_PREFIX_RANGES = [ + _GS1PrefixRange(**kwargs) + for kwargs in json.loads(_GS1_PREFIX_RANGES_FILE.read_text()) +] + +_GS1_COMPANY_PREFIX_TRIE_FILE = ( + resources.files("biip") / "gs1" / "_company_prefix_trie.json.lzma" +) +_GS1_COMPANY_PREFIX_TRIE: _TrieNode = json.loads( + lzma.decompress(_GS1_COMPANY_PREFIX_TRIE_FILE.read_bytes()).decode() +)