Skip to content

Commit

Permalink
Use importlib.resources.files() API
Browse files Browse the repository at this point in the history
Since we now require Python 3.9+, we can use this newer API instead of
the deprecated `importlib.resources.open_binary()`.
  • Loading branch information
jodal committed Oct 3, 2024
1 parent 9528fc2 commit e48a757
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
19 changes: 10 additions & 9 deletions src/biip/gs1/_application_identifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
]
}
31 changes: 12 additions & 19 deletions src/biip/gs1/_prefixes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)

0 comments on commit e48a757

Please sign in to comment.