Skip to content

contract.base

DataStream

DataStream(
    name: str,
    *,
    description: Optional[str] = None,
    reader_params: Optional[TReaderParams] = None,
    **kwargs,
)

Bases: ABC, Generic[TData, TReaderParams]

Abstract base class for all data streams.

Provides a generic interface for data reading operations with configurable parameters and hierarchical organization.

Parameters:

Name Type Description Default
name str

Name identifier for the data stream.

required
description Optional[str]

Optional description of the data stream.

None
reader_params Optional[TReaderParams]

Optional parameters for the data reader.

None
**kwargs

Additional keyword arguments.

{}

Attributes:

Name Type Description
_is_collection bool

Class variable indicating if this is a collection of data streams.

Raises:

Type Description
ValueError

If name contains '::' characters which are reserved for path resolution.

Source code in src/contraqctor/contract/base.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def __init__(
    self: Self,
    name: str,
    *,
    description: Optional[str] = None,
    reader_params: Optional[_typing.TReaderParams] = None,
    **kwargs,
) -> None:
    if "::" in name:
        raise ValueError("Name cannot contain '::' character.")
    self._name = name

    self._description = description
    self._reader_params = reader_params if reader_params is not None else _typing.UnsetParams
    self._data = _typing.UnsetData
    self._parent: Optional["DataStream"] = None

name property

name: str

Get the name of the data stream.

Returns:

Name Type Description
str str

Name identifier of the data stream.

resolved_name property

resolved_name: str

Get the full hierarchical name of the data stream.

Generates a path-like name showing the stream's position in the hierarchy, using '::' as a separator between parent and child names.

Returns:

Name Type Description
str str

The fully resolved name including all parent names.

description property

description: Optional[str]

Get the description of the data stream.

Returns:

Type Description
Optional[str]

Optional[str]: Description of the data stream, or None if not provided.

parent property

Get the parent data stream.

Returns:

Type Description
Optional[DataStream]

Optional[DataStream]: Parent data stream, or None if this is a root stream.

is_collection property

is_collection: bool

Check if this data stream is a collection of other streams.

Returns:

Name Type Description
bool bool

True if this is a collection stream, False otherwise.

reader_params property

reader_params: TReaderParams

Get the parameters for the data reader.

Returns:

Name Type Description
TReaderParams TReaderParams

Parameters for the data reader.

has_data property

has_data: bool

Check if the data stream has loaded data.

Returns:

Name Type Description
bool bool

True if data has been loaded, False otherwise.

data property

data: TData

Get the loaded data.

Returns:

Name Type Description
TData TData

The loaded data.

Raises:

Type Description
ValueError

If data has not been loaded yet.

read

read(
    reader_params: Optional[TReaderParams] = None,
) -> TData

Read data using the configured reader.

Parameters:

Name Type Description Default
reader_params Optional[TReaderParams]

Optional parameters to override the default reader parameters.

None

Returns:

Name Type Description
TData TData

Data read from the source.

Raises:

Type Description
ValueError

If reader parameters are not set.

Source code in src/contraqctor/contract/base.py
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
def read(self, reader_params: Optional[_typing.TReaderParams] = None) -> _typing.TData:
    """Read data using the configured reader.

    Args:
        reader_params: Optional parameters to override the default reader parameters.

    Returns:
        TData: Data read from the source.

    Raises:
        ValueError: If reader parameters are not set.
    """
    reader_params = reader_params if reader_params is not None else self._reader_params
    if _typing.is_unset(reader_params):
        raise ValueError("Reader parameters are not set. Cannot read data.")
    return self._reader(reader_params)

bind_reader_params

bind_reader_params(params: TReaderParams) -> Self

Bind reader parameters to the data stream.

Parameters:

Name Type Description Default
params TReaderParams

Parameters to bind to the data stream's reader.

required

Returns:

Name Type Description
Self Self

The data stream instance for method chaining.

Raises:

Type Description
ValueError

If reader parameters have already been set.

Source code in src/contraqctor/contract/base.py
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
def bind_reader_params(self, params: _typing.TReaderParams) -> Self:
    """Bind reader parameters to the data stream.

    Args:
        params: Parameters to bind to the data stream's reader.

    Returns:
        Self: The data stream instance for method chaining.

    Raises:
        ValueError: If reader parameters have already been set.
    """
    if not _typing.is_unset(self._reader_params):
        raise ValueError("Reader parameters are already set. Cannot bind again.")
    self._reader_params = params
    return self

at

at(name: str) -> DataStream

Get a child data stream by name.

Parameters:

Name Type Description Default
name str

Name of the child data stream to retrieve.

required

Returns:

Name Type Description
DataStream DataStream

The child data stream with the given name.

Raises:

Type Description
NotImplementedError

If the data stream does not support child access.

Examples:

# Access stream in a collection
collection = data_collection.load()
temp_stream = collection.at("temperature")

# Or using dictionary-style syntax
humidity_stream = collection["humidity"]
Source code in src/contraqctor/contract/base.py
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
def at(self, name: str) -> "DataStream":
    """Get a child data stream by name.

    Args:
        name: Name of the child data stream to retrieve.

    Returns:
        DataStream: The child data stream with the given name.

    Raises:
        NotImplementedError: If the data stream does not support child access.

    Examples:
        ```python
        # Access stream in a collection
        collection = data_collection.load()
        temp_stream = collection.at("temperature")

        # Or using dictionary-style syntax
        humidity_stream = collection["humidity"]
        ```
    """
    raise NotImplementedError("This method is not implemented for DataStream.")

load

load() -> Self

Load data into the data stream.

Reads data from the source and stores it in the data stream.

Returns:

Name Type Description
Self Self

The data stream instance for method chaining.

Examples:

from contraqctor.contract import csv

# Create and load a CSV stream
params = csv.CsvParams(path="data/measurements.csv")
csv_stream = csv.Csv("measurements", reader_params=params)
csv_stream.load()

# Access the data
df = csv_stream.data
print(f"Loaded {len(df)} rows")
Source code in src/contraqctor/contract/base.py
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
def load(self) -> Self:
    """Load data into the data stream.

    Reads data from the source and stores it in the data stream.

    Returns:
        Self: The data stream instance for method chaining.

    Examples:
        ```python
        from contraqctor.contract import csv

        # Create and load a CSV stream
        params = csv.CsvParams(path="data/measurements.csv")
        csv_stream = csv.Csv("measurements", reader_params=params)
        csv_stream.load()

        # Access the data
        df = csv_stream.data
        print(f"Loaded {len(df)} rows")
        ```
    """
    self._data = self.read()
    return self

load_all

load_all(
    strict: bool = False,
) -> list[tuple[DataStream, Exception], None, None]

Recursively load this data stream and all child streams.

Performs depth-first traversal to load all streams in the hierarchy.

Parameters:

Name Type Description Default
strict bool

If True, raises exceptions immediately; otherwise collects and returns them.

False

Returns:

Name Type Description
list list[tuple[DataStream, Exception], None, None]

List of tuples containing streams and exceptions that occurred during loading.

Raises:

Type Description
Exception

If strict is True and an exception occurs during loading.

Examples:

# Load all streams and handle errors
errors = collection.load_all(strict=False)

if errors:
    for stream, error in errors:
        print(f"Error loading {stream.name}: {error}")
Source code in src/contraqctor/contract/base.py
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
def load_all(self, strict: bool = False) -> list[tuple["DataStream", Exception], None, None]:
    """Recursively load this data stream and all child streams.

    Performs depth-first traversal to load all streams in the hierarchy.

    Args:
        strict: If True, raises exceptions immediately; otherwise collects and returns them.

    Returns:
        list: List of tuples containing streams and exceptions that occurred during loading.

    Raises:
        Exception: If strict is True and an exception occurs during loading.

    Examples:
        ```python
        # Load all streams and handle errors
        errors = collection.load_all(strict=False)

        if errors:
            for stream, error in errors:
                print(f"Error loading {stream.name}: {error}")
        ```
    """
    self.load()
    exceptions = []
    for stream in self:
        if stream is None:
            continue
        try:
            exceptions += stream.load_all(strict=strict)
        except Exception as e:
            if strict:
                raise e
            exceptions.append((stream, e))
    return exceptions

DataStreamCollectionBase

DataStreamCollectionBase(
    name: str,
    *,
    description: Optional[str] = None,
    reader_params: Optional[TReaderParams] = None,
    **kwargs,
)

Bases: DataStream[List[TDataStream], TReaderParams], Generic[TDataStream, TReaderParams]

Base class for collections of data streams.

Provides functionality for managing and accessing multiple child data streams.

Parameters:

Name Type Description Default
name str

Name identifier for the collection.

required
description Optional[str]

Optional description of the collection.

None
reader_params Optional[TReaderParams]

Optional parameters for the reader.

None
**kwargs

Additional keyword arguments.

{}
Source code in src/contraqctor/contract/base.py
317
318
319
320
321
322
323
324
325
326
327
def __init__(
    self: Self,
    name: str,
    *,
    description: Optional[str] = None,
    reader_params: Optional[_typing.TReaderParams] = None,
    **kwargs,
) -> None:
    super().__init__(name=name, description=description, reader_params=reader_params, **kwargs)
    self._hashmap: Dict[str, TDataStream] = {}
    self._update_hashmap()

name property

name: str

Get the name of the data stream.

Returns:

Name Type Description
str str

Name identifier of the data stream.

resolved_name property

resolved_name: str

Get the full hierarchical name of the data stream.

Generates a path-like name showing the stream's position in the hierarchy, using '::' as a separator between parent and child names.

Returns:

Name Type Description
str str

The fully resolved name including all parent names.

description property

description: Optional[str]

Get the description of the data stream.

Returns:

Type Description
Optional[str]

Optional[str]: Description of the data stream, or None if not provided.

parent property

Get the parent data stream.

Returns:

Type Description
Optional[DataStream]

Optional[DataStream]: Parent data stream, or None if this is a root stream.

is_collection property

is_collection: bool

Check if this data stream is a collection of other streams.

Returns:

Name Type Description
bool bool

True if this is a collection stream, False otherwise.

reader_params property

reader_params: TReaderParams

Get the parameters for the data reader.

Returns:

Name Type Description
TReaderParams TReaderParams

Parameters for the data reader.

has_data property

has_data: bool

Check if the data stream has loaded data.

Returns:

Name Type Description
bool bool

True if data has been loaded, False otherwise.

data property

data: TData

Get the loaded data.

Returns:

Name Type Description
TData TData

The loaded data.

Raises:

Type Description
ValueError

If data has not been loaded yet.

load

load()

Load data for this collection.

Overrides the base method to add validation that loaded data is a list of DataStreams.

Returns:

Name Type Description
Self

The collection instance for method chaining.

Raises:

Type Description
ValueError

If loaded data is not a list of DataStreams.

