diff --git a/fincal/core.py b/fincal/core.py index 4f7b698..1f94877 100644 --- a/fincal/core.py +++ b/fincal/core.py @@ -16,6 +16,30 @@ class Frequency: symbol: str +def date_parser(pos, params): + def parse_dates(func): + def wrapper_func(*args, **kwargs): + date_format = kwargs.get("date_format", None) + args = list(args) + for i, j in enumerate(params): + date = kwargs.get(j, None) + in_args = False + if date is None: + date = args[pos[i]] + in_args = True + + parsed_date = _parse_date(date, date_format) + if not in_args: + kwargs[j] = parsed_date + else: + args[pos[i]] = parsed_date + return func(*args, **kwargs) + + return wrapper_func + + return parse_dates + + class AllFrequencies: D = Frequency("daily", "days", 1, 1, "D") W = Frequency("weekly", "days", 7, 7, "W") @@ -43,23 +67,6 @@ class _IndexSlicer: return item -def date_parser(*params): - def parse_dates(func): - def wrapper_func(*args, **kwargs): - date_format = kwargs.get('date_format', None) - - for i, j in enumerate(params): - date = kwargs.get(j, None) - if date is None: - date = args[i+1] - - parsed_date = _parse_date(date, date_format) - kwargs[j] = parsed_date - return func(**kwargs) - return wrapper_func - return parse_dates - - class Series(UserList): """Container for a series of objects, all objects must be of the same type""" @@ -238,7 +245,7 @@ class TimeSeriesCore(UserDict): def _get_printable_slice(self, n: int): """Helper function for __repr__ and __str__ - Returns a slice of the dataframe from beginning and end. + Returns a slice of the dataframe from beginning and end. """ printable = {} diff --git a/fincal/fincal.py b/fincal/fincal.py index 2288752..e3b994b 100644 --- a/fincal/fincal.py +++ b/fincal/fincal.py @@ -184,7 +184,7 @@ class TimeSeries(TimeSeriesCore): return self.__class__(new_ts, frequency=self.frequency.symbol) - # @date_parser('as_on') + @date_parser(pos=[1], params=['as_on']) def calculate_returns( self, as_on: Union[str, datetime.datetime], @@ -261,7 +261,7 @@ class TimeSeries(TimeSeriesCore): (datetime.datetime(2020, 1, 1, 0, 0), .0567) """ - as_on = _parse_date(as_on, date_format) + # as_on = _parse_date(as_on, date_format) as_on_delta, prior_delta = _preprocess_match_options(as_on_match, prior_match, closest) prev_date = as_on - relativedelta(**{interval_type: interval_value}) @@ -277,7 +277,7 @@ class TimeSeries(TimeSeriesCore): returns = returns ** (1 / years) return (current[0] if return_actual_date else as_on), returns - 1 - # @date_parser('from_date', 'to_date') + @date_parser(pos=[1, 2], params=['from_date', 'to_date']) def calculate_rolling_returns( self, from_date: Union[datetime.date, str], @@ -358,8 +358,8 @@ class TimeSeries(TimeSeriesCore): TimeSeries.calculate_returns """ - from_date = _parse_date(from_date, date_format) - to_date = _parse_date(to_date, date_format) + # from_date = _parse_date(from_date, date_format) + # to_date = _parse_date(to_date, date_format) if frequency is None: frequency = self.frequency diff --git a/fincal/utils.py b/fincal/utils.py index f031978..e0cb531 100644 --- a/fincal/utils.py +++ b/fincal/utils.py @@ -13,7 +13,7 @@ class FincalOptions: def _parse_date(date: str, date_format: str = None): """Parses date and handles errors""" - + # print(date, date_format) if isinstance(date, (datetime.datetime, datetime.date)): return datetime.datetime.fromordinal(date.toordinal())