From 2b072a8d1991a488c8216ea5d1c4ee25b8fb6d9c Mon Sep 17 00:00:00 2001 From: John Kelly Date: Sat, 16 Mar 2024 15:16:50 +0000 Subject: [PATCH] Add `in` and `.get(k, default)` support to `BaseDataSet` --- alpaca/data/models/base.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/alpaca/data/models/base.py b/alpaca/data/models/base.py index 2700ebd4..21c0e2f6 100644 --- a/alpaca/data/models/base.py +++ b/alpaca/data/models/base.py @@ -43,6 +43,9 @@ class BaseDataSet(BaseModel): data: Dict[str, List[BaseModel]] = {} model_config = ConfigDict(protected_namespaces=tuple()) + def get(self, symbol: str, default: Any = None) -> Any: + return self.data.get(symbol, default) + def __getitem__(self, symbol: str) -> Any: """Gives dictionary-like access to multi-symbol data @@ -60,6 +63,18 @@ def __getitem__(self, symbol: str) -> Any: return self.data[symbol] + def __contains__(self, symbol: str) -> bool: + """Gives dictionary-like ability to check if a symbol is within the data + + Args: + symbol (str): The ticker identifier to check + + + Returns: + bool: Whether the symbol was in the data set + """ + return symbol in self.data + def dict(self, **kwargs) -> dict: """ Gives dictionary representation of data.