Source code in src/contraqctor/contract/base.py
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
@override
def load(self):
    """Load data for this collection.

    Overrides the base method to add validation that loaded data is a list of DataStreams.

    Returns:
        Self: The collection instance for method chaining.

    Raises:
        ValueError: If loaded data is not a list of DataStreams.
    """
    super().load()
    if not isinstance(self._data, list):
        self._data = _typing.UnsetData
        raise ValueError("Data must be a list of DataStreams.")
    self._update_hashmap()
    return self

at

at(name: str) -> TDataStream

Get a child data stream by name.

Parameters:

Name Type Description Default
name str

Name of the child data stream to retrieve.

required

Returns:

Name Type Description
TDataStream TDataStream

The child data stream with the given name.

Raises:

Type Description
ValueError

If data has not been loaded yet.

KeyError

If no child stream with the given name exists.

Source code in src/contraqctor/contract/base.py
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
@override
def at(self, name: str) -> TDataStream:
    """Get a child data stream by name.

    Args:
        name: Name of the child data stream to retrieve.

    Returns:
        TDataStream: The child data stream with the given name.

    Raises:
        ValueError: If data has not been loaded yet.
        KeyError: If no child stream with the given name exists.
    """
    if not self.has_data:
        raise ValueError("data streams have not been read yet. Cannot access data streams.")
    if name in self._hashmap:
        return self._hashmap[name]
    else:
        raise KeyError(f"Stream with name: '{name}' not found in data streams.")

iter_all

iter_all() -> Generator[DataStream, None, None]

Iterator for all child data streams, including nested collections.

Implements a depth-first traversal of the stream hierarchy.

Yields:

Name Type Description
DataStream DataStream

All recursively yielded child data streams.

Source code in src/contraqctor/contract/base.py
436
437
438
439
440
441
442
443
444
445
446
447
448
def iter_all(self) -> Generator[DataStream, None, None]:
    """Iterator for all child data streams, including nested collections.

    Implements a depth-first traversal of the stream hierarchy.

    Yields:
        DataStream: All recursively yielded child data streams.
    """
    for value in self:
        if isinstance(value, DataStream):
            yield value
        if isinstance(value, DataStreamCollectionBase):
            yield from value.iter_all()

read

read(
    reader_params: Optional[TReaderParams] = None,
) -> TData

Read data using the configured reader.

Parameters:

Name Type Description Default
reader_params Optional[TReaderParams]

Optional parameters to override the default reader parameters.

None

Returns:

Name Type Description
TData TData

Data read from the source.

Raises:

Type Description
ValueError

If reader parameters are not set.

Source code in src/contraqctor/contract/base.py
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
def read(self, reader_params: Optional[_typing.TReaderParams] = None) -> _typing.TData:
    """Read data using the configured reader.

    Args:
        reader_params: Optional parameters to override the default reader parameters.

    Returns:
        TData: Data read from the source.

    Raises:
        ValueError: If reader parameters are not set.
    """
    reader_params = reader_params if reader_params is not None else self._reader_params
    if _typing.is_unset(reader_params):
        raise ValueError("Reader parameters are not set. Cannot read data.")
    return self._reader(reader_params)

bind_reader_params

bind_reader_params(params: TReaderParams) -> Self

Bind reader parameters to the data stream.

Parameters:

Name Type Description Default
params TReaderParams

Parameters to bind to the data stream's reader.

required

Returns:

Name Type Description
Self Self

The data stream instance for method chaining.

Raises:

Type Description
ValueError

If reader parameters have already been set.

Source code in src/contraqctor/contract/base.py
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
def bind_reader_params(self, params: _typing.TReaderParams) -> Self:
    """Bind reader parameters to the data stream.

    Args:
        params: Parameters to bind to the data stream's reader.

    Returns:
        Self: The data stream instance for method chaining.

    Raises:
        ValueError: If reader parameters have already been set.
    """
    if not _typing.is_unset(self._reader_params):
        raise ValueError("Reader parameters are already set. Cannot bind again.")
    self._reader_params = params
    return self

load_all

load_all(
    strict: bool = False,
) -> list[tuple[DataStream, Exception], None, None]

Recursively load this data stream and all child streams.

Performs depth-first traversal to load all streams in the hierarchy.

Parameters:

Name Type Description Default
strict bool

If True, raises exceptions immediately; otherwise collects and returns them.

False

Returns:

Name Type Description
list list[tuple[DataStream, Exception], None, None]

List of tuples containing streams and exceptions that occurred during loading.

Raises:

Type Description
Exception

If strict is True and an exception occurs during loading.

Examples:

# Load all streams and handle errors
errors = collection.load_all(strict=False)

if errors:
    for stream, error in errors:
        print(f"Error loading {stream.name}: {error}")
Source code in src/contraqctor/contract/base.py
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
def load_all(self, strict: bool = False) -> list[tuple["DataStream", Exception], None, None]:
    """Recursively load this data stream and all child streams.

    Performs depth-first traversal to load all streams in the hierarchy.

    Args:
        strict: If True, raises exceptions immediately; otherwise collects and returns them.

    Returns:
        list: List of tuples containing streams and exceptions that occurred during loading.

    Raises:
        Exception: If strict is True and an exception occurs during loading.

    Examples:
        ```python
        # Load all streams and handle errors
        errors = collection.load_all(strict=False)

        if errors:
            for stream, error in errors:
                print(f"Error loading {stream.name}: {error}")
        ```
    """
    self.load()
    exceptions = []
    for stream in self:
        if stream is None:
            continue
        try:
            exceptions += stream.load_all(strict=strict)
        except Exception as e:
            if strict:
                raise e
            exceptions.append((stream, e))
    return exceptions

DataStreamCollection

