Skip to content
Snippets Groups Projects
Commit 15c67a0c authored by Timothe Jost's avatar Timothe Jost
Browse files

ongoing developpement of improved typing support via other means of creation than decorators

parent 073aa3ac
No related branches found
No related tags found
No related merge requests found
__version__ = "0.0.71" __version__ = "0.0.72"
from . import loggs from . import loggs
from .pipes import * from .pipes import *
......
...@@ -75,20 +75,20 @@ class BasePipe(BasePipeType, metaclass=ABCMeta): ...@@ -75,20 +75,20 @@ class BasePipe(BasePipeType, metaclass=ABCMeta):
requires_is_step_attr = True requires_is_step_attr = True
for class_name, class_object in inspect.getmembers(self, predicate=inspect.isclass): for class_name, class_object in inspect.getmembers(self, predicate=inspect.isclass):
print(class_name, class_object)
if class_name == "Steps": if class_name == "Steps":
print("FOUND")
steps_members_scanner = inspect.getmembers(class_object(), predicate=inspect.ismethod) steps_members_scanner = inspect.getmembers(class_object(), predicate=inspect.ismethod)
requires_is_step_attr = False requires_is_step_attr = False
break break
# this loop populates self.steps dictionnary from the instanciated (bound) step methods. # this loop populates self.steps dictionnary from the instanciated (bound) step methods.
for step_name, step in steps_members_scanner: for step_name, step in steps_members_scanner:
print("step:", step_name)
if not requires_is_step_attr or getattr(step, "is_step", False): if not requires_is_step_attr or getattr(step, "is_step", False):
step_name = to_snake_case(step_name) step_name = to_snake_case(step_name)
_steps[step_name] = step _steps[step_name] = step
for step_name, step in inspect.getmembers(self, predicate=inspect.isclass):
if
if len(_steps) < 1: if len(_steps) < 1:
raise ValueError( raise ValueError(
f"You should register at least one step class with @stepmethod in {self.pipe_name} class. {_steps=}" f"You should register at least one step class with @stepmethod in {self.pipe_name} class. {_steps=}"
......
...@@ -8,7 +8,7 @@ from pandas import DataFrame ...@@ -8,7 +8,7 @@ from pandas import DataFrame
from dataclasses import dataclass from dataclasses import dataclass
from types import MethodType from types import MethodType
from typing import Callable, Type, Iterable, Protocol, List, TYPE_CHECKING, Any from typing import Callable, Type, Iterable, Protocol, List, TYPE_CHECKING, Any, Optional
if TYPE_CHECKING: if TYPE_CHECKING:
from .pipelines import Pipeline from .pipelines import Pipeline
...@@ -78,7 +78,9 @@ class BaseStep: ...@@ -78,7 +78,9 @@ class BaseStep:
pipe: "BasePipe" pipe: "BasePipe"
pipeline: "Pipeline" pipeline: "Pipeline"
def __init__(self, pipeline: "Pipeline", pipe: "BasePipe", worker: MethodType, step_name: str = ""): def __init__(
self, pipeline: "Pipeline", pipe: "BasePipe", worker: Optional[MethodType] = None, step_name: str = ""
):
"""Initialize a BaseStep object. """Initialize a BaseStep object.
Args: Args:
...@@ -102,22 +104,37 @@ class BaseStep: ...@@ -102,22 +104,37 @@ class BaseStep:
self.pipeline = pipeline self.pipeline = pipeline
# save an instanciated access to the pipe parent # save an instanciated access to the pipe parent
self.pipe = pipe self.pipe = pipe
self.step_name = to_snake_case(self.get_attribute_or_default("step_name", step_name))
if not self.step_name:
raise ValueError(f"Step name in {self.pipe.pipe_name} cannot be blank nor None")
# save an instanciated access to the step function (undecorated) # save an instanciated access to the step function (undecorated)
self.worker = worker if not hasattr(self, "worker"):
if worker is None:
raise AttributeError(
f"For the step : {self.pipe.pipe_name}.{self.step_name}, a worker method must "
"be defined if created from a class"
)
needs_attachment = True
self.worker = worker
else:
needs_attachment = False
# we attach the values of the worker elements to BaseStep # we attach the values of the worker elements to BaseStep
# as they are get only (no setter) on worker (bound method) # as they are get only (no setter) on worker (bound method)
self.do_dispatch = getattr(self.worker, "do_dispatch", False)
self.version = getattr(self.worker, "version", 0)
self.requires = getattr(self.worker, "requires", [])
self.step_name = to_snake_case(getattr(self.worker, "step_name", step_name))
if not self.step_name: getattr(self, "do_dispatch", getattr(self.worker, "do_dispatch", False))
raise ValueError("Step name cannot be blank nor None")
self.callbacks = getattr(self.worker, "callbacks", []) self.do_dispatch = self.get_attribute_or_default("do_dispatch", False)
self.version = self.get_attribute_or_default("version", 0)
self.requires = self.get_attribute_or_default("requires", [])
self.worker = MethodType(worker.__func__, self) self.callbacks = self.get_attribute_or_default("callbacks", [])
if needs_attachment:
self.worker = MethodType(worker.__func__, self)
# self.make_wrapped_functions() # self.make_wrapped_functions()
...@@ -128,6 +145,10 @@ class BaseStep: ...@@ -128,6 +145,10 @@ class BaseStep:
self.task = self.pipeline.runner_backend.create_task_manager(self) self.task = self.pipeline.runner_backend.create_task_manager(self)
def get_attribute_or_default(self, attribute_name: str, default: Any) -> Any:
# TODO : fix here , when calling get_attribute_or_default before worker s set, cannot work
return getattr(self, attribute_name, getattr(self.worker, attribute_name, default))
@property @property
def requirement_stack(self) -> Callable: def requirement_stack(self) -> Callable:
"""Return a partial function that calls the get_requirement_stack method of the pipeline """Return a partial function that calls the get_requirement_stack method of the pipeline
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment