Writing files — write and writeStream¶
DataGenerator.write writes a batch once; DataGenerator.writeStream writes a microbatch per
trigger until a timeout. Both take .format(), .mode(), .option() / .options().
import os
from rand_engine import DataGenerator
spec = {
"id": {"method": "pk", "kwargs": {"start": 1}},
"name": {"method": "distincts", "kwargs": {"distincts": ["ana", "bia"]}},
"active": {"method": "booleans", "kwargs": {}},
}
gen = DataGenerator(spec, seed=1).size(1_000)
gen.write.format("parquet").save("out/clients.parquet")
gen.write.format("csv").option("compression", "gzip").save("out/clients.csv")
gen.write.format("json").options(numFiles=4).mode("overwrite").save("out/clients_json")
assert os.path.exists("out/clients.parquet") and os.path.exists("out/clients.csv.gz")
assert len(os.listdir("out/clients_json")) == 4
Writer contract¶
csvandparquetare written by pyarrow;jsonby pandas (orient="records", one record per line).- Only the options below are accepted. Any other raises
RandEngineErrornaming it and listing the accepted ones, before any file is deleted or written.
| Format | Options |
|---|---|
csv |
sep (delimiter, default ,); compression: gzip, bz2, xz, zip; index: only False (Arrow writes no index) |
parquet |
compression: snappy (default), gzip, zstd, brotli, lz4 |
json |
force_ascii, indent, compression (gzip, bz2, xz, zip); orient: only "records" |
sqlite |
table (required, non-empty table name); overwrite or append; no file batching or file-format options |
write (batch) |
numFiles (default 1); maxRowsPerBatch (positive int; rows generated and written per batch, so memory stays bounded) |
writeStream |
timeout in seconds (default 20) |
- A column holding mixed types (e.g. ints and strings in one object column) raises
RandEngineErrornaming the column forcsvandparquet. - Parquet reads back equal to the frame in values, tz-aware columns included, and in dtype for every non-text column. A generated text column reads back as
pd.StringDtype("pyarrow")under the default read and as the frame'spd.ArrowDtype(pa.string())withpd.read_parquet(..., dtype_backend="pyarrow").
from rand_engine.validators.exceptions import RandEngineError
try:
gen.write.format("parquet").option("engine", "fastparquet").save("out/bad.parquet")
except RandEngineError as error:
assert "engine" in str(error) and "numFiles" in str(error)
else:
raise AssertionError("expected RandEngineError")
Spark: a text column with nulls holds pd.NA. To convert such a frame with spark.createDataFrame, enable spark.sql.execution.arrow.pyspark.enabled first. Without it, spark.createDataFrame raises PySparkTypeError ([CANNOT_MERGE_TYPE]).
Batch: write¶
save(path)withnumFiles1 writes one file atpath, the format and compression extension added when missing (out/clients.csv+gzip→out/clients.csv.gz; parquet never gets one).- With
numFiles> 1,path(extensions stripped) is a folder ofpart_<id>.<ext>files sharing thesizerows. The batch writer stages every new file beside the destination and commits only after staging succeeds. Onlymode("append")adds staged parts to an existing folder; every other mode replaces the destination folder when the commit succeeds. - A final file has one schema: with
maxRowsPerBatchevery batch of a file must produce the same schema, andmode("append")to a single file must match the existing one. - Keys continue across the files:
pkvalues never repeat between parts.
import pandas as pd
gen.write.format("csv").options(numFiles=3, sep=";").save("out/parts.csv")
parts = [pd.read_csv(f"out/parts/{f}", sep=";") for f in sorted(os.listdir("out/parts"))]
ids = pd.concat(parts)["id"]
assert len(ids) == 1_000 and ids.is_unique
spec_log = {"level": {"method": "distincts", "kwargs": {"distincts": ["info", "warn"]}}}
log = DataGenerator(spec_log, seed=2).size(1_000)
log.write.format("csv").options(numFiles=2, maxRowsPerBatch=100).save("out/log.csv")
log.write.format("csv").options(numFiles=2).mode("append").save("out/log.csv")
logs = os.listdir("out/log")
assert len(logs) == 4
assert sum(len(pd.read_csv(f"out/log/{f}")) for f in logs) == 2_000
SQLite table sink¶
SQLite is a batch-only output sink: use write.format("sqlite").option("table", name) and one
of overwrite or append. Identifiers are quoted and row values are bound. An overwrite stages
the selected table replacement while preserving unrelated tables; append is transactional. SQLite
stores bools and signed 64-bit integers as INTEGER, floats as REAL, strings and ISO dates as TEXT,
bytes as BLOB, and nulls as NULL. Mixed and all-null columns have no declared affinity. It does
not create a primary-key constraint from a RandSpec key, resume files, or accept numFiles,
maxRowsPerBatch, or other file options.
import sqlite3
from rand_engine.validators.exceptions import RandEngineError
sqlite_path = "out/clients.sqlite"
sqlite_table = 'client "events"'
gen.write.format("sqlite").option("table", sqlite_table).mode("overwrite").save(sqlite_path)
gen.write.format("sqlite").option("table", sqlite_table).mode("append").save(sqlite_path)
with sqlite3.connect(sqlite_path) as database:
quoted = sqlite_table.replace('"', '""')
assert database.execute(f'SELECT COUNT(*) FROM "{quoted}"').fetchone()[0] == 2_000
try:
gen.writeStream.format("sqlite").option("table", "events").start("out/events.sqlite")
except RandEngineError:
pass
else:
raise AssertionError("SQLite is batch-only")
Stream: writeStream¶
trigger(frequency) only configures the pause after each write and returns the stream writer.
start(path) writes one microbatch of size rows to path/part-<uuid>.<ext>, sleeps the
configured number of seconds and repeats until timeout seconds have passed. mode("overwrite")
(the default) clears the folder first. start blocks until the timeout.
gen.writeStream.format("json").trigger(frequency=0.1).option("timeout", 0.3).start("out/stream.json")
files = os.listdir("out/stream")
assert len(files) >= 2
CSV written by pyarrow¶
CSV output differs from pandas.DataFrame.to_csv:
- booleans are written
true/false; - integral floats are written
1, so they read back as int; - the header and every string value are quoted;
- a naive timestamp's fraction follows its column's unit:
ns.000000000,ussix digits,msthree,snone; - an all-midnight naive datetime column is written with its full time
(
2020-01-01 00:00:00.000000000), not a bare date; - a tz-aware column is written in its pandas string form, quoted
(
2024-01-01 00:00:00+00:00,…-03:00); - a missing timestamp (NaT) in a multi-column frame is an empty field, never
NaT; - in a one-column frame a null is written as a blank line, which
pd.read_csvskips by default, so that row is lost on read-back; - line endings are always LF, on every OS.
gen.write.format("csv").save("out/check.csv")
with open("out/check.csv", newline="") as file:
header, first = file.read().split("\n")[:2]
assert header == '"id","name","active"'
assert first.split(",")[2] in ("true", "false")
Returned writer reference¶
DataGenerator.write returns a FileBatchWriter; DataGenerator.writeStream
returns a FileStreamWriter. These are returned objects, not top-level
rand_engine exports. Both inherit the format, mode and option chain from
FileWriter; save completes a batch write, trigger configures a stream writer
and returns it, and start performs blocking stream writes until the timeout.
Configure the file format, write mode and options of write and writeStream.
SQLite is an output-only batch format with a required table option; stream writers refuse it.
Examples:
>>> import os, tempfile
>>> import pandas as pd
>>> from rand_engine import DataGenerator
>>> spec = {"age": {"method": "integers", "kwargs": {"min": 18, "max": 80}}}
>>> path = os.path.join(tempfile.mkdtemp(), "class.csv")
>>> DataGenerator(spec, seed=42).size(10).write.format("csv").save(path)
>>> len(pd.read_csv(path))
10
Source code in rand_engine/file_handlers/writer.py
5 6 7 8 9 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 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 | |
__map_methods()
¶
Map file formats to their corresponding FileHandler methods.
Source code in rand_engine/file_handlers/writer.py
33 34 35 36 37 38 39 | |
format(format)
¶
Set the output file format of the next save or start.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
format
|
str
|
|
required |
Returns:
| Type | Description |
|---|---|
FileWriter
|
This writer, for chaining. |
Examples:
>>> import os, tempfile
>>> import pandas as pd
>>> from rand_engine import DataGenerator
>>> spec = {"age": {"method": "integers", "kwargs": {"min": 18, "max": 80}}}
>>> path = os.path.join(tempfile.mkdtemp(), "format.parquet")
>>> DataGenerator(spec, seed=42).size(10).write.format("parquet").save(path)
>>> len(pd.read_parquet(path))
10
Source code in rand_engine/file_handlers/writer.py
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 | |
mode(write_mode)
¶
Set the write mode of the next save or start.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
write_mode
|
str
|
|
required |
Returns:
| Type | Description |
|---|---|
FileWriter
|
This writer, for chaining. |
Examples:
>>> import os, tempfile
>>> import pandas as pd
>>> from rand_engine import DataGenerator
>>> spec = {"age": {"method": "integers", "kwargs": {"min": 18, "max": 80}}}
>>> gen = DataGenerator(spec, seed=42).size(10)
>>> path = os.path.join(tempfile.mkdtemp(), "mode.csv")
>>> gen.write.format("csv").save(path)
>>> gen.write.mode("append").format("csv").save(path)
>>> len(pd.read_csv(path))
20
Source code in rand_engine/file_handlers/writer.py
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 | |
option(key, value)
¶
Set one write option of the next save or start.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Option name: |
required |
value
|
Any
|
Option value. |
required |
Returns:
| Type | Description |
|---|---|
FileWriter
|
This writer, for chaining. |
Examples:
>>> import os, tempfile
>>> from rand_engine import DataGenerator
>>> spec = {"age": {"method": "integers", "kwargs": {"min": 18, "max": 80}}}
>>> path = os.path.join(tempfile.mkdtemp(), "parts")
>>> DataGenerator(spec, seed=42).size(10).write.option("numFiles", 2).format("csv").save(path)
>>> len(os.listdir(path))
2
Source code in rand_engine/file_handlers/writer.py
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 | |
options(**kwargs)
¶
Set several write options of the next save or start at once.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Option names and values, such as |
{}
|
Returns:
| Type | Description |
|---|---|
FileWriter
|
This writer, for chaining. |
Examples:
>>> import os, tempfile
>>> import pandas as pd
>>> from rand_engine import DataGenerator
>>> spec = {"age": {"method": "integers", "kwargs": {"min": 18, "max": 80}}}
>>> path = os.path.join(tempfile.mkdtemp(), "options")
>>> gen = DataGenerator(spec, seed=42).size(10).write.options(numFiles=2, maxRowsPerBatch=3)
>>> gen.format("csv").save(path)
>>> sum(len(pd.read_csv(os.path.join(path, name))) for name in os.listdir(path))
10
Source code in rand_engine/file_handlers/writer.py
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 | |
Bases: FileWriter
Write the rows of a DataGenerator to files or one SQLite table, once per save.
Built by DataGenerator.write; chain format, mode and option first. SQLite
accepts only a table name, quotes identifiers, binds values, and supports overwrite or append.
Examples:
>>> import os, tempfile
>>> import pandas as pd
>>> from rand_engine import DataGenerator
>>> spec = {"age": {"method": "integers", "kwargs": {"min": 18, "max": 80}}}
>>> path = os.path.join(tempfile.mkdtemp(), "batch_ages.csv")
>>> DataGenerator(spec, seed=42).size(10).write.format("csv").save(path)
>>> len(pd.read_csv(path))
10
Source code in rand_engine/file_handlers/_writer_batch.py
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 | |
__map_methods()
¶
Map file formats to their corresponding FileHandler methods.
Source code in rand_engine/file_handlers/writer.py
33 34 35 36 37 38 39 | |
format(format)
¶
Set the output file format of the next save or start.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
format
|
str
|
|
required |
Returns:
| Type | Description |
|---|---|
FileWriter
|
This writer, for chaining. |
Examples:
>>> import os, tempfile
>>> import pandas as pd
>>> from rand_engine import DataGenerator
>>> spec = {"age": {"method": "integers", "kwargs": {"min": 18, "max": 80}}}
>>> path = os.path.join(tempfile.mkdtemp(), "format.parquet")
>>> DataGenerator(spec, seed=42).size(10).write.format("parquet").save(path)
>>> len(pd.read_parquet(path))
10
Source code in rand_engine/file_handlers/writer.py
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 | |
mode(write_mode)
¶
Set the write mode of the next save or start.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
write_mode
|
str
|
|
required |
Returns:
| Type | Description |
|---|---|
FileWriter
|
This writer, for chaining. |
Examples:
>>> import os, tempfile
>>> import pandas as pd
>>> from rand_engine import DataGenerator
>>> spec = {"age": {"method": "integers", "kwargs": {"min": 18, "max": 80}}}
>>> gen = DataGenerator(spec, seed=42).size(10)
>>> path = os.path.join(tempfile.mkdtemp(), "mode.csv")
>>> gen.write.format("csv").save(path)
>>> gen.write.mode("append").format("csv").save(path)
>>> len(pd.read_csv(path))
20
Source code in rand_engine/file_handlers/writer.py
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 | |
option(key, value)
¶
Set one write option of the next save or start.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Option name: |
required |
value
|
Any
|
Option value. |
required |
Returns:
| Type | Description |
|---|---|
FileWriter
|
This writer, for chaining. |
Examples:
>>> import os, tempfile
>>> from rand_engine import DataGenerator
>>> spec = {"age": {"method": "integers", "kwargs": {"min": 18, "max": 80}}}
>>> path = os.path.join(tempfile.mkdtemp(), "parts")
>>> DataGenerator(spec, seed=42).size(10).write.option("numFiles", 2).format("csv").save(path)
>>> len(os.listdir(path))
2
Source code in rand_engine/file_handlers/writer.py
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 | |
options(**kwargs)
¶
Set several write options of the next save or start at once.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Option names and values, such as |
{}
|
Returns:
| Type | Description |
|---|---|
FileWriter
|
This writer, for chaining. |
Examples:
>>> import os, tempfile
>>> import pandas as pd
>>> from rand_engine import DataGenerator
>>> spec = {"age": {"method": "integers", "kwargs": {"min": 18, "max": 80}}}
>>> path = os.path.join(tempfile.mkdtemp(), "options")
>>> gen = DataGenerator(spec, seed=42).size(10).write.options(numFiles=2, maxRowsPerBatch=3)
>>> gen.format("csv").save(path)
>>> sum(len(pd.read_csv(os.path.join(path, name))) for name in os.listdir(path))
10
Source code in rand_engine/file_handlers/writer.py
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 | |
save(path)
¶
Generate size rows and write them to path.
Writes one file at path by default; with the numFiles option, a directory of
that many part files. mode("append") adds the rows to what is already there.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Relative or absolute path; a single file gets the format's extension. |
required |
Raises:
| Type | Description |
|---|---|
RandEngineError
|
If |
Examples:
>>> import os, tempfile
>>> import pandas as pd
>>> from rand_engine import DataGenerator
>>> spec = {"age": {"method": "integers", "kwargs": {"min": 18, "max": 80}}}
>>> path = os.path.join(tempfile.mkdtemp(), "save_parts")
>>> DataGenerator(spec, seed=42).size(10).write.option("numFiles", 2).format("csv").save(path)
>>> len(os.listdir(path))
2
>>> sum(len(pd.read_csv(os.path.join(path, name))) for name in os.listdir(path))
10
Source code in rand_engine/file_handlers/_writer_batch.py
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 | |
Bases: FileWriter
Write a DataGenerator stream to a directory, one microbatch file per pass.
Built by DataGenerator.writeStream; chain format, trigger and
option("timeout", seconds) first, then call start(path).
Examples:
>>> from rand_engine import DataGenerator
>>> spec = {"age": {"method": "integers", "kwargs": {"min": 18, "max": 80}}}
>>> stream = DataGenerator(spec, seed=42).size(10).writeStream
>>> stream.format("csv").trigger(frequency=0.1).option("timeout", 0.3) is stream
True
Source code in rand_engine/file_handlers/_writer_stream.py
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 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 | |
__map_methods()
¶
Map file formats to their corresponding FileHandler methods.
Source code in rand_engine/file_handlers/writer.py
33 34 35 36 37 38 39 | |
format(format)
¶
Set the output file format of the next save or start.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
format
|
str
|
|
required |
Returns:
| Type | Description |
|---|---|
FileWriter
|
This writer, for chaining. |
Examples:
>>> import os, tempfile
>>> import pandas as pd
>>> from rand_engine import DataGenerator
>>> spec = {"age": {"method": "integers", "kwargs": {"min": 18, "max": 80}}}
>>> path = os.path.join(tempfile.mkdtemp(), "format.parquet")
>>> DataGenerator(spec, seed=42).size(10).write.format("parquet").save(path)
>>> len(pd.read_parquet(path))
10
Source code in rand_engine/file_handlers/writer.py
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 | |
mode(write_mode)
¶
Set the write mode of the next save or start.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
write_mode
|
str
|
|
required |
Returns:
| Type | Description |
|---|---|
FileWriter
|
This writer, for chaining. |
Examples:
>>> import os, tempfile
>>> import pandas as pd
>>> from rand_engine import DataGenerator
>>> spec = {"age": {"method": "integers", "kwargs": {"min": 18, "max": 80}}}
>>> gen = DataGenerator(spec, seed=42).size(10)
>>> path = os.path.join(tempfile.mkdtemp(), "mode.csv")
>>> gen.write.format("csv").save(path)
>>> gen.write.mode("append").format("csv").save(path)
>>> len(pd.read_csv(path))
20
Source code in rand_engine/file_handlers/writer.py
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 | |
option(key, value)
¶
Set one write option of the next save or start.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Option name: |
required |
value
|
Any
|
Option value. |
required |
Returns:
| Type | Description |
|---|---|
FileWriter
|
This writer, for chaining. |
Examples:
>>> import os, tempfile
>>> from rand_engine import DataGenerator
>>> spec = {"age": {"method": "integers", "kwargs": {"min": 18, "max": 80}}}
>>> path = os.path.join(tempfile.mkdtemp(), "parts")
>>> DataGenerator(spec, seed=42).size(10).write.option("numFiles", 2).format("csv").save(path)
>>> len(os.listdir(path))
2
Source code in rand_engine/file_handlers/writer.py
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 | |
options(**kwargs)
¶
Set several write options of the next save or start at once.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Option names and values, such as |
{}
|
Returns:
| Type | Description |
|---|---|
FileWriter
|
This writer, for chaining. |
Examples:
>>> import os, tempfile
>>> import pandas as pd
>>> from rand_engine import DataGenerator
>>> spec = {"age": {"method": "integers", "kwargs": {"min": 18, "max": 80}}}
>>> path = os.path.join(tempfile.mkdtemp(), "options")
>>> gen = DataGenerator(spec, seed=42).size(10).write.options(numFiles=2, maxRowsPerBatch=3)
>>> gen.format("csv").save(path)
>>> sum(len(pd.read_csv(os.path.join(path, name))) for name in os.listdir(path))
10
Source code in rand_engine/file_handlers/writer.py
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 | |
start(path)
¶
Write one microbatch file per pass under path until the timeout option elapses.
Each pass writes size rows to path/part-<uuid>.<ext> and sleeps trigger
seconds. The timeout option (default 20 seconds) ends the loop after the pass that
crosses it. Blocks for that duration; in overwrite mode it clears path first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Directory to write the part files into, created if missing. |
required |
Raises:
| Type | Description |
|---|---|
RandEngineError
|
If |
Examples:
>>> import os, tempfile
>>> import pandas as pd
>>> from rand_engine import DataGenerator
>>> spec = {"age": {"method": "integers", "kwargs": {"min": 18, "max": 80}}}
>>> target = os.path.join(tempfile.mkdtemp(), "ages")
>>> stream = DataGenerator(spec, seed=42).size(10).writeStream
>>> stream.format("csv").trigger(frequency=0.1).option("timeout", 0.3).start(target)
>>> files = os.listdir(target)
>>> bool(files) and all(len(pd.read_csv(os.path.join(target, name))) == 10 for name in files)
True
Source code in rand_engine/file_handlers/_writer_stream.py
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 | |
trigger(frequency)
¶
Set the pause between two microbatch writes of the next start.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frequency
|
float
|
Seconds to sleep after each write, fractions allowed; default 1. |
required |
Returns:
| Type | Description |
|---|---|
FileStreamWriter
|
This writer, for chaining. |
Examples:
>>> from rand_engine import DataGenerator
>>> spec = {"age": {"method": "integers", "kwargs": {"min": 18, "max": 80}}}
>>> stream = DataGenerator(spec, seed=42).size(10).writeStream
>>> stream.trigger(frequency=0.5) is stream
True
Source code in rand_engine/file_handlers/_writer_stream.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | |