-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake-data.py
More file actions
59 lines (43 loc) · 1.57 KB
/
Copy pathmake-data.py
File metadata and controls
59 lines (43 loc) · 1.57 KB
1
2
3
4
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
#!/usr/bin/env python3
"""Create some data."""
from pathlib import Path
import random
import shutil
import string
NUM_FILES = 6000
TOTAL_MB = 600
TOTAL_BYTES = TOTAL_MB * 1024 * 1024 # 600 MiB
OUTPUT_DIR = Path("results")
RANDOM_SEED_OFFSET = 3
def random_block(rng: random.Random, size: int) -> bytes:
# Keep output as readable text while varying content between files
alphabet = string.ascii_letters + string.digits + " \n"
return "".join(rng.choices(alphabet, k=size)).encode("utf-8")
def main() -> None:
if OUTPUT_DIR.exists():
shutil.rmtree(OUTPUT_DIR)
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
base_size = TOTAL_BYTES // NUM_FILES
remainder = TOTAL_BYTES % NUM_FILES
total_written = 0
for i in range(NUM_FILES):
target_size = base_size + (1 if i < remainder else 0)
file_path = OUTPUT_DIR / f"file_{i + 1:04d}.txt"
# Seed per file index for deterministic but distinct content
rng = random.Random(i + 1 + RANDOM_SEED_OFFSET)
# Write in chunks to avoid building large strings in memory
chunk_size = 8192
remaining = target_size
with file_path.open("wb") as f:
while remaining > 0:
n = min(chunk_size, remaining)
f.write(random_block(rng, n))
remaining -= n
total_written += target_size
print(f"Created {NUM_FILES} files in '{OUTPUT_DIR}'.")
print(
f"Total bytes written: {total_written} "
f"({total_written / (1024 * 1024):.2f} MiB)"
)
if __name__ == "__main__":
main()