mixle.data.core module¶
DataSource – a lazy, typed, structured reference to data that yields encoder-ready records.
This is the single concept that replaces the “is it a list? a DataFrame? an RDD? a SQL cursor?” branching
scattered across call sites. A DataSource carries a Schema (logical field
types) and a SampleStructure (its exchangeability class), and knows how to
yield records() and partition itself safely.
It is purely additive: seq_encode(list) and seq_encode(rdd) are untouched fast paths;
seq_encode gains one branch that recognizes a DataSource and routes through its structure-aware
encoder, returning the same [(count, payload)] shape consumers already expect.
- class DataSource(*args, **kwargs)[source]
Bases:
ProtocolA lazy, typed, structured source of encoder-ready records.
- records()[source]
Yield raw records compatible with an encoder’s input type.
- class MaterializedSource(data, structure=EXCHANGEABLE, schema=None)[source]
Bases:
objectAn in-memory
DataSourcewrapping aSequence– what a bare list becomes.- Parameters:
data (Sequence[Any])
structure (SampleStructure)
schema (Schema | None)
- records()[source]
Return conformed records from the owned replayable snapshot.
- materialize()[source]
Return a caller-owned list copy of the conformed snapshot.
- partition(n, *, by=None)[source]
Split into
nstructure-safe sub-sources (group-aware for partially-exchangeable data).
- class LazySource(factory, structure=EXCHANGEABLE, schema=None, length=None)[source]
Bases:
objectA
DataSourcethat defers reading to a records factory and materializes on demand.Connectors (Parquet, SQL, CSV, …) return one of these so
open(...)does no I/O until the data is actually encoded; the records are read (and schema-coerced) once and cached.- Parameters:
factory (Any)
structure (SampleStructure)
schema (Schema | None)
length (int | None)
- materialize()[source]
Return a caller-owned list copy of the immutable, conformed cache.
- records()[source]
Return an iterator over the cached materialized records.
- partition(n, *, by=None)[source]
Materialize and split into
nstructure-aware in-memory sources.
- as_source(data, structure=EXCHANGEABLE, schema=None)[source]
Coerce
datato aDataSource(pass a source through; wrap a sequence as materialized).- Parameters:
data (Any)
structure (SampleStructure)
schema (Schema | None)
- Return type:
DataSource
- num_chunks_for(size, num_chunks=1, chunk_size=None)[source]
Resolve the chunk count from an explicit
num_chunksor a targetchunk_size(as seq_encode does).Both controls are validated as exact positive integers before any arithmetic:
chunk_size=0used to raise a bareZeroDivisionError, and a negative/fractionalchunk_sizeor a nonpositive/fractionalnum_chunksused to silently collapse to one chunk instead of failing – these bound memory, so silently weakening them can materially change an execution plan. A non-defaultnum_chunkssupplied together withchunk_sizeis rejected as conflicting rather than silently lettingchunk_sizewin.