How to create fake data through Python ?

 Faker the perfect python package to generate fake data

Install and usage

Install with pip:

pip install Faker

generating realistic and diverse data for testing and prototyping purposes can be a time-consuming and tedious task. This is where the Faker Python library comes to the rescue. Faker is a powerful open-source library that provides developers with a simple and efficient way to generate fake data in a wide range of formats

what is faker ?

Faker is a Python library developed by François Best that generates large volumes of high-quality fake data. It is designed to produce realistic data in various formats, including names, addresses, phone numbers, dates, and more. With Faker, developers can easily populate databases and create realistic test environments without the risk of using sensitive or real data.

Use faker.Faker() to create and initialize a faker generator, which can generate data by accessing properties named after the type of data you want.


faker import Faker
fake = Faker()

fake.name()
# 'Lucy Cechtelar'

fake.address()
# '426 Jordy Lodge
# Cartwrightshire, SC 88120-6700'

fake.text()
# 'Sint velit eveniet. Rerum atque repellat voluptatem quia rerum. Numquam excepturi

Each call to method fake.name() yields a different (random) result. This is because faker forwards faker.Generator.method_name() calls to faker.Generator.format(method_name).

for i in range(10):
print(fake.name())

# 'Adaline Reichel'
# 'Dr. Santa Prosacco DVM'
# 'Noemy Vandervort V'
# 'Lexi O'Conner'
# 'Gracie Weber'
# 'Roscoe Johns'
# 'Emmett Lebsack'
# 'Keegan Thiel'
# 'Wellington Koelpin II'
# 'Ms. Karley Kiehn V'

Localization

faker.Faker can take a locale as an argument, to return localized data. If no localized provider is found, the factory falls back to the default LCID string for US english, ie: en_US.

from faker import Faker
fake = Faker(['it_IT', 'en_US', 'ja_JP']) #it --> italy ja --> japan
for _ in range(10):
print(fake.name())

# 鈴木 陽一
# Leslie Moreno
# Emma Williams
# 渡辺 裕美子
# Marcantonio Galuppi
# Martha Davis
# Kristen Turner
# 中津川 春香
# Ashley Castillo
# 山田 桃子

Unique values Through use of the .unique property on the generator, you can guarantee that any generated values are unique for this specific instance.

from faker import Faker
fake = Faker()
names = [fake.unique.first_name() for i in range(500)]
assert len(set(names)) == len(names)


Seeding the Generator

When using Faker for unit testing, you will often want to generate the same dataset. For convenience, the generator also provide a seed() method, which seeds theshared random number generator. Seed produces the same result when the same methodswith the same version of faker are called.

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

print(fake.name())
# 'Margaret Boehm'

Conclusion Faker is a Python package that generates fake data for you. Whether you need tobootstrap your database, create good-looking XML documents, fill-in yourpersistence to stress test it, or anonymize data taken from a production service,Faker is for you.

in this article I have introduce a brief introduction to faker with a discussfor some of it’s features to know every thing about faker you can check theofficial documentation from here .

Post a Comment

0 Comments