forked from cclauss/Ten-lines-or-less
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake_format.py
48 lines (34 loc) · 1.21 KB
/
fake_format.py
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
#!/usr/bin/env python
# coding: utf-8
from faker import Faker
def fake_fmt(fmt="{first_name}'s favorite number: {random_int}", fake=None):
fake = fake or Faker()
fields = [fld.split("}")[0].split(":")[0] for fld in fmt.split("{")[1:]]
return fmt.format(**{field: getattr(fake, field)() for field in fields})
# with no parameters
print(fake_fmt())
# without a fmt but no fake parameter
print(fake_fmt("Hello {name}, How is the weather in {city}, {state_abbr}?"))
print(fake_fmt("My username is {user_name} but my email address is {email}."))
# with both fmt and fake parameters
fake = Faker()
print(fake_fmt("{name} considers a {catch_phrase} in {country}", fake))
# ===== more examples
# a list of ten fake people
fmt = "\t{first_name:10} {last_name:10} {random_int:>7}"
print("\n".join(fake_fmt(fmt) for _ in range(10)))
# a fake form letter
print(
fake_fmt(
"""
\nDear valued {credit_card_provider} customer,
On {credit_card_expire} your credit card {credit_card_number} will expire.
Three weeks before that date, we will send a replacement card to your address:
{street_address}
{city}, {state_abbr} {postalcode_plus4}
Yours truly,
{name}
{credit_card_provider}
"""
)
)