DataStreamCollection(
    name: str,
    data_streams: List[DataStream],
    *,
    description: Optional[str] = None,
)

Bases: DataStreamCollectionBase[DataStream, UnsetParamsType]

Collection of data streams with direct initialization.

A specialized collection where child streams are passed directly instead of being created by a reader function.

Parameters:

Name Type Description Default
name str

Name identifier for the collection.

required
data_streams List[DataStream]

List of child data streams to include.

required
description Optional[str]

Optional description of the collection.

None

Examples:

from contraqctor.contract import csv, text, DataStreamCollection

# Create streams
text_stream = text.Text("readme", reader_params=text.TextParams(path="README.md"))
csv_stream = csv.Csv("data", reader_params=csv.CsvParams(path="data.csv"))

# Create the collection
collection = DataStreamCollection("project_files", [text_stream, csv_stream])

# Load and use
collection.load_all()
readme_content = collection["readme"].data

Initializes a special DataStreamGroup where the data streams are passed directly, without a reader.

Source code in src/contraqctor/contract/base.py
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
@override
def __init__(
    self,
    name: str,
    data_streams: List[DataStream],
    *,
    description: Optional[str] = None,
) -> None:
    """Initializes a special DataStreamGroup where the data streams are passed directly, without a reader."""
    super().__init__(
        name=name,
        description=description,
        reader_params=_typing.UnsetParams,
    )
    self.bind_data_streams(data_streams)

name property

name: str

Get the name of the data stream.

Returns:

Name Type Description
str str

Name identifier of the data stream.

resolved_name property

resolved_name: str

Get the full hierarchical name of the data stream.

Generates a path-like name showing the stream's position in the hierarchy, using '::' as a separator between parent and child names.

Returns:

Name Type Description
str str

The fully resolved name including all parent names.

description property

description: Optional[str]

Get the description of the data stream.

Returns:

Type Description
Optional[str]

Optional[str]: Description of the data stream, or None if not provided.

parent property

Get the parent data stream.

Returns:

Type Description
Optional[DataStream]

Optional[DataStream]: Parent data stream, or None if this is a root stream.

is_collection property

is_collection: bool

Check if this data stream is a collection of other streams.

Returns:

Name Type Description
bool bool

True if this is a collection stream, False otherwise.

reader_params property

reader_params: TReaderParams

Get the parameters for the data reader.

Returns:

Name Type Description
TReaderParams TReaderParams

Parameters for the data reader.

has_data property

has_data: bool

Check if the data stream has loaded data.

Returns:

Name Type Description
bool bool

True if data has been loaded, False otherwise.

data property

data: TData

Get the loaded data.

Returns:

Name Type Description
TData TData

The loaded data.

Raises:

Type Description
ValueError

If data has not been loaded yet.

parameters staticmethod

parameters(*args, **kwargs) -> UnsetParamsType

Parameters function to return UnsetParams.

Returns:

Name Type Description
UnsetParamsType UnsetParamsType

Special unset parameters value.

Source code in src/contraqctor/contract/base.py
495
496
497
498
499
500
501
502
@staticmethod
def parameters(*args, **kwargs) -> _typing.UnsetParamsType:
    """Parameters function to return UnsetParams.

    Returns:
        UnsetParamsType: Special unset parameters value.
    """
    return _typing.UnsetParams

read

read(*args, **kwargs) -> List[DataStream]

Read data from the collection.

Returns:

Type Description
List[DataStream]

List[DataStream]: The pre-set data streams.

Raises:

Type Description
ValueError

If data streams have not been set yet.

Source code in src/contraqctor/contract/base.py
512
513
514
515
516
517
518
519
520
521
522
523
524
@override
def read(self, *args, **kwargs) -> List[DataStream]:
    """Read data from the collection.

    Returns:
        List[DataStream]: The pre-set data streams.

    Raises:
        ValueError: If data streams have not been set yet.
    """
    if not self.has_data:
        raise ValueError("Data streams have not been read yet.")
    return self._data

bind_data_streams

bind_data_streams(data_streams: List[DataStream]) -> Self

Bind a list of data streams to the collection.

Parameters:

Name Type Description Default
data_streams List[DataStream]

List of data streams to include in the collection.

required

Returns:

Name Type Description
Self Self

The collection instance for method chaining.

Raises:

Type Description
ValueError

If data streams have already been set.

Source code in src/contraqctor/contract/base.py
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
def bind_data_streams(self, data_streams: List[DataStream]) -> Self:
    """Bind a list of data streams to the collection.

    Args:
        data_streams: List of data streams to include in the collection.

    Returns:
        Self: The collection instance for method chaining.

    Raises:
        ValueError: If data streams have already been set.
    """
    if self.has_data:
        raise ValueError("Data streams are already set. Cannot bind again.")
    self._data = data_streams
    self._update_hashmap()
    return self

add_stream

add_stream(stream: DataStream) -> Self

Add a new data stream to the collection.

Parameters:

Name Type Description Default
stream DataStream

Data stream to add to the collection.

required

Returns:

Name Type Description
Self Self

The collection instance for method chaining.

Raises:

Type Description
KeyError

If a stream with the same name already exists.

Examples:

from contraqctor.contract import json, DataStreamCollection

# Create an empty collection
collection = DataStreamCollection("api_data", [])

# Add streams
collection.add_stream(
    json.Json("config", reader_params=json.JsonParams(path="config.json"))
)

