implemented decorator
This commit is contained in:
parent
00ac66219b
commit
eeda4064b2
@ -16,6 +16,30 @@ class Frequency:
|
|||||||
symbol: str
|
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:
|
class AllFrequencies:
|
||||||
D = Frequency("daily", "days", 1, 1, "D")
|
D = Frequency("daily", "days", 1, 1, "D")
|
||||||
W = Frequency("weekly", "days", 7, 7, "W")
|
W = Frequency("weekly", "days", 7, 7, "W")
|
||||||
@ -43,23 +67,6 @@ class _IndexSlicer:
|
|||||||
return item
|
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):
|
class Series(UserList):
|
||||||
"""Container for a series of objects, all objects must be of the same type"""
|
"""Container for a series of objects, all objects must be of the same type"""
|
||||||
|
|
||||||
|
@ -184,7 +184,7 @@ class TimeSeries(TimeSeriesCore):
|
|||||||
|
|
||||||
return self.__class__(new_ts, frequency=self.frequency.symbol)
|
return self.__class__(new_ts, frequency=self.frequency.symbol)
|
||||||
|
|
||||||
# @date_parser('as_on')
|
@date_parser(pos=[1], params=['as_on'])
|
||||||
def calculate_returns(
|
def calculate_returns(
|
||||||
self,
|
self,
|
||||||
as_on: Union[str, datetime.datetime],
|
as_on: Union[str, datetime.datetime],
|
||||||
@ -261,7 +261,7 @@ class TimeSeries(TimeSeriesCore):
|
|||||||
(datetime.datetime(2020, 1, 1, 0, 0), .0567)
|
(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)
|
as_on_delta, prior_delta = _preprocess_match_options(as_on_match, prior_match, closest)
|
||||||
|
|
||||||
prev_date = as_on - relativedelta(**{interval_type: interval_value})
|
prev_date = as_on - relativedelta(**{interval_type: interval_value})
|
||||||
@ -277,7 +277,7 @@ class TimeSeries(TimeSeriesCore):
|
|||||||
returns = returns ** (1 / years)
|
returns = returns ** (1 / years)
|
||||||
return (current[0] if return_actual_date else as_on), returns - 1
|
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(
|
def calculate_rolling_returns(
|
||||||
self,
|
self,
|
||||||
from_date: Union[datetime.date, str],
|
from_date: Union[datetime.date, str],
|
||||||
@ -358,8 +358,8 @@ class TimeSeries(TimeSeriesCore):
|
|||||||
TimeSeries.calculate_returns
|
TimeSeries.calculate_returns
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from_date = _parse_date(from_date, date_format)
|
# from_date = _parse_date(from_date, date_format)
|
||||||
to_date = _parse_date(to_date, date_format)
|
# to_date = _parse_date(to_date, date_format)
|
||||||
|
|
||||||
if frequency is None:
|
if frequency is None:
|
||||||
frequency = self.frequency
|
frequency = self.frequency
|
||||||
|
@ -13,7 +13,7 @@ class FincalOptions:
|
|||||||
|
|
||||||
def _parse_date(date: str, date_format: str = None):
|
def _parse_date(date: str, date_format: str = None):
|
||||||
"""Parses date and handles errors"""
|
"""Parses date and handles errors"""
|
||||||
|
# print(date, date_format)
|
||||||
if isinstance(date, (datetime.datetime, datetime.date)):
|
if isinstance(date, (datetime.datetime, datetime.date)):
|
||||||
return datetime.datetime.fromordinal(date.toordinal())
|
return datetime.datetime.fromordinal(date.toordinal())
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user