Datasets¶
datasets
¶
Dataset storage and serialization infrastructure.
AbstractDataset
¶
Bases: ABC
Abstract base class for type-based serialization.
Subclasses auto-register via init_subclass. Supports lazy plugin discovery via entry points when a type/format combination is not found.
Examples:
Create a custom dataset using class parameters:
>>> class JsonDataset(AbstractDataset, format="json", types=dict, extensions="json"):
... def serialize(self, obj: dict) -> bytes:
... import json
...
... return json.dumps(obj).encode("utf-8")
...
... def deserialize(self, data: bytes) -> dict:
... import json
...
... return json.loads(data.decode("utf-8"))
Or using class variables:
>>> class YamlDataset(AbstractDataset):
... format = "yaml"
... types = (dict,)
... extensions = ("yaml", "yml")
...
... def serialize(self, obj: dict) -> bytes: ...
... def deserialize(self, data: bytes) -> dict: ...
The dataset is now registered and can be retrieved:
>>> dataset_class = AbstractDataset.get(dict, "json")
>>> dataset = dataset_class()
>>> dataset.serialize({"key": "value"})
b'{"key": "value"}'
Source code in src/daglite/datasets/base.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 | |
__init_subclass__
¶
__init_subclass__(
*,
format: str | None = None,
types: type | tuple[type, ...] | None = None,
extensions: str | tuple[str, ...] | None = None,
**kwargs: Any,
) -> None
Auto-register subclasses in the dataset registry.
Parameters can be provided as class parameters or as class variables. Class parameters take precedence over class variables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
format
|
str | None
|
Format identifier (e.g., 'pickle', 'parquet'). If None, checks for class variable. If neither exists, this is an abstract intermediate class and won't be registered. |
None
|
types
|
type | tuple[type, ...] | None
|
Python type(s) this dataset can serialize. Can be a single type or a tuple of types. |
None
|
extensions
|
str | tuple[str, ...] | None
|
Optional file extension(s) for format inference (without dots). Can be a single string or a tuple of strings. |
None
|
kwargs
|
Any
|
Additional keyword arguments. |
{}
|
Examples:
Using class parameters:
Using class variables:
>>> class CsvDataset(AbstractDataset):
... format = "csv"
... types = (dict,)
... extensions = ("csv",)
... ...
Source code in src/daglite/datasets/base.py
deserialize
abstractmethod
¶
Convert bytes back to a value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
Serialized bytes to deserialize. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The deserialized value. |
get
classmethod
¶
Look up and return the Dataset type for a type/format combination.
Supports lazy plugin discovery - if the combination isn't found, attempts to load relevant entry points before failing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_
|
type
|
The Python type to serialize/deserialize. |
required |
format
|
str
|
The format identifier (e.g., 'pickle', 'parquet'). |
required |
Returns:
| Type | Description |
|---|---|
type[AbstractDataset]
|
An instantiated Dataset that can handle the type/format. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no dataset is registered for the type/format. |
Examples:
>>> dataset_cls = AbstractDataset.get(str, "text")
>>> dataset = dataset_cls()
>>> dataset.serialize("hello")
b'hello'
Source code in src/daglite/datasets/base.py
get_extension
classmethod
¶
Get the preferred file extension for a type/format combination.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_
|
type
|
The Python type. |
required |
format
|
str
|
The format identifier. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
The preferred file extension (without dot), or None if not found. |
Examples:
Source code in src/daglite/datasets/base.py
get_formats_for_type
classmethod
¶
Get all registered formats for a type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_
|
type
|
The Python type. |
required |
Returns:
| Type | Description |
|---|---|
set[str]
|
Set of format identifiers registered for this type. |
Examples:
Source code in src/daglite/datasets/base.py
infer_format
classmethod
¶
Infer the format for a type, optionally using extension as a hint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_
|
type
|
The Python type. |
required |
extension
|
str | None
|
Optional file extension (without dot) to help infer format. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The inferred format identifier. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no format can be inferred for the type. |
Examples:
>>> AbstractDataset.infer_format(str, "txt")
'text'
>>> AbstractDataset.infer_format(dict, "pkl")
'pickle'
Source code in src/daglite/datasets/base.py
load_plugins
classmethod
¶
Explicitly load dataset plugins.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*names
|
str
|
Plugin names to load (e.g., "pandas", "numpy"). If empty, loads all installed plugins. |
()
|
auto_discover
|
bool | None
|
If provided, sets whether lazy discovery is enabled. Set to False to disable automatic plugin loading on lookup failure. |
None
|
Examples:
Load specific plugins:
Load all installed plugins:
Disable auto-discovery (strict mode):
Source code in src/daglite/datasets/base.py
serialize
abstractmethod
¶
Convert a Python object to bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Python object to serialize. |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
Serialized bytes representation. |
DatasetStore
¶
High-level store that handles serialization via Datasets.
This wraps a Driver (like FileDriver) and adds automatic serialization/deserialization using the Dataset registry.
Format is inferred from the file extension in the key.
This is the user-facing API - it accepts Python objects and handles all serialization internally.
Examples:
>>> from daglite.datasets import DatasetStore
>>> store = DatasetStore("/tmp/outputs")
>>> store.save("data.pkl", {"data": [1, 2, 3]})
>>> store.load("data.pkl", dict)
{'data': [1, 2, 3]}
Source code in src/daglite/datasets/store.py
30 31 32 33 34 35 36 37 38 39 40 41 42 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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |
__getstate__
¶
__init__
¶
Initialize with a driver.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
driver
|
Driver | str
|
A Driver instance or string path (creates FileDriver). |
required |
Source code in src/daglite/datasets/store.py
__setstate__
¶
delete
¶
exists
¶
list_keys
¶
load
¶
load(
key: str,
return_type: type[T] | None = None,
format: str | None = None,
options: dict[str, Any] | None = None,
) -> T
Load a value using Dataset deserialization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Storage key/path. Format hint from driver (e.g., extension). |
required |
return_type
|
type[T] | None
|
Expected return type. If None, uses pickle format. |
None
|
format
|
str | None
|
Serialization format. If None, inferred from type and/or driver hint. |
None
|
options
|
dict[str, Any] | None
|
Additional options passed to the Dataset's load method. |
None
|
Returns:
| Type | Description |
|---|---|
T
|
The deserialized value. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If key not found. |
Source code in src/daglite/datasets/store.py
save
¶
Save a value using Dataset serialization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Storage key/path. Format hint from driver (e.g., extension). |
required |
value
|
Any
|
Value to serialize and save. |
required |
format
|
str | None
|
Serialization format. If None, inferred from type and/or driver hint. |
None
|
options
|
dict[str, Any] | None
|
Additional options passed to the Dataset's save method. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The actual path where data was stored. |
Source code in src/daglite/datasets/store.py
load_dataset
¶
load_dataset(
key: str,
return_type: type[R] | None = None,
*,
format: str | None = None,
store: DatasetStore | str | None = None,
options: dict[str, Any] | None = None,
) -> R | Any
Load a dataset from the active (or explicitly provided) dataset store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Storage key/path. May contain |
required |
return_type
|
type[R] | None
|
Expected Python type for deserialization dispatch. |
None
|
format
|
str | None
|
Serialization format hint (e.g. |
None
|
store
|
DatasetStore | str | None
|
Explicit |
None
|
options
|
dict[str, Any] | None
|
Additional options forwarded to the Dataset constructor. |
None
|
Returns:
| Type | Description |
|---|---|
R | Any
|
The deserialized Python object. |
Source code in src/daglite/composers.py
save_dataset
¶
save_dataset(
key: str,
value: Any,
*,
format: str | None = None,
store: DatasetStore | str | None = None,
options: dict[str, Any] | None = None,
) -> str
Save a value to the active (or explicitly provided) dataset store.
When a DatasetReporter is available (inside a session), the save is routed through
the reporter so that process/remote backends push the write back to the coordinator.
The store is resolved from the context chain: explicit argument -> task context -> session context -> global settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Storage key/path. May contain |
required |
value
|
Any
|
The Python object to serialize and persist. |
required |
format
|
str | None
|
Serialization format hint (e.g. |
None
|
store
|
DatasetStore | str | None
|
Explicit |
None
|
options
|
dict[str, Any] | None
|
Additional options forwarded to the Dataset constructor. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The actual path where data was stored. |