Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
clean date shift action
Browse files Browse the repository at this point in the history
  • Loading branch information
floriankrb committed Feb 26, 2024
1 parent c0374b6 commit df2df0c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 43 deletions.
50 changes: 20 additions & 30 deletions ecml_tools/create/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,25 +466,22 @@ def __repr__(self):
return super().__repr__(content)


class _DatesShiftAction(Action):
def __init__(self, context, action_path, _delta, **kwargs):
class DateShiftAction(Action):
def __init__(self, context, action_path, delta, **kwargs):
super().__init__(context, action_path, **kwargs)

if isinstance(_delta, str):
if _delta[0] == "-":
_delta, sign = int(_delta[1:]), -1
if isinstance(delta, str):
if delta[0] == "-":
delta, sign = int(delta[1:]), -1
else:
_delta, sign = int(_delta), 1
_delta = datetime.timedelta(hours=sign * _delta)
assert isinstance(_delta, int), _delta
_delta = datetime.timedelta(hours=_delta)
self.delta = _delta
delta, sign = int(delta), 1
delta = datetime.timedelta(hours=sign * delta)
assert isinstance(delta, int), delta
delta = datetime.timedelta(hours=delta)
self.delta = delta

self.content = action_factory(kwargs, context, self.action_path + ["shift"])

def is_shift_action(cls, name):
return "-" in name or "+" in name

@trace_select
def select(self, dates):
shifted_dates = [d + self.delta for d in dates]
Expand Down Expand Up @@ -773,23 +770,16 @@ def action_factory(config, context, action_path):
if isinstance(config[key], dict):
args, kwargs = [], config[key]

if "-" in key or "+" in key:
new_key, delta = parse_function_name(key)
new_config = dict(_dates_shift={"_delta": delta, new_key: config[key]})
return action_factory(new_config, context, action_path)

else:
cls = dict(
_dates_shift=_DatesShiftAction,
concat=ConcatAction,
join=JoinAction,
# label=LabelAction,
pipe=PipeAction,
# source=SourceAction,
function=FunctionAction,
dates=DateAction,
# dependency=DependencyAction,
).get(key)
cls = dict(
date_shift=DateShiftAction,
# date_filter=DateFilterAction,
# include=IncludeAction,
concat=ConcatAction,
join=JoinAction,
pipe=PipeAction,
function=FunctionAction,
dates=DateAction,
).get(key)

if cls is None:
if not is_function(key, "actions"):
Expand Down
15 changes: 2 additions & 13 deletions tests/create-shift.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,8 @@ input:
param:
- insolation


- constants-3h:
template: ${input.join.0.mars}
param:
- insolation
- constants+6h:
template: ${input.join.0.mars}
param:
- insolation

- _dates_shift: # private, do not use directly
#_delta: 6
_delta: -25
- date_shift:
delta: -25
constants:
template: ${input.join.0.mars}
param:
Expand Down

0 comments on commit df2df0c

Please sign in to comment.