Skip to content

Commit

Permalink
Move caching decorator from types to utils module.
Browse files Browse the repository at this point in the history
  • Loading branch information
jadchaar committed Jan 9, 2022
1 parent 27da135 commit e320f87
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 27 deletions.
8 changes: 4 additions & 4 deletions sec_cik_mapper/BaseMapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down
21 changes: 11 additions & 10 deletions sec_cik_mapper/MutualFundMapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down
15 changes: 8 additions & 7 deletions sec_cik_mapper/StockMapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down
7 changes: 1 addition & 6 deletions sec_cik_mapper/types.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
8 changes: 8 additions & 0 deletions sec_cik_mapper/utils.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e320f87

Please sign in to comment.