Ebooks

Python Faker tutorial

Python Faker tutorial shows how to generate fake data in Python with Faker package. We use the joke2k/faker library.

Faker

Faker is a Python library that generates fake data. Faka data is often used for testing or filling databases with some dummy data. Faker is heavily inspired by PHP's Faker, Perl's Data::Faker, and by Ruby's Faker.

Setting up Faker

The package is installed with composer.

$ pip install Faker 

We install the Faker module.

$ pip install Dumper

In addition, we install the Dumper, which provides nicer console output when dumping variables.

Faker generator

The faker.Faker() creates and initializes a faker generator, which can generate data by accessing properties named after the type of data.

Faker delegates the data generation to providers. The default provider uses the English locale. Faker supports other locales; they differ in level of completion.

Simple Faker

The following example is a simple demonstration of Faker.

simple.py
#!/usr/bin/env python

from faker import Faker

faker = Faker()

print(f'name: {faker.name()}')
print(f'address: {faker.address()}')

print(f'text: {faker.text()}')

The example outputs a fake name, address, and text.

$ ./simple.py
name: Arthur Patton
address: 0638 Larsen Way
Tylermouth, CA 48344
text: Save general start bar. Become class both bank Mrs so myself.
Each difficult performance even. Total anyone develop her raise research both.
Laugh sport necessary tree. As during day up fall.

This is a sample output.

Faking names

In the second example, we fake data related to user names.

names.py
#!/usr/bin/env python

from faker import Faker

faker = Faker()

print(f'Name: {faker.name()}')
print(f'First name: {faker.first_name()}')
print(f'Last name: {faker.last_name()}')

print('--------------------------')

print(f'Male name: {faker.name_male()}')
print(f'Female name: {faker.name_female()}')

The example creates fake full names, first names, last names of males and females.

$ ./names.py
Name: Tara Brown
First name: Stephanie
Last name: Martin
--------------------------
Male name: John Banks
Female name: Lacey Roberts

This is a sample output.

Faking jobs

Jobs are genereated with job().

jobs.py
#!/usr/bin/env python

from faker import Faker

faker = Faker()

for _ in range(6):
    print(faker.job())

The example creates six fake jobs.

$ ./jobs.py
Town planner
Paediatric nurse
Geographical information systems officer
Water quality scientist
Engineer, maintenance
Designer, ceramics/pottery

This is a sample output.

Faking locale data

The Faker supports localized data to some extent. The locale is passed to the constructor method. Note that the locales are finished to various levels.

localized.py
#!/usr/bin/env python

from faker import Faker

faker = Faker('cz_CZ')

for i in range(3):

    name = faker.name()
    address = faker.address()
    phone = faker.phone_number()

    print(f'{name}, {address}, {phone}')

The example generates fake data in Czech language.

$ ./localized.py
Irena Novotná, Nad Šancemi 725
055 80 Bojkovice, 606 136 053
Stanislav Svoboda, Březanova 225
621 17 Bystřice pod Hostýnem, 727 057 251
Klára Moravcová, Neužilova 6/3
134 97 Česká Kamenice, 606 374 469

This is a sample output. Notice that Czech language has accents.

Faking currencies

The following example creates fake data for currencies.

currencies.py
#!/usr/bin/env python

from faker import Faker

faker = Faker()

print(f'currency: {faker.currency()}')
print(f'currency name: {faker.currency_name()}')

print(f'currency code: {faker.currency_code()}')

The program generates fake currencies.

$ ./currencies.py
currency: ('ISK', 'Icelandic króna')
currency name: Israeli new shekel
currency code: DJF

This is a sample output.

Faking words

Faker allows to create dummy words.

words.py
#!/usr/bin/env python

from faker import Faker

faker = Faker()

print(f'a word: {faker.word()}')
print(f'six words: {faker.words(6)}')

words = ['forest', 'blue', 'cloud', 'sky', 'wood', 'falcon']

print(f'customized unique words: {faker.words(3, words, True)}')

The example creates dummy words.

print(f'a word: {faker.word()}')

This line generates a fake word.

print(f'six words: {faker.words(6)}')

Here we generate six dummy words.

words = ['forest', 'blue', 'cloud', 'sky', 'wood', 'falcon']

print(f'customized unique words: {faker.words(3, words, True)}')

We can also create fake words from a predefined list of words.

$ words.py
a word: show
six words: ['value', 'its', 'those', 'wish', 'enter', 'hold']
customized unique words: ['forest', 'blue', 'sky']

This is a sample output.

Faking profiles

Faker can create simple dummy profiles with simple_profile() and extended profiles with profile().

profiles.py
#!/usr/bin/env python

from faker import Faker
import dumper

faker = Faker()

profile1 = faker.simple_profile()
dumper.dump(profile1)

print('--------------------------')

profile2 = faker.simple_profile('M')
dumper.dump(profile2)

print('--------------------------')

profile3 = faker.profile(sex='F')
dumper.dump(profile3)

