-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add module lazy loading in __init__ files (#87)
This commit was made to avoid loading all of the tables when not required. This brings down the memory usage from ~400Mib to ~2MiB Co-authored-by: Beni Reydman <[email protected]>
- Loading branch information
1 parent
eeb723e
commit 2c24d77
Showing
2 changed files
with
49 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,30 @@ | ||
"""Package for evaluating a poker hand.""" | ||
from . import hash as hash_ # FIXME: `hash` collides to built-in function | ||
from . import tables | ||
from .card import Card | ||
from .evaluator import _evaluate_cards, evaluate_cards | ||
from .evaluator_omaha import _evaluate_omaha_cards, evaluate_omaha_cards | ||
from .utils import sample_cards | ||
|
||
__all__ = ["tables", "Card", "evaluate_cards", "evaluate_omaha_cards", "sample_cards"] | ||
|
||
# import mapping to objects in other modules | ||
all_by_module = { | ||
"phevaluator": ["card", "evaluator_omaha", "evaluator", "hash", "tables", "utils"], | ||
"phevaluator.card": ["Card"], | ||
"phevaluator.evaluator": ["_evaluate_cards", "evaluate_cards"], | ||
"phevaluator.evaluator_omaha": ["_evaluate_omaha_cards", "evaluate_omaha_cards"], | ||
"phevaluator.utils": ["sample_cards"] | ||
} | ||
|
||
# Based on werkzeug library | ||
object_origins = {} | ||
for module, items in all_by_module.items(): | ||
for item in items: | ||
object_origins[item] = module | ||
|
||
__docformat__ = "google" | ||
|
||
|
||
# Based on https://peps.python.org/pep-0562/ and werkzeug library | ||
def __getattr__(name): | ||
"""lazy submodule imports""" | ||
if name in object_origins: | ||
module = __import__(object_origins[name], None, None, [name]) | ||
return getattr(module, name) | ||
else: | ||
raise AttributeError(f"module {__name__!r} has no attribute {name!r}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters