Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add in and .get(k, default) support to BaseDataSet #422

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions alpaca/data/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down
Loading