Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add module lazy loading in __init__ files #87

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions python/phevaluator/__init__.py
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}")
44 changes: 24 additions & 20 deletions python/phevaluator/tables/__init__.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
"""Pre-calculated tables."""
from .dptables import CHOOSE, DP, SUITS
from .hashtable import FLUSH
from .hashtable5 import NO_FLUSH_5
from .hashtable6 import NO_FLUSH_6
from .hashtable7 import NO_FLUSH_7
from .hashtable_omaha import FLUSH_OMAHA, NO_FLUSH_OMAHA
# import mapping to pre-calculated tables in other modules
all_by_module = {
"phevaluator.tables.dptables": ["CHOOSE", "DP", "SUITS"],
"phevaluator.tables.hashtable": ["FLUSH"],
"phevaluator.tables.hashtable5": ["NO_FLUSH_5"],
"phevaluator.tables.hashtable6": ["NO_FLUSH_6"],
"phevaluator.tables.hashtable7": ["NO_FLUSH_7"],
"phevaluator.tables.hashtable_omaha": ["FLUSH_OMAHA", "NO_FLUSH_OMAHA"]
}

__all__ = [
"BINARIES_BY_ID",
"CHOOSE",
"DP",
"SUITBIT_BY_ID",
"SUITS",
"FLUSH",
"NO_FLUSH_5",
"NO_FLUSH_6",
"NO_FLUSH_7",
"FLUSH_OMAHA",
"NO_FLUSH_OMAHA",
]
# Based on werkzeug library
object_origins = {}
for module, items in all_by_module.items():
for item in items:
object_origins[item] = module

# fmt: off
BINARIES_BY_ID = [
Expand All @@ -39,3 +33,13 @@

SUITBIT_BY_ID = [0x1, 0x8, 0x40, 0x200] * 13
# fmt: on


# 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}")
Loading