79 lines
3.5 KiB
Python
Executable File
79 lines
3.5 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# fakeuser - generate a fake user profile (name, email, address) for testing
|
|
# Usage: fakeuser [--count N] [--seed N] [--format text|json]
|
|
import argparse, json, random, sys
|
|
|
|
FIRST = ["Ada", "Alan", "Grace", "Linus", "Margaret", "Dennis", "Barbara", "Ken",
|
|
"Edsger", "Dorothy", "John", "Mary", "James", "Patricia", "Robert", "Linda",
|
|
"Michael", "Elizabeth", "David", "Susan", "Richard", "Jessica", "Joseph", "Sarah"]
|
|
LAST = ["Lovelace", "Turing", "Hopper", "Torvalds", "Hamilton", "Ritchie", "Liskov",
|
|
"Thompson", "Vaughan", "Backus", "Smith", "Johnson", "Williams", "Brown",
|
|
"Jones", "Garcia", "Miller", "Davis", "Rodriguez", "Martinez", "Hernandez"]
|
|
STREETS = ["Oak St", "Maple Ave", "Cedar Ln", "Elm St", "Pine Rd", "Birch Way",
|
|
"Willow Dr", "Chestnut St", "Walnut Ave", "Spruce Ct"]
|
|
CITIES = [("Springfield", "IL", "62701"), ("Rivertown", "OH", "43001"),
|
|
("Lakeside", "MN", "55001"), ("Fairview", "TX", "75001"),
|
|
("Brookfield", "WI", "53005"), ("Hillcrest", "CA", "90210"),
|
|
("Meadowbrook", "PA", "19001"), ("Sunnyvale", "CA", "94086")]
|
|
DOMAINS = ["example.com", "example.org", "testmail.dev", "mailinator.test"]
|
|
JOBS = ["software engineer", "teacher", "nurse", "designer", "accountant",
|
|
"chef", "electrician", "writer", "analyst", "mechanic"]
|
|
|
|
def profile(rng):
|
|
first = rng.choice(FIRST)
|
|
last = rng.choice(LAST)
|
|
city, state, zipp = rng.choice(CITIES)
|
|
user = f"{first.lower()}.{last.lower()}{rng.randint(1, 99)}"
|
|
return {
|
|
"name": f"{first} {last}",
|
|
"username": user,
|
|
"email": f"{user}@{rng.choice(DOMAINS)}",
|
|
"phone": f"+1-{rng.randint(200, 989)}-{rng.randint(200, 989)}-{rng.randint(1000, 9999)}",
|
|
"address": f"{rng.randint(1, 9999)} {rng.choice(STREETS)}, {city}, {state} {zipp}",
|
|
"city": city, "state": state, "zip": zipp,
|
|
"job": rng.choice(JOBS),
|
|
"age": rng.randint(18, 85),
|
|
}
|
|
|
|
def fmt_text(p):
|
|
return "\n".join([
|
|
f"Name: {p['name']}",
|
|
f"Username: {p['username']}",
|
|
f"Email: {p['email']}",
|
|
f"Phone: {p['phone']}",
|
|
f"Address: {p['address']}",
|
|
f"Job: {p['job']}",
|
|
f"Age: {p['age']}",
|
|
])
|
|
|
|
def selftest():
|
|
rng = random.Random(42)
|
|
p = profile(rng)
|
|
assert "@" in p["email"] and p["name"] and p["age"] >= 18
|
|
assert profile(random.Random(42)) == p
|
|
assert json.loads(json.dumps(p))["email"] == p["email"]
|
|
print(fmt_text(p))
|
|
print("fakeuser selftest OK")
|
|
|
|
def main():
|
|
p = argparse.ArgumentParser(description="Generate a fake user profile (name, email, address) for testing.")
|
|
p.add_argument("--count", type=int, default=1, help="number of profiles (default 1)")
|
|
p.add_argument("--seed", type=int, default=None, help="seed for deterministic output")
|
|
p.add_argument("--format", choices=["text", "json"], default="text", help="output format")
|
|
p.add_argument("--selftest", action="store_true", help="run a built-in self-test and exit")
|
|
a = p.parse_args()
|
|
if a.selftest:
|
|
selftest(); return 0
|
|
rng = random.Random(a.seed)
|
|
profiles = [profile(rng) for _ in range(max(1, a.count))]
|
|
if a.format == "json":
|
|
print(json.dumps(profiles if len(profiles) > 1 else profiles[0], indent=2))
|
|
else:
|
|
print(("\n\n" if len(profiles) > 1 else "").join(fmt_text(x) for x in profiles) if len(profiles) > 1 else fmt_text(profiles[0]))
|
|
if len(profiles) > 1:
|
|
pass
|
|
return 0
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|