Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate consistent person data

Tags:

python

I want to use Faker to generate some data for tests.

But I have trouble generating consitent data for a single user:

>>> from faker import Factory

>>> fake = Factory.create()

>>> fake.name()
>>> u'Tayshaun Corkery'

>>> fake.email()
>>> u'[email protected]'

As you see the email doesn't reflect the previously generated name. The docs say:

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).

Is there a way to generate consistent person data without writing much of additional code?

like image 724
warvariuc Avatar asked Jan 28 '15 07:01

warvariuc


2 Answers

You can use Factory_Boy for it. It comes with built-in Faker and allows you to create, from an already generated attribute (a name, a surname, for example) a "lazy" attribute (an email for example) using the previous data.

like image 50
cjadeveloper Avatar answered Nov 20 '22 17:11

cjadeveloper


I have made a script that will use the first name and last name from faker and add a random number between 1 and 1000, put them together to make a fake generated email with a number to make it look more legit (Example [email protected]), here is the code

from faker import Faker # Import Faker And Random For The Random Number #
from random import *
fake = Faker('en_GB') # Sets Faker's Location To Great Britain#
fn = ((fake.first_name())) # Makes The First Name Into A Veriable #
ln = ((fake.last_name())) # Makes The Last Name Into A Veriable #
rn = str (randint(1,1000)) # Makes A Random Number From 1 To 1000 And It Used To Add A Random Number To The End Of The Email To Make It Look Legit #
email = (fn + ln + rn + "@gmail.co.uk") # Adds The First Name, Last Name And The Random Number And The Extension '@gmail.co.uk' #
print("First Name: " + fn + "\n" + "last Name: " + ln + "\n" + "Email: " + email) # Prints The Whole Output#

I Hope This Helps, If You Need Any More Help Message Me On Twitter @R00T_H4X0R

like image 1
R00T_H4X0R Avatar answered Nov 20 '22 18:11

R00T_H4X0R