# Load the data
collection.load_all()
Source code in src/contraqctor/contract/base.py
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
def add_stream(self, stream: DataStream) -> Self:
    """Add a new data stream to the collection.

    Args:
        stream: Data stream to add to the collection.

    Returns:
        Self: The collection instance for method chaining.

    Raises:
        KeyError: If a stream with the same name already exists.

    Examples:
        ```python
        from contraqctor.contract import json, DataStreamCollection

        # Create an empty collection
        collection = DataStreamCollection("api_data", [])

        # Add streams
        collection.add_stream(
            json.Json("config", reader_params=json.JsonParams(path="config.json"))
        )

        # Load the data
        collection.load_all()
        ```
    """
    if not self.has_data:
        self._data = [stream]
        self._update_hashmap()
        return self

    if stream.name in self._hashmap:
        raise KeyError(f"Stream with name: '{stream.name}' already exists in data streams.")

    self._data.append(stream)
    self._update_hashmap()
    return self

remove_stream

remove_stream(name: str) -> None

Remove a data stream from the collection.

Parameters:

Name Type Description Default
name str

Name of the data stream to remove.

required

Raises:

Type Description
ValueError

If data streams have not been set yet.

KeyError

If no stream with the given name exists.

Source code in src/contraqctor/contract/base.py
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
def remove_stream(self, name: str) -> None:
    """Remove a data stream from the collection.

    Args:
        name: Name of the data stream to remove.

    Raises:
        ValueError: If data streams have not been set yet.
        KeyError: If no stream with the given name exists.
    """
    if not self.has_data:
        raise ValueError("Data streams have not been read yet. Cannot access data streams.")

    if name not in self._hashmap:
        raise KeyError(f"Data stream with name '{name}' not found in data streams.")
    self._data.remove(self._hashmap[name])
    self._update_hashmap()
    return

from_data_stream classmethod

from_data_stream(data_stream: DataStream) -> Self

Create a DataStreamCollection from a DataStream object.

Factory method to convert a single data stream or collection into a DataStreamCollection.

Parameters:

Name Type Description Default
data_stream DataStream

Source data stream to convert.

required

Returns:

Name Type Description
DataStreamCollection Self

New collection containing the source stream's data.

Raises:

Type Description
TypeError

If the source is not a DataStream.

ValueError

If the source has not been loaded yet.

Source code in src/contraqctor/contract/base.py
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
@classmethod
def from_data_stream(cls, data_stream: DataStream) -> Self:
    """Create a DataStreamCollection from a DataStream object.

    Factory method to convert a single data stream or collection into a DataStreamCollection.

    Args:
        data_stream: Source data stream to convert.

    Returns:
        DataStreamCollection: New collection containing the source stream's data.

    Raises:
        TypeError: If the source is not a DataStream.
        ValueError: If the source has not been loaded yet.
    """
    if not isinstance(data_stream, DataStream):
        raise TypeError("data_stream must be an instance of DataStream.")
    if not data_stream.has_data:
        raise ValueError("DataStream has not been loaded yet. Cannot create DataStreamCollection.")
    data = data_stream.data if data_stream.is_collection else [data_stream.data]
    return cls(name=data_stream.name, data_streams=data, description=data_stream.description)

bind_reader_params

bind_reader_params(params: TReaderParams) -> Self

Bind reader parameters to the data stream.

Parameters:

Name Type Description Default
params TReaderParams

Parameters to bind to the data stream's reader.

required

Returns:

Name Type Description
Self Self

The data stream instance for method chaining.

Raises:

Type Description
ValueError

If reader parameters have already been set.

Source code in src/contraqctor/contract/base.py
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
def bind_reader_params(self, params: _typing.TReaderParams) -> Self:
    """Bind reader parameters to the data stream.

    Args:
        params: Parameters to bind to the data stream's reader.

    Returns:
        Self: The data stream instance for method chaining.

    Raises:
        ValueError: If reader parameters have already been set.
    """
    if not _typing.is_unset(self._reader_params):
        raise ValueError("Reader parameters are already set. Cannot bind again.")
    self._reader_params = params
    return self

at

at(name: str) -> TDataStream

Get a child data stream by name.

Parameters:

Name Type Description Default
name str

Name of the child data stream to retrieve.

required

Returns:

Name Type Description
TDataStream TDataStream

The child data stream with the given name.

Raises:

Type Description
ValueError

If data has not been loaded yet.

KeyError

If no child stream with the given name exists.

Source code in src/contraqctor/contract/base.py
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
@override
def at(self, name: str) -> TDataStream:
    """Get a child data stream by name.

    Args:
        name: Name of the child data stream to retrieve.

    Returns:
        TDataStream: The child data stream with the given name.

    Raises:
        ValueError: If data has not been loaded yet.
        KeyError: If no child stream with the given name exists.
    """
    if not self.has_data:
        raise ValueError("data streams have not been read yet. Cannot access data streams.")
    if name in self._hashmap:
        return self._hashmap[name]
    else:
        raise KeyError(f"Stream with name: '{name}' not found in data streams.")

load

load()

Load data for this collection.

Overrides the base method to add validation that loaded data is a list of DataStreams.

Returns:

Name Type Description
Self

The collection instance for method chaining.

Raises:

Type Description
ValueError

If loaded data is not a list of DataStreams.

Source code in src/contraqctor/contract/base.py
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
@override
def load(self):
    """Load data for this collection.

    Overrides the base method to add validation that loaded data is a list of DataStreams.

    Returns:
        Self: The collection instance for method chaining.

    Raises:
        ValueError: If loaded data is not a list of DataStreams.
    """
    super().load()
    if not isinstance(self._data, list):
        self._data = _typing.UnsetData
        raise ValueError("Data must be a list of DataStreams.")
    self._update_hashmap()
    return self