The example creates dummy profiles for both males and females.

$ ./profiles.py
<dict at 0x20d590a7678>:
  username: 'gmorgan'
  name: 'Jessica Clark'
  sex: 'F'
  address: '694 Joseph Valleys\nJohnfort, ME 81780'
  mail: 'bettybuckley@yahoo.com'
  birthdate: <str at 0x20d5bcbd7b0>: 'datetime.date(1938, 9, 18)'
--------------------------
<dict at 0x20d5b0065e8>:
  username: 'mrios'
  name: 'Harold Wagner'
  sex: 'M'
  address: '26439 Robinson Radial\nWest Meghanmouth, PA 85463'
  mail: 'josechoi@gmail.com'
  birthdate: <str at 0x20d5bcbd7b0>: 'datetime.date(1986, 8, 18)'
--------------------------
<dict at 0x20d5bd24990>:
  job: 'Engineer, communications'
  company: 'Jackson-Willis'
  ssn: '430-41-6118'
  residence: 'USNS Odom\nFPO AP 47095'
  current_location: <tuple at 0x20d5bca9a88>
    0: <str at 0x20d5bd248a0>: "Decimal('74.1885785')"
    1: <str at 0x20d5bd248a0>: "Decimal('119.951995')"
  blood_group: 'O-'
  website: ['http://roberson.com/']
  username: 'ygutierrez'
  name: 'Lindsay Walker'
  sex: 'F'
  address: '22968 Beverly Road Suite 918\nTimothyborough, SD 10494'
  mail: 'aliciamccall@yahoo.com'
  birthdate: <str at 0x20d5bcbd7b0>: 'datetime.date(1979, 1, 4)'

This is a sample output.

Faking numbers

The Faker allows to generate random digits and integers.

fake_numbers.py
#!/usr/bin/env python

from faker import Faker

faker = Faker()

print(f'Random int: {faker.random_int()}')
print(f'Random int: {faker.random_int(0, 100)}')
print(f'Random digit: {faker.random_digit()}')

The example generates random digits and integers.

print(f'Random int: {faker.random_int(0, 100)}')

We can specify the bounds in the random_int() method.

$ ./fake_numbers.py
Random int: 5181
Random int: 91
Random digit: 9

This is a sample output.

Faking hashes and uuids

Faker support for dummy hashes and uuids.

hash_uuid.py
#!/usr/bin/env python

from faker import Faker

faker = Faker()

print(f'md5: {faker.md5()}')
print(f'sha1: {faker.sha1()}')
print(f'sha256: {faker.sha256()}')
print(f'uuid4: {faker.uuid4()}')

The example generates three fake hash and one uuid values.

$ ./hash_uuid.py
md5: aace57d56794686acec9eb190d401d46
sha1: 9f8f6af3089e7b5ed571591701afcfd9c2bb7a0e
sha256: 8b117b58599809f50735c701f598312b0f64203a8ffacde09af23db69cfd62d5
uuid4: 38092dcd-0e49-4ac0-b39b-7edf6db62290

This is a sample output.

Faking internet related data

Faker has several accessors for faking internet related data.

internet.py
#!/usr/bin/env python

from faker import Faker

faker = Faker()

print(f'Email: {faker.email()}')
print(f'Safe email: {faker.safe_email()}')
print(f'Free email: {faker.free_email()}')
print(f'Company email: {faker.company_email()}')

print('------------------------------------')

print(f'Host name: {faker.hostname()}')
print(f'Domain name: {faker.domain_name()}')
print(f'Domain word: {faker.domain_word()}')
print(f'TLD: {faker.tld()}')

print('------------------------------------')

print(f'IPv4: {faker.ipv4()}')
print(f'IPv6: {faker.ipv6()}')
print(f'MAC address: {faker.mac_address()}')

print('------------------------------------')

print(f'Slug: {faker.slug()}')
print(f'Image URL: {faker.image_url()}')

The example shows various internet related data, including emails, domain names, slugs, IP addresses and URLs.

$ ./internet.py
Email: hescobar@acevedo.info
Safe email: jonesgregory@example.net
Free email: zchambers@yahoo.com
Company email: paulbailey@gordon-woods.com
------------------------------------
Host name: desktop-12.rodriguez-underwood.com
Domain name: henry.com
Domain word: davis
TLD: com
------------------------------------
IPv4: 192.31.48.26
IPv6: 75cd:2c43:37f5:774c:dd:5a2f:ae5d:bfc9
MAC address: 3d:b1:39:ec:c6:53
------------------------------------
Slug: of-street-fight
Image URL: https://placeimg.com/311/871/any

This is a sample output.

Faking date and time

Faker has plenty of methods for faking date and time values.

date_time.py
#!/usr/bin/env python

from faker import Faker

faker = Faker()

print(f'Date of birth: {faker.date_of_birth()}')
print(f'Century: {faker.century()}')
print(f'Year: {faker.year()}')
print(f'Month: {faker.month()}')
print(f'Month name: {faker.month_name()}')
print(f'Day of week: {faker.day_of_week()}')
print(f'Day of month: {faker.day_of_month()}')
print(f'Time zone: {faker.timezone()}')
print(f'AM/PM: {faker.am_pm()}')

