-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(datasets): share the cache of Ibis connections (#941)
* fix(datasets): share the cache of Ibis connections Signed-off-by: Deepyaman Datta <[email protected]> * chore(datasets): define required attrs and methods Signed-off-by: Deepyaman Datta <[email protected]> * chore(datasets): don't count connect stub coverage Signed-off-by: Deepyaman Datta <[email protected]> --------- Signed-off-by: Deepyaman Datta <[email protected]>
- Loading branch information
Showing
7 changed files
with
89 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .connection_mixin import ConnectionMixin # noqa: F401 |
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,32 @@ | ||
from abc import ABC, abstractmethod | ||
from collections.abc import Hashable | ||
from typing import Any, ClassVar | ||
|
||
|
||
class ConnectionMixin(ABC): | ||
_CONNECTION_GROUP: ClassVar[str] | ||
|
||
_connection_config: dict[str, Any] | ||
|
||
_connections: ClassVar[dict[Hashable, Any]] = {} | ||
|
||
@abstractmethod | ||
def _connect(self) -> Any: | ||
... # pragma: no cover | ||
|
||
@property | ||
def _connection(self) -> Any: | ||
def hashable(value: Any) -> Hashable: | ||
"""Return a hashable key for a potentially-nested object.""" | ||
if isinstance(value, dict): | ||
return tuple((k, hashable(v)) for k, v in sorted(value.items())) | ||
if isinstance(value, list): | ||
return tuple(hashable(x) for x in value) | ||
return value | ||
|
||
cls = type(self) | ||
key = self._CONNECTION_GROUP, hashable(self._connection_config) | ||
if key not in cls._connections: | ||
cls._connections[key] = self._connect() | ||
|
||
return cls._connections[key] |
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