From d48387d0271462c727a1a94070b2d9bf9ccda26b Mon Sep 17 00:00:00 2001 From: DongGeon Lee Date: Mon, 15 Apr 2024 20:53:23 +0900 Subject: [PATCH] feat: Replace remove_warmup_periods() implementation with Python (#374) --- stock_indicators/_cslib/__init__.py | 1 - stock_indicators/indicators/common/results.py | 32 +++++++------------ 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/stock_indicators/_cslib/__init__.py b/stock_indicators/_cslib/__init__.py index 6d9febfa..5fc44167 100644 --- a/stock_indicators/_cslib/__init__.py +++ b/stock_indicators/_cslib/__init__.py @@ -38,7 +38,6 @@ from Skender.Stock.Indicators import Quote as CsQuote from Skender.Stock.Indicators import QuoteUtility as CsQuoteUtility from Skender.Stock.Indicators import ResultBase as CsResultBase -from Skender.Stock.Indicators import Pruning as CsPruning # Enums from Skender.Stock.Indicators import BetaType as CsBetaType diff --git a/stock_indicators/indicators/common/results.py b/stock_indicators/indicators/common/results.py index b3473b01..0b065f6a 100644 --- a/stock_indicators/indicators/common/results.py +++ b/stock_indicators/indicators/common/results.py @@ -1,9 +1,8 @@ from datetime import datetime as PyDateTime from typing import Callable, Iterable, List, Optional, Type, TypeVar -from stock_indicators._cslib import CsResultBase, CsPruning +from stock_indicators._cslib import CsResultBase from stock_indicators._cstypes import DateTime as CsDateTime -from stock_indicators._cstypes import List as CsList from stock_indicators._cstypes import to_pydatetime @@ -77,26 +76,17 @@ def __add__(self, other: "IndicatorResults"): def __mul__(self, value: int): return self.__class__(list(self._csdata).__mul__(value), self._wrapper_class) + @_verify_data + def remove_warmup_periods(self, remove_periods: int): + """Remove a specific quantity of results from the beginning of the results list.""" + if not isinstance(remove_periods, int): + raise TypeError("remove_periods must be an integer.") + + return self.__class__(list(self._csdata)[remove_periods:], self._wrapper_class) + def find(self, lookup_date: PyDateTime) -> Optional[_T]: """Find indicator values on a specific date. It returns `None` if no result found.""" if not isinstance(lookup_date, PyDateTime): - raise TypeError( - "lookup_date must be an instance of datetime.datetime." - ) - - return next((r for r in self if r.date == lookup_date), None) + raise TypeError("lookup_date must be an instance of datetime.datetime.") - @_verify_data - def remove_warmup_periods(self, remove_periods: int): - """ - Remove a specific quantity of results from the beginning of the results list. - """ - if not isinstance(remove_periods, int): - raise TypeError( - "remove_periods must be an integer." - ) - - removed_results = CsPruning.RemoveWarmupPeriods[CsResultBase]( - CsList(self._get_csdata_type(), self._csdata), remove_periods - ) - return self.__class__(removed_results, self._wrapper_class) + return next((r for r in self if r.date == lookup_date), None)