Skip to content

Commit

Permalink
Speed up package name lookup in to_dict()
Browse files Browse the repository at this point in the history
  • Loading branch information
hagenw committed Nov 23, 2023
1 parent d1f3dd8 commit 38d2591
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions audobject/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
from audobject.core.config import config


# Cache result of packages_distributions()
PACKAGES_DISTRIBUTIONS = {}


def create_class_key(cls: type, include_version: bool) -> str:
r"""Create class key.
Expand All @@ -25,13 +29,15 @@ def create_class_key(cls: type, include_version: bool) -> str:
- $PyYAML:yaml.loader.Loader
"""
global PACKAGES_DISTRIBUTIONS
key = define.OBJECT_TAG

# add package name (if different from module name)
module_name = cls.__module__.split('.')[0]
package_names = packages_distributions()
if module_name in package_names:
package_name = package_names[module_name][0]
if module_name not in PACKAGES_DISTRIBUTIONS:
PACKAGES_DISTRIBUTIONS = packages_distributions()
if module_name in PACKAGES_DISTRIBUTIONS:
package_name = PACKAGES_DISTRIBUTIONS[module_name][0]
if package_name != module_name:
key += f'{package_name}{define.PACKAGE_TAG}'

Expand Down

0 comments on commit 38d2591

Please sign in to comment.