Skip to content

Models

models

Business domain model for equities_classifier.

ClassificationNode(value: str, code: str | None = None) dataclass

Single hierarchy node.

code: str | None = None class-attribute instance-attribute

value: str instance-attribute

ClassificationSystem(id: ClassificationSystemID, display_name: str, short_name: str, authorities: tuple[str, ...], hierarchy: tuple[str, ...], supports_codes: bool) dataclass

Definition of a classification system.

authorities: tuple[str, ...] instance-attribute

display_name: str instance-attribute

hierarchy: tuple[str, ...] instance-attribute

id: ClassificationSystemID instance-attribute

short_name: str instance-attribute

supports_codes: bool instance-attribute

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

Return a provider-specific attribute.

Source code in src/equities_classifier/models.py
def provider_attribute(self, provider: DataSourceID, attribute: str) -> Any | None:
    """Return a provider-specific attribute."""
    return self.provider_attributes.get(provider, {}).get(attribute)

SecurityClassification(system: ClassificationSystem, nodes: dict[ClassificationLevel, ClassificationNode]) dataclass

Classification of a security.

nodes: dict[ClassificationLevel, ClassificationNode] instance-attribute

system: ClassificationSystem instance-attribute

__post_init__() -> None

Source code in src/equities_classifier/models.py
def __post_init__(self) -> None:

    for level in ClassificationLevel:

        if level.value > len(self.system.hierarchy):
            break

        self.nodes.setdefault(level, ClassificationNode(value=""))

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()

Getter function for country.

Source code in src/equities_classifier/models.py
def get_country(self):
    """Getter function for country."""
    return self.country

get_value_cleaned()

Getter function for value_cleaned

Source code in src/equities_classifier/models.py
def get_value_cleaned(self):
    """Getter function for value_cleaned"""
    return self.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

Check if an identifier of a specific type is registered.

Source code in src/equities_classifier/models.py
def has_identifier(self, identifier_type: SecurityIdentifierType) -> bool:
    """Check if an identifier of a specific type is registered."""

    return self.identifier(identifier_type) is not None

identifier(identifier_type: SecurityIdentifierType) -> SecurityIdentifier | None

Find identifier of a specific type.

Source code in src/equities_classifier/models.py
def identifier(
    self,
    identifier_type: SecurityIdentifierType,
) -> SecurityIdentifier | None:
    """Find identifier of a specific type."""

    return next(
        (
            identifier
            for identifier in self.identifiers
            if identifier.type is identifier_type
        ),
        None,
    )

identifier_country(identifier_type: SecurityIdentifierType) -> str | None

Return country of an identifier of a type TICKER.

Source code in src/equities_classifier/models.py
def identifier_country(self, identifier_type: SecurityIdentifierType) -> str | None:
    """Return country of an identifier of a type TICKER."""

    identifier = self.identifier(identifier_type)
    return identifier.country if identifier is not None else None

identifier_value(identifier_type: SecurityIdentifierType) -> str | None

Return value of an identifier of a specific type.

Source code in src/equities_classifier/models.py
def identifier_value(self, identifier_type: SecurityIdentifierType) -> str | None:
    """Return value of an identifier of a specific type."""

    identifier = self.identifier(identifier_type)
    return identifier.value if identifier is not None else None

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
def identifier_value_cleaned(self, identifier_type: SecurityIdentifierType) -> str | None:
    """Return cleaned value of an identifier of a specific type."""

    identifier = self.identifier(identifier_type)
    return identifier.value_cleaned if identifier is not None else None

SecurityIdentifierList(iterable: Iterable[SecurityIdentifier] = ())

List of security identifiers with unique identifier types.

Source code in src/equities_classifier/models.py
def __init__(self, iterable: Iterable[SecurityIdentifier] = ()) -> None:
    """List of security identifiers with unique identifier types."""

    super().__init__()
    self.extend(iterable)

__setitem__(index: SupportsIndex | slice, value: SecurityIdentifier | Iterable[SecurityIdentifier]) -> None

__setitem__(index: SupportsIndex, value: SecurityIdentifier) -> None
__setitem__(index: slice, value: 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.

Source code in src/equities_classifier/models.py
def append(self, identifier: SecurityIdentifier) -> None:
    """List of security identifiers with unique identifier types - append overloading."""

    self._validate(identifier)
    self._check_duplicate(identifier)

    super().append(identifier)

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
def insert(self, index: SupportsIndex, identifier: SecurityIdentifier) -> None:
    """List of security identifiers with unique identifier types - insert overloading."""

    self._validate(identifier)
    self._check_duplicate(identifier)

    super().insert(index, identifier)

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.

datasource: DataSourceID class-attribute

name: str | None = None class-attribute instance-attribute

ticker: str | None = None class-attribute instance-attribute

provider_attributes() -> dict[str, Any]

Return all provider-specific attributes.

Source code in src/equities_classifier/models.py
def provider_attributes(self) -> dict[str, Any]:
    """Return all provider-specific attributes."""

    return {
        field.name: getattr(self, field.name)
        for field in fields(self)
        if field.name != "identifiers" and getattr(self, field.name) is not None
    }