load_all

load_all(
    strict: bool = False,
) -> list[tuple[DataStream, Exception], None, None]

Recursively load this data stream and all child streams.

Performs depth-first traversal to load all streams in the hierarchy.

Parameters:

Name Type Description Default
strict bool

If True, raises exceptions immediately; otherwise collects and returns them.

False

Returns:

Name Type Description
list list[tuple[DataStream, Exception], None, None]

List of tuples containing streams and exceptions that occurred during loading.

Raises:

Type Description
Exception

If strict is True and an exception occurs during loading.

Examples:

# Load all streams and handle errors
errors = collection.load_all(strict=False)

if errors:
    for stream, error in errors:
        print(f"Error loading {stream.name}: {error}")
Source code in src/contraqctor/contract/base.py
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
def load_all(self, strict: bool = False) -> list[tuple["DataStream", Exception], None, None]:
    """Recursively load this data stream and all child streams.

    Performs depth-first traversal to load all streams in the hierarchy.

    Args:
        strict: If True, raises exceptions immediately; otherwise collects and returns them.

    Returns:
        list: List of tuples containing streams and exceptions that occurred during loading.

    Raises:
        Exception: If strict is True and an exception occurs during loading.

    Examples:
        ```python
        # Load all streams and handle errors
        errors = collection.load_all(strict=False)

        if errors:
            for stream, error in errors:
                print(f"Error loading {stream.name}: {error}")
        ```
    """
    self.load()
    exceptions = []
    for stream in self:
        if stream is None:
            continue
        try:
            exceptions += stream.load_all(strict=strict)
        except Exception as e:
            if strict:
                raise e
            exceptions.append((stream, e))
    return exceptions

iter_all

iter_all() -> Generator[DataStream, None, None]

Iterator for all child data streams, including nested collections.

Implements a depth-first traversal of the stream hierarchy.

Yields:

Name Type Description
DataStream DataStream

All recursively yielded child data streams.

Source code in src/contraqctor/contract/base.py
436
437
438
439
440
441
442
443
444
445
446
447
448
def iter_all(self) -> Generator[DataStream, None, None]:
    """Iterator for all child data streams, including nested collections.

    Implements a depth-first traversal of the stream hierarchy.

    Yields:
        DataStream: All recursively yielded child data streams.
    """
    for value in self:
        if isinstance(value, DataStream):
            yield value
        if isinstance(value, DataStreamCollectionBase):
            yield from value.iter_all()

Dataset

Dataset(
    name: str,
    data_streams: List[DataStream],
    *,
    version: str | Version = "0.0.0",
    description: Optional[str] = None,
)

Bases: DataStreamCollection

A version-tracked collection of data streams.

Extends DataStreamCollection by adding semantic versioning support.

Parameters:

Name Type Description Default
name str

Name identifier for the dataset.

required
data_streams List[DataStream]

List of data streams to include in the dataset.

required
version str | Version

Semantic version string or Version object. Defaults to "0.0.0".

'0.0.0'
description Optional[str]

Optional description of the dataset.

None

Examples:

from contraqctor.contract import text, csv, Dataset

# Create streams
text_stream = text.Text("notes", reader_params=text.TextParams(path="notes.txt"))
csv_stream = csv.Csv("data", reader_params=csv.CsvParams(path="data.csv"))

# Create a versioned dataset
dataset = Dataset(
    "experiment_results",
    [text_stream, csv_stream],
    version="1.2.3"
)

# Load the dataset
dataset.load_all(strict=True)

# Access streams
txt = dataset["notes"].data
csv_data = dataset["data"].data

print(f"Dataset version: {dataset.version}")

Initializes a Dataset with a version and a list of data streams.

Source code in src/contraqctor/contract/base.py
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
@override
def __init__(
    self,
    name: str,
    data_streams: List[DataStream],
    *,
    version: str | Version = "0.0.0",
    description: Optional[str] = None,
) -> None:
    """Initializes a Dataset with a version and a list of data streams."""
    super().__init__(
        name=name,
        data_streams=data_streams,
        description=description,
    )
    self._version = self._parse_semver(version)

version property

version: Version

Get the semantic version of the dataset.

Returns:

Name Type Description
Version Version

Semantic version object.

name property

name: str

Get the name of the data stream.

Returns:

Name Type Description
str str

Name identifier of the data stream.

resolved_name property

resolved_name: str

Get the full hierarchical name of the data stream.

Generates a path-like name showing the stream's position in the hierarchy, using '::' as a separator between parent and child names.

Returns:

Name Type Description
str str

The fully resolved name including all parent names.

description property

description: Optional[str]

Get the description of the data stream.

Returns:

Type Description
Optional[str]

Optional[str]: Description of the data stream, or None if not provided.

parent property

Get the parent data stream.

Returns:

Type Description
Optional[DataStream]

Optional[DataStream]: Parent data stream, or None if this is a root stream.

is_collection property

is_collection: bool

Check if this data stream is a collection of other streams.

Returns:

Name Type Description
bool bool

True if this is a collection stream, False otherwise.

reader_params property

reader_params: TReaderParams

Get the parameters for the data reader.

Returns:

Name Type Description
TReaderParams TReaderParams

Parameters for the data reader.

has_data property

has_data: bool

Check if the data stream has loaded data.

Returns:

Name Type Description
bool bool

True if data has been loaded, False otherwise.

data property

data: TData

Get the loaded data.

Returns:

Name Type Description
TData TData

The loaded data.

Raises:

Type Description
ValueError

If data has not been loaded yet.

