Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Python faker Library



What is Python faker Library?

Python faker library is highly powerful for producing false / fake data. It allows developers to rapidly create life-like data for a whole host of purposes, just some include testing, prototyping and even populating databases with sample text.

Why is faker Library Useful?

Due to the following reasons, the faker library is useful −

  • Privacy − It generates realistic empirical datasets without real person data.
  • Efficiency − It is very efficient to generate large volume of sample data.
  • Flexibility − It is useful for customizable to produce data in certain formats, languages and specifications.

Usage of faker Library

The faker library can be used in the follow areas −

  • Data Science − For the EDA (Exploratory Data Analysis) phase later data science projects, Faker can generate sample datasets to help train and test models— even using bogus dataset in case of real datasets unavailable or restricted.
  • Software Testing − The faker is used to smoke test and populate databases with fake data so that different scenarios can be tested within the application. This is very useful for really pushing software to its limits and verifying how it handles with lots of data.
  • Web Development − While developing web applications, developers can use the faker library to generate their database with user profiles, comments and posts etc. which in turn helps them visualize how their application behaves under real-world scenarios.

Advantages of Using faker Library

Following are the advantages of using the faker library −

  • Scalability − If you need to create big data sets for testing purposes that may be difficult or impossible with good quality sources.
  • Customizable − It is used to create richer data generation, tweak locales names or little addition to fields output format
  • Consistency − It provides the same fake data in different test environments.

Importing faker Library

To use the faker library in your Python project, you need to import it. You can import the faker library by using the following statement −

from faker import Faker

Creating Instance of faker Library

To create an instance of the faker library, use the following statement −

fake = Faker()

This will produce a Faker instance that you can use to provide fake data.

Examples of faker Library

Practice the following examples to understand the use of faker library −

Example 1: Generate Fake Identities

In this example, we are generating fake identities i.e., fake user data such as name, email addresses, etc.

# Importing faker library
from faker import Faker
# Creating its instance
fake = Faker()
# Generating data 
print("Name:", fake.name()) 
print("Address:", fake.address()) 
print("Phone Number:", fake.phone_number()) 
print("Email:", fake.email()) 
print("Job Title:", fake.job()) 
print("Company:", fake.company())

Output

Name: Emily Wilson 
Address: 7425 Oak Street 
Apt. 692 
Springfield, IL 62794 
Phone Number: 217-555-0147 
Email: [email protected] 
Job Title: Senior Software Engineer 
Company: Smith & Sons Inc. 

Example 2: Generate Fake Financial Information

In this example, we are generating fake financial information for the users −

# Importing faker library
from faker import Faker
# Creating its instance
fake = Faker()
# Generating data 
print("Credit Card Number:", fake.credit_card_number(card_type=None)) 
print("IBAN:", fake.iban()) 
print("SWIFT/BIC:", fake.swift()) 
print("Currency Code:", fake.currency_code()) 
print("Cryptocurrency Code:", fake.cryptocurrency_code())

Output

Credit Card Number: 4532015112830368 
IBAN: GB82WEST12345698765432 
SWIFT/BIC: RZBAATWWXXX 
Currency Code: USD 
Cryptocurrency Code: BTC 
Generate Fake Internet-related Data 
print("IPv4 Address:", fake.ipv4()) 
print("IPv6 Address:", fake.ipv6()) 
print("MAC Address:", fake.mac_address()) 
print("URL:", fake.url()) 
print("Domain Name:", fake.domain_name())

Output

IPv4 Address: 192.168.42.118 
IPv6 Address: 2001:db8:a0b:12f0::1 
MAC Address: 00:11:22:33:44:55 
URL: https://www.example.com 
Domain Name: example.net  

Example 3: Generate Fake Text and Paragraphs

In this example, we are generating fake text and paragraphs −

# Importing faker library
from faker import Faker
# Creating its instance
fake = Faker()
# Generating data 
print("Random Word:", fake.word()) 
print("Sentence:", fake.sentence(nb_words=15)) 
print("Paragraph:", fake.paragraph(nb_sentences=3))

Output

Random Word: elephant 
Sentence: The quick brown fox jumps over the lazy dog repeatedly outside. 
Paragraph: The sun was shining brightly in the clear sky. Birds were singing their morning songs from the trees. A gentle breeze rustled through the leaves, creating a soothing melody.

Example 4: Generate Fake Dates and Times

In this example, we are generating fake dates and times −

# Importing faker library
from faker import Faker
# Creating its instance
fake = Faker()
# Generating data 
print("Date:", fake.date()) 
print("Time:", fake.time()) 
print("Past Date:", fake.past_date(start_date="-30d")) 
print("Future Date:", fake.future_date(end_date="+30d"))

Output

Date: 2023-07-25 
Time: 14:30:45 
Past Date: 2023-07-25 
Future Date: 2023-08-23 

Commonly Used Methods of faker Library

The following are some of the commonly used methods of the faker library that you can use to generate fake data for different use −

Method Description
name() Generates a full name
address() Produces a complete address
email() Creates a fake email address
job() Generates job titles
company() Creates company names
phone_number() Generates phone numbers
text() Produces random text
sentence() Creates a single sentence
paragraph() Generates a paragraph of text

Advanced Usage of fake Library

1. Generating Profiles

The faker library can be used to create comprehensive profiles with various personal details.

Example

# Importing faker library
from faker import Faker
# Creating its instance
fake = Faker()
# Generating data
profile = fake.profile() 
print("Name:", profile['name']) 
print("Address:", profile['residence']) 
print("Job:", profile['job']) 
print("Company:", profile['company']) 
print("SSN:", profile['ssn']) 
print("Birthday:", profile['birthdate'])

Output

Name: Olivia Martin 
Address: 123 Main St\nAnytown, CA 12345 
Job: Software Engineer 
Company: Tech Corp 
SSN: XXX-XX-6789 
Birthday: 1995-03-12

2. Seeding for Reproducible Results

The faker library allows you to seed the generator for reproducible results.

Example

# Importing faker library
from faker import Faker
# Creating its instance
fake = Faker()
# Generating data
fake.seed(42) 
print(fake.name()) 
print(fake.address()) 
print(fake.email())

Output

Whenever these command are run with same seed value always they will have the output. 

3. Generating Data in Various Dialects

The faker library can also be used to generate fake data in different languages.

Example

In this example, we are creating fake data in French language.

# Importing faker library
from faker import Faker
# Creating its instance
fake = Faker('fr_FR')  # French 
# Generating data
print(fake.name()) 
print(fake.address())

Output

Anas Dupont 
12 rue de la Paix 
75002 Paris 
France 
Best Practices
python_reference.htm
Advertisements