dataforger 0.1.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dataforger/__init__.py +3 -0
- dataforger/cli.py +98 -0
- dataforger/core.py +250 -0
- dataforger/countries.json +84146 -0
- dataforger/data.json +78 -0
- dataforger-0.1.1.dist-info/METADATA +106 -0
- dataforger-0.1.1.dist-info/RECORD +11 -0
- dataforger-0.1.1.dist-info/WHEEL +5 -0
- dataforger-0.1.1.dist-info/entry_points.txt +2 -0
- dataforger-0.1.1.dist-info/licenses/LICENSE +21 -0
- dataforger-0.1.1.dist-info/top_level.txt +1 -0
dataforger/__init__.py
ADDED
dataforger/cli.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import sys
|
|
3
|
+
from collections.abc import Sequence
|
|
4
|
+
|
|
5
|
+
from dataforger.core import FIELD_TYPES, Format, generate_data, write_data
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def _parse_fields(values: Sequence[str]) -> dict[str, str]:
|
|
9
|
+
fields: dict[str, str] = {}
|
|
10
|
+
for value in values:
|
|
11
|
+
name, separator, field_type = value.partition(":")
|
|
12
|
+
name = name.strip()
|
|
13
|
+
field_type = field_type.strip()
|
|
14
|
+
if not separator or not name or not field_type:
|
|
15
|
+
raise ValueError(
|
|
16
|
+
f"Invalid field {value!r}; use the format NAME:TYPE."
|
|
17
|
+
)
|
|
18
|
+
if name in fields:
|
|
19
|
+
raise ValueError(f"Field {name!r} was specified more than once.")
|
|
20
|
+
fields[name] = field_type
|
|
21
|
+
return fields
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _prompt_fields() -> dict[str, str]:
|
|
25
|
+
print(f"Available field types: {', '.join(FIELD_TYPES)}")
|
|
26
|
+
fields: dict[str, str] = {}
|
|
27
|
+
while True:
|
|
28
|
+
name = input("Field name (leave blank when finished): ").strip()
|
|
29
|
+
if not name:
|
|
30
|
+
break
|
|
31
|
+
field_type = input("Field type [name]: ").strip() or "name"
|
|
32
|
+
if name in fields:
|
|
33
|
+
raise ValueError(f"Field {name!r} was specified more than once.")
|
|
34
|
+
fields[name] = field_type
|
|
35
|
+
if not fields:
|
|
36
|
+
raise ValueError("At least one field is required.")
|
|
37
|
+
return fields
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _build_parser() -> argparse.ArgumentParser:
|
|
41
|
+
parser = argparse.ArgumentParser(
|
|
42
|
+
prog="dataforger",
|
|
43
|
+
description="Generate fake data in CSV or JSON format.",
|
|
44
|
+
)
|
|
45
|
+
parser.add_argument(
|
|
46
|
+
"--field",
|
|
47
|
+
action="append",
|
|
48
|
+
metavar="NAME:TYPE",
|
|
49
|
+
help="field to generate; repeat for additional fields (e.g. name:name)",
|
|
50
|
+
)
|
|
51
|
+
parser.add_argument("--rows", type=int, help="number of records to generate")
|
|
52
|
+
parser.add_argument(
|
|
53
|
+
"--format",
|
|
54
|
+
choices=("csv", "json"),
|
|
55
|
+
help="output format (defaults to json)",
|
|
56
|
+
)
|
|
57
|
+
parser.add_argument("--output", help="output file path")
|
|
58
|
+
parser.add_argument("--seed", type=int, help="seed for repeatable data")
|
|
59
|
+
return parser
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def main(argv: Sequence[str] | None = None) -> int:
|
|
63
|
+
parser = _build_parser()
|
|
64
|
+
args = parser.parse_args(argv)
|
|
65
|
+
|
|
66
|
+
try:
|
|
67
|
+
if args.field:
|
|
68
|
+
fields = _parse_fields(args.field)
|
|
69
|
+
rows = 10 if args.rows is None else args.rows
|
|
70
|
+
output_format: Format = args.format or "json"
|
|
71
|
+
output = args.output or f"data.{output_format}"
|
|
72
|
+
else:
|
|
73
|
+
default_rows = args.rows if args.rows is not None else 10
|
|
74
|
+
rows_input = input(f"Number of rows [{default_rows}]: ").strip()
|
|
75
|
+
rows = int(rows_input) if rows_input else default_rows
|
|
76
|
+
format_input = input(
|
|
77
|
+
f"Output format (csv/json) [{args.format or 'json'}]: "
|
|
78
|
+
).strip()
|
|
79
|
+
output_format = format_input or args.format or "json"
|
|
80
|
+
output = input(
|
|
81
|
+
f"Output file [{args.output or f'data.{output_format}'}]: "
|
|
82
|
+
).strip() or args.output or f"data.{output_format}"
|
|
83
|
+
fields = _prompt_fields()
|
|
84
|
+
|
|
85
|
+
records = generate_data(fields, rows=rows, seed=args.seed)
|
|
86
|
+
path = write_data(records, output, output_format)
|
|
87
|
+
except (OSError, ValueError) as error:
|
|
88
|
+
parser.error(str(error))
|
|
89
|
+
except KeyboardInterrupt:
|
|
90
|
+
print("\nGeneration cancelled.", file=sys.stderr)
|
|
91
|
+
return 130
|
|
92
|
+
|
|
93
|
+
print(f"Generated {len(records)} rows in {path}.")
|
|
94
|
+
return 0
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
if __name__ == "__main__":
|
|
98
|
+
raise SystemExit(main())
|
dataforger/core.py
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import csv
|
|
2
|
+
import json
|
|
3
|
+
import random
|
|
4
|
+
import uuid
|
|
5
|
+
from collections.abc import Mapping, Sequence
|
|
6
|
+
from datetime import date, timedelta
|
|
7
|
+
from importlib.resources import files
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Callable, Literal
|
|
10
|
+
|
|
11
|
+
Format = Literal["csv", "json"]
|
|
12
|
+
|
|
13
|
+
_DATA_KEYS = (
|
|
14
|
+
"first_names",
|
|
15
|
+
"last_names",
|
|
16
|
+
"streets",
|
|
17
|
+
"street_types",
|
|
18
|
+
"companies",
|
|
19
|
+
"company_suffixes",
|
|
20
|
+
"email_domains",
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _load_data() -> dict[str, tuple[str, ...]]:
|
|
25
|
+
raw_data = json.loads(
|
|
26
|
+
files("dataforger").joinpath("data.json").read_text(encoding="utf-8")
|
|
27
|
+
)
|
|
28
|
+
if not isinstance(raw_data, dict):
|
|
29
|
+
raise ValueError("Bundled data.json must contain a JSON object.")
|
|
30
|
+
|
|
31
|
+
data: dict[str, tuple[str, ...]] = {}
|
|
32
|
+
for key in _DATA_KEYS:
|
|
33
|
+
values = raw_data.get(key)
|
|
34
|
+
if (
|
|
35
|
+
not isinstance(values, list)
|
|
36
|
+
or not values
|
|
37
|
+
or any(not isinstance(value, str) or not value for value in values)
|
|
38
|
+
):
|
|
39
|
+
raise ValueError(
|
|
40
|
+
f"Bundled data.json must define {key!r} as a non-empty list "
|
|
41
|
+
"of strings."
|
|
42
|
+
)
|
|
43
|
+
data[key] = tuple(values)
|
|
44
|
+
return data
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
_DATA = _load_data()
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _load_country_cities() -> dict[str, tuple[str, ...]]:
|
|
51
|
+
raw_data = json.loads(
|
|
52
|
+
files("dataforger").joinpath("countries.json").read_text(encoding="utf-8")
|
|
53
|
+
)
|
|
54
|
+
if not isinstance(raw_data, dict) or not raw_data:
|
|
55
|
+
raise ValueError("Bundled countries.json must contain a non-empty JSON object.")
|
|
56
|
+
|
|
57
|
+
country_cities: dict[str, tuple[str, ...]] = {}
|
|
58
|
+
for country, cities in raw_data.items():
|
|
59
|
+
if (
|
|
60
|
+
not isinstance(country, str)
|
|
61
|
+
or not country
|
|
62
|
+
or not isinstance(cities, list)
|
|
63
|
+
or not cities
|
|
64
|
+
or any(not isinstance(city, str) or not city for city in cities)
|
|
65
|
+
):
|
|
66
|
+
raise ValueError(
|
|
67
|
+
"Bundled countries.json must map country names to non-empty "
|
|
68
|
+
"lists of city names."
|
|
69
|
+
)
|
|
70
|
+
country_cities[country] = tuple(cities)
|
|
71
|
+
return country_cities
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
_COUNTRY_CITIES = _load_country_cities()
|
|
75
|
+
|
|
76
|
+
FIELD_TYPES = (
|
|
77
|
+
"name",
|
|
78
|
+
"first_name",
|
|
79
|
+
"last_name",
|
|
80
|
+
"email",
|
|
81
|
+
"phone",
|
|
82
|
+
"address",
|
|
83
|
+
"city",
|
|
84
|
+
"country",
|
|
85
|
+
"company",
|
|
86
|
+
"uuid",
|
|
87
|
+
"date",
|
|
88
|
+
"age",
|
|
89
|
+
"integer",
|
|
90
|
+
"float",
|
|
91
|
+
"boolean",
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
def _first_name(rng: random.Random) -> str:
|
|
95
|
+
return rng.choice(_DATA["first_names"])
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _last_name(rng: random.Random) -> str:
|
|
99
|
+
return rng.choice(_DATA["last_names"])
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def _name(rng: random.Random) -> str:
|
|
103
|
+
return f"{_first_name(rng)} {_last_name(rng)}"
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def _email(rng: random.Random) -> str:
|
|
107
|
+
first = _first_name(rng).lower()
|
|
108
|
+
last = _last_name(rng).lower()
|
|
109
|
+
suffix = rng.randint(1, 9999)
|
|
110
|
+
return f"{first}.{last}{suffix}@{rng.choice(_DATA['email_domains'])}"
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _phone(rng: random.Random) -> str:
|
|
114
|
+
area = rng.randint(200, 999)
|
|
115
|
+
exchange = rng.randint(200, 999)
|
|
116
|
+
subscriber = rng.randint(0, 9999)
|
|
117
|
+
return f"+1-{area}-{exchange}-{subscriber:04d}"
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def _address(rng: random.Random, city: str) -> str:
|
|
121
|
+
number = rng.randint(1, 9999)
|
|
122
|
+
street = rng.choice(_DATA["streets"])
|
|
123
|
+
street_type = rng.choice(_DATA["street_types"])
|
|
124
|
+
return f"{number} {street} {street_type}, {city}"
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def _company(rng: random.Random) -> str:
|
|
128
|
+
company = rng.choice(_DATA["companies"])
|
|
129
|
+
suffix = rng.choice(_DATA["company_suffixes"])
|
|
130
|
+
return f"{company} {suffix}"
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def _date(rng: random.Random) -> str:
|
|
134
|
+
start = date(1970, 1, 1)
|
|
135
|
+
days = (date.today() - start).days
|
|
136
|
+
return (start + timedelta(days=rng.randint(0, days))).isoformat()
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def _integer(rng: random.Random) -> int:
|
|
140
|
+
return rng.randint(0, 100_000)
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def _age(rng: random.Random) -> int:
|
|
144
|
+
return rng.randint(0, 100)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def _float(rng: random.Random) -> float:
|
|
148
|
+
return round(rng.uniform(0, 100_000), 2)
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
_PROVIDERS: dict[str, Callable[[random.Random], object]] = {
|
|
152
|
+
"name": _name,
|
|
153
|
+
"first_name": _first_name,
|
|
154
|
+
"last_name": _last_name,
|
|
155
|
+
"email": _email,
|
|
156
|
+
"phone": _phone,
|
|
157
|
+
"company": _company,
|
|
158
|
+
"uuid": lambda rng: str(uuid.UUID(int=rng.getrandbits(128), version=4)),
|
|
159
|
+
"date": _date,
|
|
160
|
+
"age": _age,
|
|
161
|
+
"integer": _integer,
|
|
162
|
+
"float": _float,
|
|
163
|
+
"boolean": lambda rng: rng.choice((True, False)),
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def generate_data(
|
|
168
|
+
fields: Mapping[str, str],
|
|
169
|
+
rows: int = 10,
|
|
170
|
+
seed: int | None = None,
|
|
171
|
+
) -> list[dict[str, object]]:
|
|
172
|
+
"""Generate records using a mapping of field names to supported value types."""
|
|
173
|
+
if not fields:
|
|
174
|
+
raise ValueError("At least one field is required.")
|
|
175
|
+
if isinstance(rows, bool) or not isinstance(rows, int) or rows < 1:
|
|
176
|
+
raise ValueError("rows must be a positive integer.")
|
|
177
|
+
|
|
178
|
+
for name, field_type in fields.items():
|
|
179
|
+
if not isinstance(name, str) or not name.strip():
|
|
180
|
+
raise ValueError("Field names must be non-empty strings.")
|
|
181
|
+
if not isinstance(field_type, str) or field_type not in FIELD_TYPES:
|
|
182
|
+
raise ValueError(
|
|
183
|
+
f"Unknown field type {field_type!r}. Choose from: "
|
|
184
|
+
f"{', '.join(FIELD_TYPES)}."
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
rng = random.Random(seed)
|
|
188
|
+
|
|
189
|
+
records: list[dict[str, object]] = []
|
|
190
|
+
for _ in range(rows):
|
|
191
|
+
location_country = (
|
|
192
|
+
rng.choice(tuple(_COUNTRY_CITIES))
|
|
193
|
+
if any(
|
|
194
|
+
field_type in ("address", "city", "country")
|
|
195
|
+
for field_type in fields.values()
|
|
196
|
+
)
|
|
197
|
+
else None
|
|
198
|
+
)
|
|
199
|
+
location_city = (
|
|
200
|
+
rng.choice(_COUNTRY_CITIES[location_country])
|
|
201
|
+
if location_country is not None
|
|
202
|
+
and any(field_type in ("address", "city") for field_type in fields.values())
|
|
203
|
+
else None
|
|
204
|
+
)
|
|
205
|
+
record: dict[str, object] = {}
|
|
206
|
+
for name, field_type in fields.items():
|
|
207
|
+
if field_type == "country":
|
|
208
|
+
record[name] = location_country
|
|
209
|
+
elif field_type == "city":
|
|
210
|
+
assert location_city is not None
|
|
211
|
+
record[name] = location_city
|
|
212
|
+
elif field_type == "address":
|
|
213
|
+
assert location_city is not None
|
|
214
|
+
record[name] = _address(rng, location_city)
|
|
215
|
+
else:
|
|
216
|
+
record[name] = _PROVIDERS[field_type](rng)
|
|
217
|
+
records.append(record)
|
|
218
|
+
return records
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def write_data(
|
|
222
|
+
records: Sequence[Mapping[str, object]],
|
|
223
|
+
output: str | Path,
|
|
224
|
+
format: Format,
|
|
225
|
+
) -> Path:
|
|
226
|
+
"""Write generated records to a CSV or JSON file and return its path."""
|
|
227
|
+
if format not in ("csv", "json"):
|
|
228
|
+
raise ValueError("format must be 'csv' or 'json'.")
|
|
229
|
+
if not records:
|
|
230
|
+
raise ValueError("At least one record is required.")
|
|
231
|
+
|
|
232
|
+
path = Path(output)
|
|
233
|
+
if format == "json":
|
|
234
|
+
with path.open("w", encoding="utf-8") as file:
|
|
235
|
+
json.dump(
|
|
236
|
+
[dict(record) for record in records],
|
|
237
|
+
file,
|
|
238
|
+
indent=2,
|
|
239
|
+
ensure_ascii=False,
|
|
240
|
+
)
|
|
241
|
+
file.write("\n")
|
|
242
|
+
else:
|
|
243
|
+
fieldnames = list(records[0])
|
|
244
|
+
if not fieldnames:
|
|
245
|
+
raise ValueError("Records must contain at least one field.")
|
|
246
|
+
with path.open("w", encoding="utf-8", newline="") as file:
|
|
247
|
+
writer = csv.DictWriter(file, fieldnames=fieldnames)
|
|
248
|
+
writer.writeheader()
|
|
249
|
+
writer.writerows(records)
|
|
250
|
+
return path
|