Skip to content

Commit 4c5dc4c

Browse files
πŸ“Œ ISSUE-#23: Code improvement
1 parent 58d3f34 commit 4c5dc4c

File tree

16 files changed

+188
-134
lines changed

16 files changed

+188
-134
lines changed

β€Ždotflow/__init__.pyβ€Ž

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
__version__ = "0.3.0"
44

5-
from .core.actions import Action as action
6-
from .core.actions import retry # deprecated
7-
from .core.workflow import DotFlow
5+
from .core.action import Action as action
86
from .core.context import Context
7+
from .core.decorators import retry # deprecated
8+
from .core.workflow import DotFlow
99

1010

1111
__all__ = [

β€Ždotflow/core/action.pyβ€Ž

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""Action module"""
2+
3+
from typing import Callable
4+
5+
from dotflow.core.context import Context
6+
7+
8+
class Action(object):
9+
10+
def __init__(self, func: Callable = None, retry: int = 1):
11+
self.func = func
12+
self.retry = retry
13+
14+
def __call__(self, *args, **kwargs):
15+
if self.func:
16+
if self._has_context():
17+
context = kwargs.get("previous_context") or Context()
18+
return Context(storage=self._retry(*args, previous_context=context))
19+
else:
20+
return Context(storage=self._retry(*args))
21+
22+
def wrapper(*_args, **_kwargs):
23+
self.func = args[0]
24+
if self._has_context():
25+
context = _kwargs.get("previous_context") or Context()
26+
return Context(storage=self._retry(*_args, previous_context=context))
27+
else:
28+
return Context(storage=self._retry(*_args))
29+
return wrapper
30+
31+
def _retry(self, *args, **kwargs):
32+
attempt = 0
33+
exception = Exception()
34+
35+
while self.retry > attempt:
36+
try:
37+
return self.func(*args, **kwargs)
38+
except Exception as error:
39+
exception = error
40+
attempt += 1
41+
42+
raise exception
43+
44+
def _has_context(self):
45+
return 'previous_context' in self.func.__code__.co_varnames

β€Ždotflow/core/actions.pyβ€Ž

Lines changed: 0 additions & 84 deletions
This file was deleted.

β€Ždotflow/core/context.pyβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ class Context:
88

99
def __init__(self, storage: Any = None) -> None:
1010
self.time = datetime.now()
11-
self.storage = storage
1211

1312
if isinstance(storage, Context):
14-
self.time = storage.time
1513
self.storage = storage.storage
14+
else:
15+
self.storage = storage

β€Ždotflow/core/controller.pyβ€Ž

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
import threading
44

55
from uuid import uuid4
6-
from datetime import datetime
76
from typing import Callable, List
87

98
from dotflow.core.context import Context
109
from dotflow.core.status.workflow import WorkflowStatus
1110
from dotflow.core.task import Task
1211
from dotflow.core.utils import exec
12+
from dotflow.core.decorators import time
1313

1414

1515
class Controller:
@@ -37,27 +37,20 @@ def _callback_workflow(self, result: Task):
3737
else:
3838
self.success(content=result)
3939

40+
@time
4041
def _excution(self, task: Task, previous_context: Context):
4142
task.workflow_id = self.workflow_id
42-
task.status = WorkflowStatus.IN_PROGRESS
43-
start_time = datetime.now()
43+
task.set_status(WorkflowStatus.IN_PROGRESS)
44+
task.set_previous_context(previous_context)
4445

4546
try:
4647
current_context = task.step(previous_context=previous_context)
47-
duration = int((datetime.now() - start_time).total_seconds())
48-
49-
task.status = WorkflowStatus.COMPLETED
50-
task.current_context = current_context
51-
task.previous_context = previous_context
52-
task.duration = duration
48+
task.set_status(WorkflowStatus.COMPLETED)
49+
task.set_current_context(current_context)
5350

5451
except Exception as error:
55-
duration = int((datetime.now() - start_time).total_seconds())
56-
57-
task.status = WorkflowStatus.FAILED
58-
task.previous_context = previous_context
52+
task.set_status(WorkflowStatus.FAILED)
5953
task.error.append(error)
60-
task.duration = duration
6154

6255
task.callback(content=task)
6356
return task
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
11
"""Decorators __init__ module."""
2+
3+
from dotflow.core.decorators.action import action
4+
from dotflow.core.decorators.time import time
5+
from dotflow.core.decorators.retry import retry
6+
7+
8+
__all__ = [
9+
"action",
10+
"time",
11+
"retry"
12+
]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Action module"""
2+
3+
from dotflow.core.context import Context
4+
5+
6+
def action(func):
7+
def inside(*args, **kwargs):
8+
previous_context = kwargs.get("previous_context") or Context()
9+
10+
if 'previous_context' in func.__code__.co_varnames:
11+
output = func(*args, previous_context=previous_context)
12+
else:
13+
output = func(*args)
14+
15+
return Context(storage=output)
16+
17+
return inside
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""Retry module"""
2+
3+
from warnings import warn
4+
5+
6+
def retry(max_retry):
7+
warn("The 'retry' decorator is deprecated", DeprecationWarning, stacklevel=2)
8+
9+
def inside(func):
10+
11+
def wrapper(*args, **kwargs):
12+
attempt = 0
13+
error_output = Exception()
14+
15+
while max_retry > attempt:
16+
try:
17+
return func(*args, **kwargs)
18+
except Exception as error:
19+
error_output = error
20+
attempt += 1
21+
22+
raise error_output
23+
24+
return wrapper
25+
return inside
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""Time module"""
2+
3+
from datetime import datetime
4+
5+
6+
def time(func):
7+
def inside(*args, **kwargs):
8+
start = datetime.now()
9+
task = func(*args, **kwargs)
10+
task.set_duration = (datetime.now() - start).total_seconds()
11+
return task
12+
return inside

β€Ždotflow/core/task.pyβ€Ž

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ def __init__(self,
1919
previous_context: Any = None,
2020
status: WorkflowStatus = WorkflowStatus.NOT_STARTED,
2121
error: List[Exception] = [],
22-
duration: int = 0,
23-
workflow_id: UUID = None):
22+
duration: float = 0,
23+
workflow_id: UUID = None) -> None:
2424
self.task_id = task_id
2525
self.step = step
2626
self.callback = callback
@@ -31,6 +31,18 @@ def __init__(self,
3131
self.error = error
3232
self.duration = duration
3333
self.workflow_id = workflow_id
34+
35+
def set_status(self, value: WorkflowStatus) -> None:
36+
self.status = value
37+
38+
def set_duration(self, value: float) -> None:
39+
self.duration = value
40+
41+
def set_current_context(self, value: Context) -> None:
42+
self.current_context = value
43+
44+
def set_previous_context(self, value: Context) -> None:
45+
self.previous_context = value
3446

3547

3648
class TaskBuilder:

0 commit comments

Comments
Β (0)