The first example shows fake methods for date of birth, datetime parts, timezone, and AM/PM.

$ date_time.py
Date of birth: 2008-08-07
Century: IV
Year: 1981
Month: 05
Month name: January
Day of week: Saturday
Day of month: 26
Time zone: Asia/Oral
AM/PM: AM

This is a sample output.

datetime2.py
#!/usr/bin/env python

from faker import Faker

faker = Faker()

print(f'Datetime this century: {faker.date_time_this_century()}')
print(f'Datetime this decade: {faker.date_time_this_decade()}')
print(f'Datetime this year: {faker.date_time_this_year()}')
print(f'Datetime this month: {faker.date_time_this_month()}')

print('-------------------------')

print(f'Date this century: {faker.date_this_century()}')
print(f'Date this decade: {faker.date_this_decade()}')
print(f'Date this year: {faker.date_this_year()}')
print(f'Date this month: {faker.date_this_month()}')

print('-------------------------')

TOTAL_SECONDS = 60*60*24*2 # two days

series = faker.time_series(start_date='-12d', end_date='now', precision=TOTAL_SECONDS)

for val in series:
    print(val[0])

The second example shows methods for generating datetime values in the current century, decade, year, or month. It also includes the generation of time series values.

$ date_time2.py
Datetime this century: 2013-08-06 23:42:47
Datetime this decade: 2010-02-15 01:08:34
Datetime this year: 2019-03-10 05:32:42
Datetime this month: 2019-04-06 10:13:53
-------------------------
Date this century: 2014-11-04
Date this decade: 2018-03-07
Date this year: 2019-02-07
Date this month: 2019-04-04
-------------------------
2019-03-26 16:16:49
2019-03-28 16:16:49
2019-03-30 16:16:49
2019-04-01 16:16:49
2019-04-03 16:16:49
2019-04-05 16:16:49

This is a sample output.

datetime3.py
#!/usr/bin/env python

from faker import Faker

faker = Faker()

print(f'Unix time: {faker.unix_time()}')
print(f'Datetime: {faker.date_time()}')
print(f'iso8601: {faker.iso8601()}')
print(f'Date: {faker.date()}')
print(f'Time: {faker.time()}')

print('-------------------------')

print(f"Datetime between: {faker.date_time_between(start_date='-15y', end_date='now')}")
print(f"Date between: {faker.date_between()}")

print('-------------------------')

print(f"Future datetime: {faker.future_datetime()}")
print(f"Future date: {faker.future_date()}")
print(f"Past datetime: {faker.past_datetime()}")
print(f"Past date: {faker.past_date()}")

The third example shows methods for various datetime formats, for getting datetime values for a chosen range, and for generating future or past values.

$ date_time3.py
Unix time: 1389138339
Datetime: 1988-01-24 09:16:09
iso8601: 2014-04-22T04:19:18
Date: 2009-05-01
Time: 12:07:51
-------------------------
Datetime between: 2011-03-18 21:00:23
Date between: 1992-07-09
-------------------------
Future datetime: 2019-04-11 13:36:02
Future date: 2019-04-14
Past datetime: 2019-03-21 12:27:20
Past date: 2019-03-22

This is a sample output.

Generating XML data with Faker

In the following example, we generate XML data with Faker and Jinja2 template. The XML file will contain users.

$ pip install jinja2

We install the Jinja2 template engine.

fake_xml.py
#!/usr/bin/env python3

from jinja2 import Environment, FileSystemLoader
from faker import Faker

class User:

    def __init__(self, first_name, last_name, occupation):

        self.first_name = first_name
        self.last_name = last_name
        self.occupation = occupation

faker = Faker()
users = []

for _ in range(10):

    first_name = faker.first_name()
    last_name = faker.last_name()
    occupation = faker.job()

    user = User(first_name, last_name, occupation)

    users.append(user)

file_loader = FileSystemLoader('templates')
env = Environment(loader=file_loader)

template = env.get_template('users.xml.j2')
output = template.render(users=users)

print(output, file=open('users.xml', 'w'))

The program generates a list of ten users. The list is passed to the Jinja2 template to be processed. The template is located in the templates directory. The generated content is written to the users.xml file.

templates/users.xml.j2
<?xml version="1.0" encoding="UTF-8"?>
<users>
    {% for user in users %}
    <user id="{{ loop.index }}">
        <firstname>{{ user.first_name }}</firstname>
        <lastname>{{ user.last_name }}</lastname>
        <occupation>{{ user.occupation }}</occupation>
    </user>
    {% endfor %}
</users>

In the template, we use the for directive to process the list of users.

In this tutorial, we have used Python Faker to generate fake data in Python.

You might also be interested in the following related tutorials: Python Jinja tutorial and Python tutorial, or list all Python tutorials.