read

read(*args, **kwargs) -> List[DataStream]

Read data from the collection.

Returns:

Type Description
List[DataStream]

List[DataStream]: The pre-set data streams.

Raises:

Type Description
ValueError

If data streams have not been set yet.

Source code in src/contraqctor/contract/base.py
512
513
514
515
516
517
518
519
520
521
522
523
524
@override
def read(self, *args, **kwargs) -> List[DataStream]:
    """Read data from the collection.

    Returns:
        List[DataStream]: The pre-set data streams.

    Raises:
        ValueError: If data streams have not been set yet.
    """
    if not self.has_data:
        raise ValueError("Data streams have not been read yet.")
    return self._data

bind_reader_params

bind_reader_params(params: TReaderParams) -> Self

Bind reader parameters to the data stream.

Parameters:

Name Type Description Default
params TReaderParams

Parameters to bind to the data stream's reader.

required

Returns:

Name Type Description
Self Self

The data stream instance for method chaining.

Raises:

Type Description
ValueError

If reader parameters have already been set.

Source code in src/contraqctor/contract/base.py
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
def bind_reader_params(self, params: _typing.TReaderParams) -> Self:
    """Bind reader parameters to the data stream.

    Args:
        params: Parameters to bind to the data stream's reader.

    Returns:
        Self: The data stream instance for method chaining.

    Raises:
        ValueError: If reader parameters have already been set.
    """
    if not _typing.is_unset(self._reader_params):
        raise ValueError("Reader parameters are already set. Cannot bind again.")
    self._reader_params = params
    return self

at

at(name: str) -> TDataStream

Get a child data stream by name.

Parameters:

Name Type Description Default
name str

Name of the child data stream to retrieve.

required

Returns:

Name Type Description
TDataStream TDataStream

The child data stream with the given name.

Raises:

Type Description
ValueError

If data has not been loaded yet.

KeyError

If no child stream with the given name exists.

Source code in src/contraqctor/contract/base.py
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
@override
def at(self, name: str) -> TDataStream:
    """Get a child data stream by name.

    Args:
        name: Name of the child data stream to retrieve.

    Returns:
        TDataStream: The child data stream with the given name.

    Raises:
        ValueError: If data has not been loaded yet.
        KeyError: If no child stream with the given name exists.
    """
    if not self.has_data:
        raise ValueError("data streams have not been read yet. Cannot access data streams.")
    if name in self._hashmap:
        return self._hashmap[name]
    else:
        raise KeyError(f"Stream with name: '{name}' not found in data streams.")

load

load()

Load data for this collection.

Overrides the base method to add validation that loaded data is a list of DataStreams.

Returns:

Name Type Description
Self

The collection instance for method chaining.

Raises:

Type Description
ValueError

If loaded data is not a list of DataStreams.

Source code in src/contraqctor/contract/base.py
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
@override
def load(self):
    """Load data for this collection.

    Overrides the base method to add validation that loaded data is a list of DataStreams.

    Returns:
        Self: The collection instance for method chaining.

    Raises:
        ValueError: If loaded data is not a list of DataStreams.
    """
    super().load()
    if not isinstance(self._data, list):
        self._data = _typing.UnsetData
        raise ValueError("Data must be a list of DataStreams.")
    self._update_hashmap()
    return self

load_all

load_all(
    strict: bool = False,
) -> list[tuple[DataStream, Exception], None, None]

Recursively load this data stream and all child streams.

Performs depth-first traversal to load all streams in the hierarchy.

Parameters:

Name Type Description Default
strict bool

If True, raises exceptions immediately; otherwise collects and returns them.

False

Returns:

Name Type Description
list list[tuple[DataStream, Exception], None, None]

List of tuples containing streams and exceptions that occurred during loading.

Raises:

Type Description
Exception

If strict is True and an exception occurs during loading.

Examples:

# Load all streams and handle errors
errors = collection.load_all(strict=False)

if errors:
    for stream, error in errors:
        print(f"Error loading {stream.name}: {error}")
Source code in src/contraqctor/contract/base.py
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
def load_all(self, strict: bool = False) -> list[tuple["DataStream", Exception], None, None]:
    """Recursively load this data stream and all child streams.

    Performs depth-first traversal to load all streams in the hierarchy.

    Args:
        strict: If True, raises exceptions immediately; otherwise collects and returns them.

    Returns:
        list: List of tuples containing streams and exceptions that occurred during loading.

    Raises:
        Exception: If strict is True and an exception occurs during loading.

    Examples:
        ```python
        # Load all streams and handle errors
        errors = collection.load_all(strict=False)

        if errors:
            for stream, error in errors:
                print(f"Error loading {stream.name}: {error}")
        ```
    """
    self.load()
    exceptions = []
    for stream in self:
        if stream is None:
            continue
        try:
            exceptions += stream.load_all(strict=strict)
        except Exception as e:
            if strict:
                raise e
            exceptions.append((stream, e))
    return exceptions

iter_all

iter_all() -> Generator[DataStream, None, None]

Iterator for all child data streams, including nested collections.

Implements a depth-first traversal of the stream hierarchy.

Yields:

Name Type Description
DataStream DataStream

All recursively yielded child data streams.

Source code in src/contraqctor/contract/base.py
436
437
438
439
440
441
442
443
444
445
446
447
448
def iter_all(self) -> Generator[DataStream, None, None]:
    """Iterator for all child data streams, including nested collections.

    Implements a depth-first traversal of the stream hierarchy.

    Yields:
        DataStream: All recursively yielded child data streams.
    """
    for value in self:
        if isinstance(value, DataStream):
            yield value
        if isinstance(value, DataStreamCollectionBase):
            yield from value.iter_all()

