-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tbl): add tbl function for pandas and sqla
- Loading branch information
Showing
2 changed files
with
79 additions
and
1 deletion.
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,52 @@ | ||
""" | ||
This module allows you to check types (e.g. using isinstance) without importing them. | ||
Note that this is copied from https://github.com/machow/databackend | ||
""" | ||
|
||
import sys | ||
import importlib | ||
|
||
from abc import ABCMeta | ||
|
||
|
||
def _load_class(mod_name: str, cls_name: str): | ||
mod = importlib.import_module(mod_name) | ||
return getattr(mod, cls_name) | ||
|
||
|
||
class _AbstractBackendMeta(ABCMeta): | ||
def __new__(mcls, clsname, bases, attrs): | ||
cls = super().__new__(mcls, clsname, bases, attrs) | ||
if not hasattr(cls, "_backends"): | ||
cls._backends = [] | ||
return cls | ||
|
||
def register_backend(cls, mod_name: str, cls_name: str): | ||
cls._backends.append((mod_name, cls_name)) | ||
cls._abc_caches_clear() | ||
|
||
|
||
class AbstractBackend(metaclass=_AbstractBackendMeta): | ||
@classmethod | ||
def __subclasshook__(cls, subclass): | ||
for mod_name, cls_name in cls._backends: | ||
if mod_name not in sys.modules: | ||
# module isn't loaded, so it can't be the subclass | ||
# we don't want to import the module to explicitly run the check | ||
# so skip here. | ||
continue | ||
else: | ||
target_cls = _load_class(mod_name, cls_name) | ||
if issubclass(target_cls, subclass): | ||
return True | ||
|
||
return NotImplemented | ||
|
||
|
||
# Implementations ------------------------------------------------------------- | ||
|
||
class SqlaEngine(AbstractBackend): pass | ||
|
||
SqlaEngine.register_backend("sqlalchemy.engine", "Engine") |