Rahul Banerjee

#Day25 - A cheatsheet to get Fake/Mock Data in Python using Faker

#Day25 - A cheatsheet to get Fake/Mock Data in Python using Faker

You can use Python to come up with your company's name and slogan

Subscribe to my newsletter and never miss my upcoming articles

Listen to this article

We will be using the library Faker

Install the library

pip install faker

Now let's import the library and create an instance of "Faker"

from faker import Faker
fake = Faker()

Seeding

Seeding ensures that the same set of random data is returned during every run. This is very useful when you want to compare functions or models in the same environment without changes in the dataset.

from faker import Faker
Faker.seed(999)
fake = Faker()

Names

print(fake.name())
'''
Wesley Turner
'''

print(fake.first_name())
'''
Alexandra
'''

print(fake.last_name())
'''
Aguilar
'''

print(fake.name_male())
'''
Charles Hanna
'''

print(fake.name_female())
'''
Shannon Walker

'''
print(fake.name_nonbinary())
'''
Christopher Brennan
'''

Address and Phone Numbers

from faker import Faker
Faker.seed(999)
fake = Faker()

print(fake.address())
'''
987 Anderson Union
Dawnville, ID 20051
'''

print(fake.phone_number())
'''
904-636-1403x832
'''

But these all seem like American Names, Addresses, and Phone numbers. Let's get data for some other region.

Locale Data based on Region

We can set the region or localize the data by passing a string during instantiation. Not all locales support all the functions in Faker. You can find all the locales here

from faker import Faker
Faker.seed(999)
## Seting locale to India and language to hindi
fake = Faker('hi_IN')

print(fake.name())
'''
गावित, हुसैन
'''

print(fake.address())
'''
13/24 ललित कृष्णा
मोतिहारी-140383
'''

print(fake.phone_number())
'''
+91 521 8975277
'''

We set the locale to India and the language to Hindi. Let's try setting the locale to France and language to french.

from faker import Faker
Faker.seed(999)
fake = Faker('fr_FR')

print(fake.name())
'''
Roland Baudry
'''

print(fake.address())
'''
25, rue de Gerard
90463 Sanchez-la-Forêt
'''

print(fake.phone_number())
'''
+33 (0)1 38 32 52 18
'''

You can also pass in multiple locales as a list.

from faker import Faker
Faker.seed(999)
fake = Faker(['hi_IN','en_US','fr_FR'])

for _ in range(5):
  print(fake.phone_number())
  print("-----------------")

'''
+33 (0)5 99 87 72 51
-----------------
+1-324-904-6361x4038
-----------------
+91 252 1897527
-----------------
00031364850
-----------------
001-271-698-2331x03084
-----------------
'''

Fake Company Names and Catch Phrase

from faker import Faker
Faker.seed(999)
fake = Faker()

for _ in range(5):
  print(f'{fake.company()} : {fake.catch_phrase()}')
  print("-x-x-")
'''
Miller, Brennan and Berry : Pre-emptive mission-critical capacity
-x-x-
Flores, Norton and Bruce : Cross-platform secondary core
-x-x-
Baker PLC : Business-focused tangible time-frame
-x-x-
Sanders-Bowers : Focused responsive initiative
-x-x-
Nelson and Sons : Stand-alone motivating database
-x-x-
'''

I believe all this actually fetched from some database, so this isn't like GPT-3 which actually generates a name.

Faking Profiles

from faker import Faker
Faker.seed(999)
fake = Faker(['en_IN'])


print(f'{fake.profile()}')


'''
{'job': 'Cartographer', 'company': 'Mangat, Loke and Korpal', 
'ssn': '251-32-4904', 
'residence': '61/403, Chawla Circle, Bathinda-189752', 
'current_location': (Decimal('87.4766035'), 
Decimal('170.848372')), 'blood_group': 'O-', 
'website': ['http://www.bhalla.com/', 'https://gokhale.com/'], 
'username': 'vrajagopal', 'name': 'Ivan Das', 'sex': 'F', 
'address': 'H.No. 681, Mander Marg, Howrah-684709', 
'mail': 'dbajaj@yahoo.com', 'birthdate': datetime.date(2019, 10, 8)}
'''

Faking Emails

from faker import Faker
Faker.seed(999)
fake = Faker(['en_IN'])


print(fake.email())
'''
ikhurana@dutt.com
'''

print(fake.safe_email() )
'''
jayantdeo@example.net
'''

print(fake.free_email() )
'''
sarnazoya@yahoo.com
'''

print(fake.company_email() )
'''
ywable@thaman.biz
'''

Generating Fake Python Data Types

from faker import Faker
Faker.seed(999)
fake = Faker(['en_IN'])

print(fake.pytuple())
'''('https://vyas.com/posts/list/main/author.htm',
 'nDKnTZlGdbkYAjJiUCWy', 2125, 'vamble@gmail.com', 
'ZLIvUSsViQwFPkfdaGhy', 7650, 'ZbBODSZctqOcGVcxOWIn')
'''

print(fake.pylist())
'''
['DqwQiBApBNXchzoOOsDB', 'uxrFsPxsaLVPzMouLrct', -3001403.6558895,
 'vohrazaina@rama.biz', -783180386.336077, 
'ODrlFCjaPMlWoEGrIvcY', 'badalwali@yahoo.com']'''

print(fake.pydict())
'''
{'hic': 'kSKSsguiUhAHdBwXsvPU', 'non': 'NnApyVhwOfvBBidAOfCa',
 'sit': 'gatik94@yahoo.com', 'cumque': 86753382.7739197, 
'aut': 93.434062857687, 'voluptatem': 321.537771434202, 
'dignissimos': 'xememaWRlowIFAtOAEMo', 'sint': 5095, 
'vero': 1680, 'molestiae': Decimal('-934.685870829972'), 
'reiciendis': 9133, 'quo': 'tRBgKmfGLRRdMFpEyPAq',
 'fuga': 'shayakdugar@gmail.com'}
'''

print(fake.pyiterable())
'''
[datetime.datetime(1990, 2, 3, 3, 11, 17), 1446, 
611903747.767127, 'http://www.sami.net/',
 'iJIxPgnNWUsZztHjzhuc', 'JZufaRqcvQJujWAgbosd', 
Decimal('74468.5598767266'), 'zachariahvanya@chahal.com']
'''

fake.pyiterable() can return any iterable in python with mock data.

#100daysofcode#python#python3#codenewbies#programming-blogs
 
Share this