dataforger 0.1.1__tar.gz
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-0.1.1/LICENSE +21 -0
- dataforger-0.1.1/PKG-INFO +106 -0
- dataforger-0.1.1/README.md +69 -0
- dataforger-0.1.1/pyproject.toml +28 -0
- dataforger-0.1.1/setup.cfg +4 -0
- dataforger-0.1.1/src/dataforger/__init__.py +3 -0
- dataforger-0.1.1/src/dataforger/cli.py +98 -0
- dataforger-0.1.1/src/dataforger/core.py +250 -0
- dataforger-0.1.1/src/dataforger/countries.json +84146 -0
- dataforger-0.1.1/src/dataforger/data.json +78 -0
- dataforger-0.1.1/src/dataforger.egg-info/PKG-INFO +106 -0
- dataforger-0.1.1/src/dataforger.egg-info/SOURCES.txt +15 -0
- dataforger-0.1.1/src/dataforger.egg-info/dependency_links.txt +1 -0
- dataforger-0.1.1/src/dataforger.egg-info/entry_points.txt +2 -0
- dataforger-0.1.1/src/dataforger.egg-info/top_level.txt +1 -0
- dataforger-0.1.1/tests/test_cli.py +47 -0
- dataforger-0.1.1/tests/test_core.py +115 -0
dataforger-0.1.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Karmanshi Shrivastava
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: dataforger
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Generate fake data from Python or the command line.
|
|
5
|
+
License: MIT License
|
|
6
|
+
|
|
7
|
+
Copyright (c) 2026 Karmanshi Shrivastava
|
|
8
|
+
|
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
in the Software without restriction, including without limitation the rights
|
|
12
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
furnished to do so, subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be included in all
|
|
17
|
+
copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
SOFTWARE.
|
|
26
|
+
|
|
27
|
+
Classifier: Development Status :: 3 - Alpha
|
|
28
|
+
Classifier: Environment :: Console
|
|
29
|
+
Classifier: Intended Audience :: Developers
|
|
30
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
31
|
+
Classifier: Programming Language :: Python :: 3
|
|
32
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
33
|
+
Requires-Python: >=3.10
|
|
34
|
+
Description-Content-Type: text/markdown
|
|
35
|
+
License-File: LICENSE
|
|
36
|
+
Dynamic: license-file
|
|
37
|
+
|
|
38
|
+
# dataforger
|
|
39
|
+
|
|
40
|
+
Generate realistic fake data from Python or the command line, and save it as
|
|
41
|
+
CSV or JSON. Dataforger uses only the Python standard library and has no
|
|
42
|
+
third-party runtime dependencies.
|
|
43
|
+
|
|
44
|
+
Built-in names, streets, companies, and email domains are stored in
|
|
45
|
+
`src/dataforger/data.json`. Country names and their associated cities are
|
|
46
|
+
stored in `src/dataforger/countries.json`; when generating a record with both
|
|
47
|
+
country and city fields, the city is selected from that country's list.
|
|
48
|
+
|
|
49
|
+
## Install
|
|
50
|
+
|
|
51
|
+
Requires Python 3.10 or later.
|
|
52
|
+
|
|
53
|
+
From a checkout of this repository:
|
|
54
|
+
|
|
55
|
+
```console
|
|
56
|
+
python -m pip install .
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
After publishing the package to PyPI:
|
|
60
|
+
|
|
61
|
+
```console
|
|
62
|
+
python -m pip install dataforger
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Publishing to PyPI
|
|
66
|
+
|
|
67
|
+
The GitHub Actions workflow runs the tests on Python 3.10 through 3.13 and
|
|
68
|
+
builds wheel and source distributions on pushes and pull requests. To publish,
|
|
69
|
+
configure a PyPI trusted publisher for this GitHub repository, using workflow
|
|
70
|
+
`.github/workflows/publish.yml` and the GitHub environment `pypi`. Then create
|
|
71
|
+
and publish a GitHub Release; the workflow publishes its tested distributions
|
|
72
|
+
to PyPI using trusted publishing.
|
|
73
|
+
|
|
74
|
+
## Command line
|
|
75
|
+
|
|
76
|
+
Run `dataforger` without field options for an interactive prompt. Or specify
|
|
77
|
+
all options directly:
|
|
78
|
+
|
|
79
|
+
```console
|
|
80
|
+
dataforger --rows 100 --field name:name --field email:email --field age:age --format csv --output users.csv --seed 42
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
The `--field` option can be repeated. Supported field types are `name`,
|
|
84
|
+
`first_name`, `last_name`, `email`, `phone`, `address`, `city`, `country`,
|
|
85
|
+
`company`, `uuid`, `date`, `age`, `integer`, `float`, and `boolean`. If
|
|
86
|
+
`--field` is omitted, the command prompts for the row count, format, output
|
|
87
|
+
path, and fields. When using command-line field options, the defaults are 10
|
|
88
|
+
rows, JSON format, and `data.json`.
|
|
89
|
+
|
|
90
|
+
## Python API
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
from dataforger import generate_data, write_data
|
|
94
|
+
|
|
95
|
+
records = generate_data(
|
|
96
|
+
{"name": "name", "email": "email", "age": "age"},
|
|
97
|
+
rows=100,
|
|
98
|
+
seed=42,
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
write_data(records, "users.json", "json")
|
|
102
|
+
write_data(records, "users.csv", "csv")
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
`generate_data` returns a list of dictionaries. Pass the resulting records,
|
|
106
|
+
an output path, and either `"csv"` or `"json"` to `write_data`.
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# dataforger
|
|
2
|
+
|
|
3
|
+
Generate realistic fake data from Python or the command line, and save it as
|
|
4
|
+
CSV or JSON. Dataforger uses only the Python standard library and has no
|
|
5
|
+
third-party runtime dependencies.
|
|
6
|
+
|
|
7
|
+
Built-in names, streets, companies, and email domains are stored in
|
|
8
|
+
`src/dataforger/data.json`. Country names and their associated cities are
|
|
9
|
+
stored in `src/dataforger/countries.json`; when generating a record with both
|
|
10
|
+
country and city fields, the city is selected from that country's list.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
Requires Python 3.10 or later.
|
|
15
|
+
|
|
16
|
+
From a checkout of this repository:
|
|
17
|
+
|
|
18
|
+
```console
|
|
19
|
+
python -m pip install .
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
After publishing the package to PyPI:
|
|
23
|
+
|
|
24
|
+
```console
|
|
25
|
+
python -m pip install dataforger
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Publishing to PyPI
|
|
29
|
+
|
|
30
|
+
The GitHub Actions workflow runs the tests on Python 3.10 through 3.13 and
|
|
31
|
+
builds wheel and source distributions on pushes and pull requests. To publish,
|
|
32
|
+
configure a PyPI trusted publisher for this GitHub repository, using workflow
|
|
33
|
+
`.github/workflows/publish.yml` and the GitHub environment `pypi`. Then create
|
|
34
|
+
and publish a GitHub Release; the workflow publishes its tested distributions
|
|
35
|
+
to PyPI using trusted publishing.
|
|
36
|
+
|
|
37
|
+
## Command line
|
|
38
|
+
|
|
39
|
+
Run `dataforger` without field options for an interactive prompt. Or specify
|
|
40
|
+
all options directly:
|
|
41
|
+
|
|
42
|
+
```console
|
|
43
|
+
dataforger --rows 100 --field name:name --field email:email --field age:age --format csv --output users.csv --seed 42
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
The `--field` option can be repeated. Supported field types are `name`,
|
|
47
|
+
`first_name`, `last_name`, `email`, `phone`, `address`, `city`, `country`,
|
|
48
|
+
`company`, `uuid`, `date`, `age`, `integer`, `float`, and `boolean`. If
|
|
49
|
+
`--field` is omitted, the command prompts for the row count, format, output
|
|
50
|
+
path, and fields. When using command-line field options, the defaults are 10
|
|
51
|
+
rows, JSON format, and `data.json`.
|
|
52
|
+
|
|
53
|
+
## Python API
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
from dataforger import generate_data, write_data
|
|
57
|
+
|
|
58
|
+
records = generate_data(
|
|
59
|
+
{"name": "name", "email": "email", "age": "age"},
|
|
60
|
+
rows=100,
|
|
61
|
+
seed=42,
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
write_data(records, "users.json", "json")
|
|
65
|
+
write_data(records, "users.csv", "csv")
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
`generate_data` returns a list of dictionaries. Pass the resulting records,
|
|
69
|
+
an output path, and either `"csv"` or `"json"` to `write_data`.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "dataforger"
|
|
7
|
+
version = "0.1.1"
|
|
8
|
+
description = "Generate fake data from Python or the command line."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { file = "LICENSE" }
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
classifiers = [
|
|
13
|
+
"Development Status :: 3 - Alpha",
|
|
14
|
+
"Environment :: Console",
|
|
15
|
+
"Intended Audience :: Developers",
|
|
16
|
+
"License :: OSI Approved :: MIT License",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
[project.scripts]
|
|
22
|
+
dataforger = "dataforger.cli:main"
|
|
23
|
+
|
|
24
|
+
[tool.setuptools.packages.find]
|
|
25
|
+
where = ["src"]
|
|
26
|
+
|
|
27
|
+
[tool.setuptools.package-data]
|
|
28
|
+
dataforger = ["data.json", "countries.json"]
|
|
@@ -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())
|
|
@@ -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
|