ddeutil-workflow 0.0.72__py3-none-any.whl → 0.0.74__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
ddeutil/workflow/utils.py CHANGED
@@ -3,7 +3,35 @@
3
3
  # Licensed under the MIT License. See LICENSE in the project root for
4
4
  # license information.
5
5
  # ------------------------------------------------------------------------------
6
- """Utility function model."""
6
+ """Utility Functions for Workflow Operations.
7
+
8
+ This module provides essential utility functions used throughout the workflow
9
+ system for ID generation, datetime handling, string processing, template
10
+ operations, and other common tasks.
11
+
12
+ Functions:
13
+ gen_id: Generate unique identifiers for workflow components
14
+ make_exec: Create executable strings for shell commands
15
+ filter_func: Filter functions based on criteria
16
+ dump_all: Serialize data to various formats
17
+ delay: Create delays in execution
18
+ to_train: Convert strings to train-case format
19
+ get_dt_now: Get current datetime with timezone
20
+ get_d_now: Get current date
21
+ cross_product: Generate cross product of matrix values
22
+ replace_sec: Replace template variables in strings
23
+
24
+ Example:
25
+ ```python
26
+ from ddeutil.workflow.utils import gen_id, get_dt_now
27
+
28
+ # Generate unique ID
29
+ run_id = gen_id("workflow")
30
+
31
+ # Get current datetime
32
+ now = get_dt_now()
33
+ ```
34
+ """
7
35
  from __future__ import annotations
8
36
 
9
37
  import stat
@@ -27,13 +55,19 @@ T = TypeVar("T")
27
55
  UTC: Final[ZoneInfo] = ZoneInfo("UTC")
28
56
  MARK_NEWLINE: Final[str] = "||"
29
57
 
58
+ # Cache for random delay values to avoid repeated randrange calls
59
+ _CACHED_DELAYS = [randrange(0, 99, step=10) / 100 for _ in range(100)]
60
+ _DELAY_INDEX = 0
61
+
30
62
 
31
63
  def to_train(camel: str) -> str:
32
64
  """Convert camel case string to train case.
33
65
 
34
- :param camel: (str) A camel case string that want to convert.
66
+ Args:
67
+ camel: A camel case string that want to convert.
35
68
 
36
- :rtype: str
69
+ Returns:
70
+ str: The converted train-case string.
37
71
  """
38
72
  return "".join("-" + i if i.isupper() else i for i in camel).lstrip("-")
39
73
 
@@ -41,9 +75,11 @@ def to_train(camel: str) -> str:
41
75
  def prepare_newline(msg: str) -> str:
42
76
  """Prepare message that has multiple newline char.
43
77
 
44
- :param msg: (str) A message that want to prepare.
78
+ Args:
79
+ msg: A message that want to prepare.
45
80
 
46
- :rtype: str
81
+ Returns:
82
+ str: The prepared message with formatted newlines.
47
83
  """
48
84
  # NOTE: Remove ending with "\n" and replace "\n" with the "||" value.
49
85
  msg: str = msg.strip("\n").replace("\n", MARK_NEWLINE)
@@ -63,9 +99,11 @@ def prepare_newline(msg: str) -> str:
63
99
  def replace_sec(dt: datetime) -> datetime:
64
100
  """Replace second and microsecond values to 0.
65
101
 
66
- :param dt: A datetime object that want to replace.
102
+ Args:
103
+ dt: A datetime object that want to replace.
67
104
 
68
- :rtype: datetime
105
+ Returns:
106
+ datetime: The datetime with seconds and microseconds set to 0.
69
107
  """
70
108
  return dt.replace(second=0, microsecond=0)
71
109
 
@@ -87,6 +125,17 @@ def get_dt_now(tz: Optional[ZoneInfo] = None, offset: float = 0.0) -> datetime:
87
125
  return datetime.now(tz=tz) - timedelta(seconds=offset)
88
126
 
89
127
 
128
+ def get_dt_ntz_now() -> datetime: # pragma: no cov
129
+ """Get current datetime with no timezone.
130
+
131
+ Returns the current datetime object using the None timezone.
132
+
133
+ Returns:
134
+ datetime: Current datetime with no timezone
135
+ """
136
+ return get_dt_now(tz=None)
137
+
138
+
90
139
  def get_d_now(
91
140
  tz: Optional[ZoneInfo] = None, offset: float = 0.0
92
141
  ) -> date: # pragma: no cov
@@ -151,7 +200,10 @@ def delay(second: float = 0) -> None: # pragma: no cov
151
200
 
152
201
  :param second: (float) A second number that want to adds-on random value.
153
202
  """
154
- time.sleep(second + randrange(0, 99, step=10) / 100)
203
+ global _DELAY_INDEX
204
+ cached_random = _CACHED_DELAYS[_DELAY_INDEX % len(_CACHED_DELAYS)]
205
+ _DELAY_INDEX = (_DELAY_INDEX + 1) % len(_CACHED_DELAYS)
206
+ time.sleep(second + cached_random)
155
207
 
156
208
 
157
209
  def gen_id(