Introduction

Recently, I had a requirement to fill the database with some data. Filling the tables manually proved to be very tedious enough. So, I was looking for some software that could do the heavy lifting for me and also that could be used with Python natively. Voila!! After a bit of search came across this awesome software.

FAKER

Faker can be used to generate fake data which resembles the real world values rather than some gibberish values.

Using FAKER With Python

The following code has been tested using Python 3.5 on a Windows 10 machine.

Type the below command in the Command Prompt to install Faker module

Installing Faker

Let’s start with a very simple example.

Import the faker module and make use of the Factory Class. Use create() method to create an object called fake. Say, if you want to print a name then use the object fake along with name() method as shown below.

from faker import Factory
fake = Factory.create()
print(fake.name())

Output
Mrs. Kenley Farrell

You can even localize the names according to your preferences. For e.g, if you want to display name in Hindi (India), then pass the string argument “hi_IN” to create() method.

from faker import Factory
fake = Factory.create("hi_IN")
print(fake.name())

Ouput

रचना बाबू

If you want to display 10 random currencies

from faker import Factory
fake = Factory.create()
for i in range(10):
    print(fake.currency_code())

Output
MWK MRO KES TOP IQD MYR PGK MDL NIO MNT

You can randomly generate addresses using address() method. Below code is localized for “en_US”.

from faker import Factory
fake = Factory.create("en_US")
for i in range(10):
    print(fake.address())

Output
PSC 7452, Box 9044 APO AA 06906-4518 65264 Vince Points Suite 897 North Pearlene, PA 66031 63876 Daijah Pass New Rickiland, NM 82820 59116 Mueller Port South Rosanne, IN 07663 486 Jerde Forest New Keyshawnfort, GA 59913-5859 360 Orie Forge Lake Izayah, NY 83459-8476 1840 O’Connell Field Apt. 566 Port Zakaryton, HI 66433-8319 PSC 2512, Box 6095 APO AP 37389 89957 Williamson Burg Apt. 255 Shelliview, WY 06711-9241 78580 Vanessa Square Apt. 095 West Sherie, CT 67665

You can generate various information about a person using different methods as shown below.

from faker import Factory
fake = Factory.create("en_US")

print(fake.last_name_male())

print(fake.name_female())

print(fake.prefix_male())

print(fake.prefix())

print(fake.name())

print(fake.suffix_female())

print(fake.name_male())

print(fake.first_name())

print(fake.suffix_male())

print(fake.suffix())

print(fake.first_name_male())

print(fake.first_name_female())

print(fake.last_name_female())

print(fake.last_name())

print(fake.prefix_female())

Output
Morar Dr. Mckinley Halvorson Dr. Mrs. Kadeem Parker MD Mr. Elliott Padberg Elvira III IV Arley Mariam Turner Huel Ms.

You can read various methods available in Faker Module in detail @

https://faker.readthedocs.io/en/master/#

You can find the above code @

https://github.com/gowrishankarnath/FakerPythonExamples


Comments

comments powered by Disqus