Data Types

Bases: ABC

Abstract interface for data containers.

Overview

Declares the contract for concrete data types used across the project.

  • Abstract methods: read_file, get_data, get_units, get_allowed_observables.
  • Intended as a minimal API that all data loaders/adapters must implement.
Usage Notes

Implementations should populate an internal representation and match the return expectations used by callers elsewhere in the codebase.

Source code in contracts\data_types.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Data(ABC):
    """
    Abstract interface for data containers.

    Overview:
        Declares the contract for concrete data types used across the project.

    - Abstract methods: read_file, get_data, get_units, get_allowed_observables.
    - Intended as a minimal API that all data loaders/adapters must implement.

    Usage Notes:
        Implementations should populate an internal representation and match the return
        expectations used by callers elsewhere in the codebase.
    """

    @abstractmethod
    def read_file(self, filepath: str) -> None:
        pass

    @abstractmethod
    def get_data(self, observable: str) -> list:
        pass

    @abstractmethod
    def get_units(self, observable: str) -> str:
        pass

    @abstractmethod
    def get_allowed_observables(self):
        pass

Bases: Data

Base implementation providing common behaviors for data types.

Overview

Supplies shared storage and partial method implementations useful to subclasses.

  • Manages raw_data and _allowed_observables.
  • Requires a file_reader conforming to FileReaderFn for loading raw data.
  • Implements datetime extraction from filenames, get_data, get_units, and get_allowed_observables.
  • Leaves read_file abstract so subclasses can:
    • call self.file_reader(filepath)
    • interpret its output into domain-specific observables.
Usage Notes

Subclasses must implement read_file and populate raw_data / _allowed_observables. get_data raises ValueError for unsupported observables.

Source code in contracts\data_types.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
@decorate_class_with_logging(log_level=DEBUG_DATA_TYPE)
class DataCore(Data):
    """
        Base implementation providing common behaviors for data types.

        Overview:
            Supplies shared storage and partial method implementations useful to subclasses.

        - Manages `raw_data` and `_allowed_observables`.
        - Requires a `file_reader` conforming to `FileReaderFn` for loading raw data.
        - Implements datetime extraction from filenames, `get_data`, `get_units`,
          and `get_allowed_observables`.
        - Leaves `read_file` abstract so subclasses can:
            - call `self.file_reader(filepath)`
            - interpret its output into domain-specific observables.

        Usage Notes:
            Subclasses must implement `read_file` and populate `raw_data` / `_allowed_observables`.
            `get_data` raises ValueError for unsupported observables.
    """
    raw_data: dict[str, Observable]
    def __init__(self, file_reader: FileReaderFn):
        self.raw_data: dict[str, Observable] = {}
        self._allowed_observables = {}
        self.file_reader = file_reader

    @abstractmethod
    def read_file(self, filepath: str) -> None:
        pass

    def _get_datetime_from_filename(self, filepath: str) -> None:
        datetime = CustomDatetime()
        filename = os.path.basename(filepath)
        self.raw_data['datetime'] = {"units": None, "data": datetime.create_datetime_from_string(filename)}

    def get_data(self, observable: str) -> Any:
        if observable in self._allowed_observables:
            return self.raw_data[observable]['data']
        else:
            raise ValueError(f"{self.__class__.__name__} does not contain {observable} data")

    def get_units(self, observable: str) -> str:
        self.get_data(observable)
        return self.raw_data[observable]["units"]

    def get_allowed_observables(self):
        return self._allowed_observables