Models
models ¶
Business domain model for equities_classifier.
ClassificationNode(value: str, code: str | None = None) dataclass ¶
ClassificationSystem(id: ClassificationSystemID, display_name: str, short_name: str, authorities: tuple[str, ...], hierarchy: tuple[str, ...], supports_codes: bool) dataclass ¶
Definition of a classification system.
Security(name: str | None = None, ticker: str | None = None, provider_attributes: dict[str, dict[str, Any]] = dict(), classifications: list[SecurityClassification] = list(), *, identifiers: SecurityIdentifierList = SecurityIdentifierList()) dataclass ¶
Security class.
classifications: list[SecurityClassification] = field(default_factory=list) class-attribute instance-attribute ¶
name: str | None = None class-attribute instance-attribute ¶
provider_attributes: dict[str, dict[str, Any]] = field(default_factory=dict) class-attribute instance-attribute ¶
ticker: str | None = None class-attribute instance-attribute ¶
provider_attribute(provider: DataSourceID, attribute: str) -> Any | None ¶
SecurityClassification(system: ClassificationSystem, nodes: dict[ClassificationLevel, ClassificationNode]) dataclass ¶
SecurityIdentifier(type: SecurityIdentifierType, value: str) dataclass ¶
Security identifier.
country: str | None = field(init=False) class-attribute instance-attribute ¶
type: SecurityIdentifierType instance-attribute ¶
value: str instance-attribute ¶
value_cleaned: str | None = field(init=False) class-attribute instance-attribute ¶
__post_init__() -> None ¶
Strip identifier value and for Ticker split into value_cleaned and country provided as postfix. NOTE: derived attributes where not marked with a leading _ to allow transparent access as for original value.
Source code in src/equities_classifier/models.py
def __post_init__(self) -> None:
"""Strip identifier value and for Ticker split into value_cleaned and country provided as postfix.
NOTE: derived attributes where not marked with a leading _ to allow transparent access as for
original value.
"""
# strip value
object.__setattr__(self, "value", self.value.strip())
# allow country suffix for ticker
if self.type != SecurityIdentifierType.TICKER or "." not in self.value:
object.__setattr__(self, "value_cleaned", self.value)
object.__setattr__(self, "country", None)
else:
value_cleaned, country = self.value.rsplit(".", 1)
object.__setattr__(self, "value_cleaned", value_cleaned)
object.__setattr__(self, "country", country)
get_country() ¶
get_value_cleaned() ¶
SecurityIdentifierIdentifiable(*, identifiers: SecurityIdentifierList = SecurityIdentifierList()) dataclass ¶
Base class for objects identified by security identifiers.
identifiers: SecurityIdentifierList = field(default_factory=SecurityIdentifierList) class-attribute instance-attribute ¶
has_identifier(identifier_type: SecurityIdentifierType) -> bool ¶
identifier(identifier_type: SecurityIdentifierType) -> SecurityIdentifier | None ¶
Find identifier of a specific type.
identifier_country(identifier_type: SecurityIdentifierType) -> str | None ¶
Return country of an identifier of a type TICKER.
identifier_value(identifier_type: SecurityIdentifierType) -> str | None ¶
Return value of an identifier of a specific type.
identifier_value_cleaned(identifier_type: SecurityIdentifierType) -> str | None ¶
Return cleaned value of an identifier of a specific type.
Source code in src/equities_classifier/models.py
SecurityIdentifierList(iterable: Iterable[SecurityIdentifier] = ()) ¶
List of security identifiers with unique identifier types.
Source code in src/equities_classifier/models.py
__setitem__(index: SupportsIndex | slice, value: SecurityIdentifier | Iterable[SecurityIdentifier]) -> None ¶
Source code in src/equities_classifier/models.py
def __setitem__(
self,
index: SupportsIndex | slice,
value: SecurityIdentifier | Iterable[SecurityIdentifier]
) -> None:
if isinstance(index, slice):
# at runtime this branch guarantees that value is iterable
replacement = list(value) # type: ignore[arg-type]
for identifier in replacement:
self._validate(identifier)
# Validate the resulting list before modifying this list.
result = list(self)
result[index] = replacement
self._validate_unique(result)
super().__setitem__(index, replacement)
return
# at runtime this branch is the single-item assignment
identifier = value # type: ignore[assignment]
self._validate(identifier) # type: ignore[arg-type]
self._check_duplicate(identifier, exclude_index=index) # type: ignore[arg-type]
super().__setitem__(index, identifier) # type: ignore[arg-type]
append(identifier: SecurityIdentifier) -> None ¶
List of security identifiers with unique identifier types - append overloading.
extend(iterable: Iterable[SecurityIdentifier]) -> None ¶
List of security identifiers with unique identifier types - extend overloading.
Source code in src/equities_classifier/models.py
def extend(self, iterable: Iterable[SecurityIdentifier]) -> None:
"""List of security identifiers with unique identifier types - extend overloading."""
identifiers = list(iterable)
self._validate_unique(identifiers)
for identifier in identifiers:
self._check_duplicate(identifier)
super().extend(identifiers)
insert(index: SupportsIndex, identifier: SecurityIdentifier) -> None ¶
List of security identifiers with unique identifier types - insert overloading.
Source code in src/equities_classifier/models.py
replace(identifier: SecurityIdentifier) -> None ¶
Replace the identifier with the same type, or append it if absent.
Source code in src/equities_classifier/models.py
def replace(self, identifier: SecurityIdentifier) -> None:
"""Replace the identifier with the same type, or append it if absent."""
self._validate(identifier)
for index, existing in enumerate(self):
if existing.type is identifier.type:
super().__setitem__(index, identifier)
return
super().append(identifier)
SecurityProviderRecord(name: str | None = None, ticker: str | None = None, *, identifiers: SecurityIdentifierList = SecurityIdentifierList()) dataclass ¶
Base class for SecurityProviderRecord classes ensuring common fields are contained.