Skip to content

Commit a7a8d68

Browse files
committed
Remove unused function
1 parent dd2dd38 commit a7a8d68

1 file changed

Lines changed: 2 additions & 64 deletions

File tree

dagfactory/utils.py

Lines changed: 2 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
import re
99
import sys
1010
import types
11-
from datetime import date, datetime, timedelta
11+
from datetime import datetime, timedelta
1212
from pathlib import Path
13-
from typing import Any, AnyStr, Dict, List, Match, Optional, Pattern, Tuple, Union
13+
from typing import Any, Dict, List, Tuple, Union
1414

1515
import pendulum
1616
import yaml
@@ -28,68 +28,6 @@ def _import_from_string(class_path):
2828
raise ImportError(f"Could not import '{class_path}': {e}")
2929

3030

31-
def get_datetime(date_value: Union[str, datetime, date], timezone: str = "UTC") -> datetime:
32-
"""
33-
Takes value from DAG config and generates valid datetime. Defaults to
34-
today, if not a valid date or relative time (1 hours, 1 days, etc.)
35-
36-
:param date_value: either a datetime (or date), a date string or a relative time as string
37-
:type date_value: Uniont[datetime, date, str]
38-
:param timezone: string value representing timezone for the DAG
39-
:type timezone: str
40-
:returns: datetime for date_value
41-
:type: datetime.datetime
42-
"""
43-
try:
44-
local_tz: pendulum.timezone = pendulum.timezone(timezone)
45-
except Exception as err:
46-
raise DagFactoryException("Failed to create timezone") from err
47-
if isinstance(date_value, datetime):
48-
return date_value.replace(tzinfo=local_tz)
49-
if isinstance(date_value, date):
50-
return datetime.combine(date=date_value, time=datetime.min.time()).replace(tzinfo=local_tz)
51-
# Try parsing as date string
52-
try:
53-
return pendulum.parse(date_value).replace(tzinfo=local_tz)
54-
except pendulum.parsing.exceptions.ParserError:
55-
# Try parsing as relative time string
56-
rel_delta: timedelta = get_time_delta(date_value)
57-
now: datetime = datetime.today().replace(hour=0, minute=0, second=0, microsecond=0).replace(tzinfo=local_tz)
58-
if not rel_delta:
59-
return now
60-
return now - rel_delta
61-
62-
63-
def get_time_delta(time_string: str) -> timedelta:
64-
"""
65-
Takes a time string (1 hours, 10 days, etc.) and returns
66-
a python timedelta object
67-
68-
:param time_string: the time value to convert to a timedelta
69-
:type time_string: str
70-
:returns: datetime.timedelta for relative time
71-
:type datetime.timedelta
72-
"""
73-
# pylint: disable=line-too-long
74-
rel_time: Pattern = re.compile(
75-
pattern=r"((?P<hours>\d+?)\s+hour)?((?P<minutes>\d+?)\s+minute)?((?P<seconds>\d+?)\s+second)?((?P<days>\d+?)\s+day)?",
76-
# noqa
77-
flags=re.IGNORECASE,
78-
)
79-
parts: Optional[Match[AnyStr]] = rel_time.match(string=time_string)
80-
if not parts:
81-
raise DagFactoryException(f"Invalid relative time: {time_string}")
82-
# https://docs.python.org/3/library/re.html#re.Match.groupdict
83-
parts: Dict[str, str] = parts.groupdict()
84-
time_params = {}
85-
if all(value is None for value in parts.values()):
86-
raise DagFactoryException(f"Invalid relative time: {time_string}")
87-
for time_unit, magnitude in parts.items():
88-
if magnitude:
89-
time_params[time_unit]: int = int(magnitude)
90-
return timedelta(**time_params)
91-
92-
9331
def merge_configs(config: Dict[str, Any], default_config: Dict[str, Any]) -> Dict[str, Any]:
9432
"""
9533
Merges a `default` config with DAG config. Used to set default values

0 commit comments

Comments
 (0)