parameters staticmethod

parameters(*args, **kwargs) -> UnsetParamsType

Parameters function to return UnsetParams.

Returns:

Name Type Description
UnsetParamsType UnsetParamsType

Special unset parameters value.

Source code in src/contraqctor/contract/base.py
495
496
497
498
499
500
501
502
@staticmethod
def parameters(*args, **kwargs) -> _typing.UnsetParamsType:
    """Parameters function to return UnsetParams.

    Returns:
        UnsetParamsType: Special unset parameters value.
    """
    return _typing.UnsetParams

bind_data_streams

bind_data_streams(data_streams: List[DataStream]) -> Self

Bind a list of data streams to the collection.

Parameters:

Name Type Description Default
data_streams List[DataStream]

List of data streams to include in the collection.

required

Returns:

Name Type Description
Self Self

The collection instance for method chaining.

Raises:

Type Description
ValueError

If data streams have already been set.

Source code in src/contraqctor/contract/base.py
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
def bind_data_streams(self, data_streams: List[DataStream]) -> Self:
    """Bind a list of data streams to the collection.

    Args:
        data_streams: List of data streams to include in the collection.

    Returns:
        Self: The collection instance for method chaining.

    Raises:
        ValueError: If data streams have already been set.
    """
    if self.has_data:
        raise ValueError("Data streams are already set. Cannot bind again.")
    self._data = data_streams
    self._update_hashmap()
    return self

add_stream

add_stream(stream: DataStream) -> Self

Add a new data stream to the collection.

Parameters:

Name Type Description Default
stream DataStream

Data stream to add to the collection.

required

Returns:

Name Type Description
Self Self

The collection instance for method chaining.

Raises:

Type Description
KeyError

If a stream with the same name already exists.

Examples:

from contraqctor.contract import json, DataStreamCollection

# Create an empty collection
collection = DataStreamCollection("api_data", [])

# Add streams
collection.add_stream(
    json.Json("config", reader_params=json.JsonParams(path="config.json"))
)

# Load the data
collection.load_all()
Source code in src/contraqctor/contract/base.py
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
def add_stream(self, stream: DataStream) -> Self:
    """Add a new data stream to the collection.

    Args:
        stream: Data stream to add to the collection.

    Returns:
        Self: The collection instance for method chaining.

    Raises:
        KeyError: If a stream with the same name already exists.

    Examples:
        ```python
        from contraqctor.contract import json, DataStreamCollection

        # Create an empty collection
        collection = DataStreamCollection("api_data", [])

        # Add streams
        collection.add_stream(
            json.Json("config", reader_params=json.JsonParams(path="config.json"))
        )

        # Load the data
        collection.load_all()
        ```
    """
    if not self.has_data:
        self._data = [stream]
        self._update_hashmap()
        return self

    if stream.name in self._hashmap:
        raise KeyError(f"Stream with name: '{stream.name}' already exists in data streams.")

    self._data.append(stream)
    self._update_hashmap()
    return self

remove_stream

remove_stream(name: str) -> None

Remove a data stream from the collection.

Parameters:

Name Type Description Default
name str

Name of the data stream to remove.

required

Raises:

Type Description
ValueError

If data streams have not been set yet.

KeyError

If no stream with the given name exists.

Source code in src/contraqctor/contract/base.py
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
def remove_stream(self, name: str) -> None:
    """Remove a data stream from the collection.

    Args:
        name: Name of the data stream to remove.

    Raises:
        ValueError: If data streams have not been set yet.
        KeyError: If no stream with the given name exists.
    """
    if not self.has_data:
        raise ValueError("Data streams have not been read yet. Cannot access data streams.")

    if name not in self._hashmap:
        raise KeyError(f"Data stream with name '{name}' not found in data streams.")
    self._data.remove(self._hashmap[name])
    self._update_hashmap()
    return

from_data_stream classmethod

from_data_stream(data_stream: DataStream) -> Self

Create a DataStreamCollection from a DataStream object.

Factory method to convert a single data stream or collection into a DataStreamCollection.

Parameters:

Name Type Description Default
data_stream DataStream

Source data stream to convert.

required

Returns:

Name Type Description
DataStreamCollection Self

New collection containing the source stream's data.

Raises:

Type Description
TypeError

If the source is not a DataStream.

ValueError

If the source has not been loaded yet.

Source code in src/contraqctor/contract/base.py
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
@classmethod
def from_data_stream(cls, data_stream: DataStream) -> Self:
    """Create a DataStreamCollection from a DataStream object.

    Factory method to convert a single data stream or collection into a DataStreamCollection.

    Args:
        data_stream: Source data stream to convert.

    Returns:
        DataStreamCollection: New collection containing the source stream's data.

    Raises:
        TypeError: If the source is not a DataStream.
        ValueError: If the source has not been loaded yet.
    """
    if not isinstance(data_stream, DataStream):
        raise TypeError("data_stream must be an instance of DataStream.")
    if not data_stream.has_data:
        raise ValueError("DataStream has not been loaded yet. Cannot create DataStreamCollection.")
    data = data_stream.data if data_stream.is_collection else [data_stream.data]
    return cls(name=data_stream.name, data_streams=data, description=data_stream.description)

FilePathBaseParam dataclass

FilePathBaseParam(path: PathLike)

Bases: ABC

Abstract base class for file-based reader parameters.

Base parameter class for readers that access files by path.

Attributes:

Name Type Description
path PathLike

Path to the file or directory to read from.