-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a6dcc33
commit 1562f12
Showing
11 changed files
with
503 additions
and
163 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Queenbee Python DSL.""" |
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,4 +1,80 @@ | ||
from dataclasses import dataclass | ||
from typing import NamedTuple | ||
import importlib | ||
from collections import namedtuple | ||
|
||
|
||
def camel_to_snake(name: str) -> str: | ||
"""Change name from CamelCase to snake-case.""" | ||
return name[0].lower() + \ | ||
''.join(['-' + x.lower() if x.isupper() else x for x in name][1:]) | ||
|
||
|
||
@dataclass | ||
class _BaseClass: | ||
"""Base class for Queenbee DSL Function and DAG. | ||
Do not use this class directly. | ||
""" | ||
_cached_queenbee = None | ||
_cached_outputs = None | ||
_cached_package = None | ||
_cached_inputs = None | ||
|
||
@property | ||
def queenbee(self): | ||
raise NotImplementedError | ||
|
||
@property | ||
def _outputs(self) -> NamedTuple: | ||
raise NotImplementedError | ||
|
||
@property | ||
def _inputs(self) -> NamedTuple: | ||
"""Return inputs as a simple object with dot notation. | ||
Use this property to access the inputs when creating a DAG. | ||
The name starts with a _ not to conflict with a possible member of the class | ||
with the name inputs. | ||
""" | ||
if self._cached_inputs: | ||
return self._cached_inputs | ||
cls_name = camel_to_snake(self.__class__.__name__) | ||
mapper = { | ||
inp.name.replace('-', '_'): { | ||
'name': inp.name.replace('-', '_'), | ||
'parent': cls_name, | ||
'value': inp | ||
} for inp in self.queenbee.inputs | ||
} | ||
|
||
inputs = namedtuple('Inputs', list(mapper.keys())) | ||
self._cached_inputs = inputs(*list(mapper.values())) | ||
|
||
return self._cached_inputs | ||
|
||
@property | ||
def _package(self) -> dict: | ||
"""Queenbee package information. | ||
This information will only be available if the function is part of a Python | ||
package. | ||
""" | ||
if self._cached_package: | ||
return self._cached_package | ||
|
||
module = importlib.import_module(self._python_package) | ||
assert hasattr(module, '__queenbee__'), \ | ||
'Failed to find __queenbee__ info in __init__.py' | ||
self._cached_package = getattr(module, '__queenbee__') | ||
return self._cached_package | ||
|
||
@property | ||
def _python_package(self) -> str: | ||
"""Python package information for this function. | ||
This information will only be available if the function is part of a Python | ||
package. | ||
""" | ||
return self.__module__.split('.')[0] |
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
Oops, something went wrong.