From e320f870a8b67a8d8f2d6f87ec8da25b6d5ed760 Mon Sep 17 00:00:00 2001 From: Jad Chaar Date: Sun, 9 Jan 2022 16:24:02 -0500 Subject: [PATCH] Move caching decorator from types to utils module. --- sec_cik_mapper/BaseMapper.py | 8 ++++---- sec_cik_mapper/MutualFundMapper.py | 21 +++++++++++---------- sec_cik_mapper/StockMapper.py | 15 ++++++++------- sec_cik_mapper/types.py | 7 +------ sec_cik_mapper/utils.py | 8 ++++++++ 5 files changed, 32 insertions(+), 27 deletions(-) create mode 100644 sec_cik_mapper/utils.py diff --git a/sec_cik_mapper/BaseMapper.py b/sec_cik_mapper/BaseMapper.py index b9f2346..f2d8f06 100644 --- a/sec_cik_mapper/BaseMapper.py +++ b/sec_cik_mapper/BaseMapper.py @@ -10,7 +10,8 @@ import requests from .retrievers import MutualFundRetriever, StockRetriever -from .types import CompanyData, FieldIndices, Fields, KeyToValueSet, cache +from .types import CompanyData, FieldIndices, Fields, KeyToValueSet +from .utils import with_cache class BaseMapper: @@ -87,7 +88,7 @@ def _form_kv_mapping(self, keys: pd.Series, values: pd.Series) -> Dict[str, str] return {k: v for k, v in zip(keys, values) if k and v} @property # type: ignore - @cache + @with_cache def cik_to_tickers(self) -> KeyToValueSet: """Get CIK to tickers mapping. @@ -106,7 +107,7 @@ def cik_to_tickers(self) -> KeyToValueSet: return self._form_kv_set_mapping(cik_col, ticker_col) @property # type: ignore - @cache + @with_cache def ticker_to_cik(self) -> Dict[str, str]: """Get ticker to CIK mapping. @@ -125,7 +126,6 @@ def ticker_to_cik(self) -> Dict[str, str]: return self._form_kv_mapping(ticker_col, cik_col) @property # type: ignore - @cache def raw_dataframe(self) -> pd.DataFrame: """Get raw pandas dataframe. diff --git a/sec_cik_mapper/MutualFundMapper.py b/sec_cik_mapper/MutualFundMapper.py index df46242..6b14ec1 100644 --- a/sec_cik_mapper/MutualFundMapper.py +++ b/sec_cik_mapper/MutualFundMapper.py @@ -5,7 +5,8 @@ from .BaseMapper import BaseMapper from .retrievers import MutualFundRetriever -from .types import KeyToValueSet, cache +from .types import KeyToValueSet +from .utils import with_cache class MutualFundMapper(BaseMapper): @@ -24,7 +25,7 @@ def __init__(self) -> None: super().__init__(MutualFundMapper._retriever) @property # type: ignore - @cache + @with_cache def cik_to_series_ids(self) -> KeyToValueSet: """Get CIK to series ID mapping. @@ -40,7 +41,7 @@ def cik_to_series_ids(self) -> KeyToValueSet: return self._form_kv_set_mapping(cik_col, series_id_col) @property # type: ignore - @cache + @with_cache def ticker_to_series_id(self) -> Dict[str, str]: """Get ticker to series ID mapping. @@ -56,7 +57,7 @@ def ticker_to_series_id(self) -> Dict[str, str]: return self._form_kv_mapping(ticker_col, series_id_col) @property # type: ignore - @cache + @with_cache def series_id_to_cik(self) -> Dict[str, str]: """Get series ID to CIK mapping. @@ -72,7 +73,7 @@ def series_id_to_cik(self) -> Dict[str, str]: return self._form_kv_mapping(series_id_col, cik_col) @property # type: ignore - @cache + @with_cache def series_id_to_tickers(self) -> KeyToValueSet: """Get series ID to tickers mapping. @@ -88,7 +89,7 @@ def series_id_to_tickers(self) -> KeyToValueSet: return self._form_kv_set_mapping(series_id_col, ticker_col) @property # type: ignore - @cache + @with_cache def series_id_to_class_ids(self) -> KeyToValueSet: """Get series ID to class IDs mapping. @@ -104,7 +105,7 @@ def series_id_to_class_ids(self) -> KeyToValueSet: return self._form_kv_set_mapping(series_id_col, class_id_col) @property # type: ignore - @cache + @with_cache def ticker_to_class_id(self) -> Dict[str, str]: """Get ticker to class ID mapping. @@ -120,7 +121,7 @@ def ticker_to_class_id(self) -> Dict[str, str]: return self._form_kv_mapping(ticker_col, class_id_col) @property # type: ignore - @cache + @with_cache def cik_to_class_ids(self) -> KeyToValueSet: """Get CIK to class IDs mapping. @@ -136,7 +137,7 @@ def cik_to_class_ids(self) -> KeyToValueSet: return self._form_kv_set_mapping(cik_col, class_id_col) @property # type: ignore - @cache + @with_cache def class_id_to_cik(self) -> Dict[str, str]: """Get class ID to CIK mapping. @@ -152,7 +153,7 @@ def class_id_to_cik(self) -> Dict[str, str]: return self._form_kv_mapping(class_id_col, cik_col) @property # type: ignore - @cache + @with_cache def class_id_to_ticker(self) -> Dict[str, str]: """Get class ID to ticker mapping. diff --git a/sec_cik_mapper/StockMapper.py b/sec_cik_mapper/StockMapper.py index 9c7480b..e4466b8 100644 --- a/sec_cik_mapper/StockMapper.py +++ b/sec_cik_mapper/StockMapper.py @@ -5,7 +5,8 @@ from .BaseMapper import BaseMapper from .retrievers import StockRetriever -from .types import KeyToValueSet, cache +from .types import KeyToValueSet +from .utils import with_cache class StockMapper(BaseMapper): @@ -24,7 +25,7 @@ def __init__(self) -> None: super().__init__(StockMapper._retriever) @property # type: ignore - @cache + @with_cache def cik_to_company_name(self) -> Dict[str, str]: """Get CIK to company name mapping. @@ -40,7 +41,7 @@ def cik_to_company_name(self) -> Dict[str, str]: return self._form_kv_mapping(cik_col, company_name_col) @property # type: ignore - @cache + @with_cache def ticker_to_company_name(self) -> Dict[str, str]: """Get ticker to company name mapping. @@ -56,7 +57,7 @@ def ticker_to_company_name(self) -> Dict[str, str]: return self._form_kv_mapping(ticker_col, company_name_col) @property # type: ignore - @cache + @with_cache def ticker_to_exchange(self) -> Dict[str, str]: """Get ticker to exchange mapping. @@ -72,7 +73,7 @@ def ticker_to_exchange(self) -> Dict[str, str]: return self._form_kv_mapping(ticker_col, exchange_col) @property # type: ignore - @cache + @with_cache def exchange_to_tickers(self) -> KeyToValueSet: """Get exchange to tickers mapping. @@ -88,7 +89,7 @@ def exchange_to_tickers(self) -> KeyToValueSet: return self._form_kv_set_mapping(exchange_col, ticker_col) @property # type: ignore - @cache + @with_cache def cik_to_exchange(self) -> Dict[str, str]: """Get CIK to exchange mapping. @@ -104,7 +105,7 @@ def cik_to_exchange(self) -> Dict[str, str]: return self._form_kv_mapping(cik_col, exchange_col) @property # type: ignore - @cache + @with_cache def exchange_to_ciks(self) -> KeyToValueSet: """Get exchange to CIKs mapping. diff --git a/sec_cik_mapper/types.py b/sec_cik_mapper/types.py index b5805fe..3d761f5 100644 --- a/sec_cik_mapper/types.py +++ b/sec_cik_mapper/types.py @@ -1,5 +1,4 @@ -from functools import lru_cache -from typing import Callable, Dict, List, Set, TypeVar, Union +from typing import Dict, List, Set, TypeVar, Union from typing_extensions import Literal, TypedDict @@ -31,7 +30,3 @@ class MutualFundFieldIndices(TypedDict): KeyToValueSet = Dict[str, Set[str]] T = TypeVar("T") - - -def cache(func: Callable[..., T]) -> T: - return lru_cache()(func) # type: ignore diff --git a/sec_cik_mapper/utils.py b/sec_cik_mapper/utils.py new file mode 100644 index 0000000..c4e53f3 --- /dev/null +++ b/sec_cik_mapper/utils.py @@ -0,0 +1,8 @@ +from functools import lru_cache +from typing import Callable + +from .types import T + + +def with_cache(func: Callable[..., T]) -> T: + return lru_cache()(func) # type: ignore