-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move dependency related classes to Transactron (#544)
- Loading branch information
Showing
23 changed files
with
123 additions
and
105 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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from collections.abc import Callable | ||
|
||
from .. import Method | ||
from .transformers import Unifier | ||
from ..utils.dependencies import * | ||
|
||
|
||
__all__ = [ | ||
"DependencyManager", | ||
"DependencyKey", | ||
"SimpleKey", | ||
"ListKey", | ||
"UnifierKey" | ||
] | ||
|
||
|
||
class UnifierKey(DependencyKey["Method", tuple["Method", dict[str, "Unifier"]]]): | ||
"""Base class for method unifier dependency keys. | ||
Method unifier dependency keys are used to collect methods to be called by | ||
some part of the core. As multiple modules may wish to be called, a method | ||
unifier is used to present a single method interface to the caller, which | ||
allows to customize the calling behavior. | ||
""" | ||
|
||
unifier: Callable[[list["Method"]], "Unifier"] | ||
|
||
def __init_subclass__(cls, unifier: Callable[[list["Method"]], "Unifier"], **kwargs) -> None: | ||
cls.unifier = unifier | ||
return super().__init_subclass__(**kwargs) | ||
|
||
def combine(self, data: list["Method"]) -> tuple["Method", dict[str, "Unifier"]]: | ||
if len(data) == 1: | ||
return data[0], {} | ||
else: | ||
unifiers: dict[str, Unifier] = {} | ||
unifier_inst = self.unifier(data) | ||
unifiers[self.__class__.__name__ + "_unifier"] = unifier_inst | ||
method = unifier_inst.method | ||
return method, unifiers |
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
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
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from typing import TypeVar, Type, Any | ||
|
||
from transactron.utils import make_hashable | ||
|
||
__all__ = ["DependentCache"] | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
class DependentCache: | ||
""" | ||
Cache for classes, that depend on the `DependentCache` class itself. | ||
Cached classes may accept one positional argument in the constructor, where this `DependentCache` class will | ||
be passed. Classes may define any number keyword arguments in the constructor and separate cache entry will | ||
be created for each set of the arguments. | ||
Methods | ||
------- | ||
get: T, **kwargs -> T | ||
Gets class `cls` from cache. Caches `cls` reference if this is the first call for it. | ||
Optionally accepts `kwargs` for additional arguments in `cls` constructor. | ||
""" | ||
|
||
def __init__(self): | ||
self._depcache: dict[tuple[Type, Any], Type] = {} | ||
|
||
def get(self, cls: Type[T], **kwargs) -> T: | ||
cache_key = make_hashable(kwargs) | ||
v = self._depcache.get((cls, cache_key), None) | ||
if v is None: | ||
positional_count = cls.__init__.__code__.co_argcount | ||
|
||
# first positional arg is `self` field, second may be `DependentCache` | ||
if positional_count > 2: | ||
raise KeyError(f"Too many positional arguments in {cls!r} constructor") | ||
|
||
if positional_count > 1: | ||
v = cls(self, **kwargs) | ||
else: | ||
v = cls(**kwargs) | ||
self._depcache[(cls, cache_key)] = v | ||
return v |
Oops, something went wrong.