To answer the question:
now how do I make sure the generated id is unique in the dataset?
You have to use: unique.random_int
So, your code will be like this as you see below:
from faker import Faker
import random
import pandas as pd
Faker.seed(0)
random.seed(0)
fake = Faker("en_US")
fixed_digits = 6
concatid = 'ID'
idcode,name, city, country, job, age = [[] for k in range(0,6)]
for row in range(0,100):
idcode.append(concatid + str(fake.unique.random_int(min=111111, max=999999)))
name.append(fake.name())
city.append(fake.city())
country.append(fake.country())
job.append(fake.job())
age.append(random.randint(20,100))
d = {"ID Code":idcode, "Name":name, "Age":age, "City":city, "Country":country, "Job":job}
df = pd.DataFrame(d)
df.head()
Answer from Geek Logbook on Stack OverflowVideos
You can use python providers as well.
class MyModel(factory.django.DjangoModelFactory)
number_field = factory.Faker('pyint', min_value=0, max_value=1000)
class Meta:
model = SomeModel
Documentation
I was able to figure out this problem by using a lazy attribute (factory.LazyAttribute). From the docs:
Most factory attributes can be added using static values that are evaluated when the factory is defined, but some attributes (such as fields whose value is computed from other elements) will need values assigned each time an instance is generated.
class FabricFactory(DjangoModelFactory):
class Meta:
model = Fabric
title = factory.Faker('name')
description = factory.Faker('catch_phrase')
price = factory.LazyAttribute(random.randrange(MIN_PRICE, MAX_PRICE + 1))
» pip install Faker
I know this is an old question but for anyone who might come across this, here's another approach that you can use.
>>> from factory.faker import faker
>>> FAKE = faker.Faker()
>>> FAKE.name()
'Scott Rodriguez'
>>> FAKE.address()
'PSC 5061, Box 1673\nAPO AP 53007'
>>>
You can use faker with factory_boy like this:
class RandomUserFactory(factory.Factory):
class Meta:
model = models.User
first_name = factory.Faker('first_name')
user = RandomUserFactory()
print user.first_name
# 'Emily'
So you need to instantiate a user with factory_boy and it will call Faker for you.
I don't know if you are trying to use this with Django or not, but if you want the factory to save the created user to the database, then you need to extend factory.django.DjangoModelFactory instead of factory.Factory.