Generación de datos con Faker#

  • Ultima modificación: Mayo 14, 2022

Uso básico#

[1]:
from faker import Faker

fake = Faker()
[2]:
fake.address()
[2]:
'71380 Lamb Ridges Suite 608\nRileyview, UT 16034'
[3]:
fake.text()
[3]:
'Always room short together trip. Lot well explain top per chance.\nLife red accept. Serious then professor hit memory sport. Understand special mission.'
[4]:
[fake.name() for _ in range(10)]
[4]:
['Thomas Walker',
 'Shane Barrett MD',
 'Anthony Dawson',
 'Brooke Beasley MD',
 'Christina Daniel',
 'Taylor Avila',
 'Rachel Ramos',
 'Michelle Romero',
 'Melissa Tate',
 'Jamie Castillo']

Providers#

[5]:
from faker.providers import internet

fake = Faker()
fake.add_provider(internet)

fake.ipv4_private()
[5]:
'192.168.207.70'

Localization#

[6]:
fake = Faker('it_IT')
for _ in range(10):
    print(fake.name())
Tonino Maffei
Ida Scarlatti
Tonia Rubbia
Eugenia Cendron-Petrucelli
Emma Taliani-Luzi
Annalisa Cardano
Sig. Giacomo Iannucci
Vittoria Curci-Emo
Luchino Leopardi
Dolores Odescalchi
[7]:
fake = Faker(['it_IT', 'en_US', 'ja_JP'])
for _ in range(10):
    print(fake.name())
後藤 涼平
森 直樹
Cecilia Dandolo
Chelsea Mcgee
Sig.ra Stella Trapani
Mr. Michael Moreno DVM
Carmen Lawrence
橋本 拓真
Dott. Sandro Nosiglia
Richard Scott

Línea de comandos#

[8]:
!faker --help
usage: faker [-h] [--version] [-v] [-o output] [-l LOCALE] [-r REPEAT]
             [-s SEP] [--seed SEED] [-i [INCLUDE [INCLUDE ...]]]
             [fake] [fake argument [fake argument ...]]

faker version 13.4.0

positional arguments:
  fake                  name of the fake to generate output for (e.g. profile)
  fake argument         optional arguments to pass to the fake (e.g. the
                        profile fake takes an optional list of comma separated
                        field names as the first argument)

optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  -v, --verbose         show INFO logging events instead of CRITICAL, which is
                        the default. These logging events provide insight into
                        localization of specific providers.
  -o output             redirect output to a file
  -l LOCALE, --lang LOCALE
                        specify the language for a localized provider (e.g.
                        de_DE)
  -r REPEAT, --repeat REPEAT
                        generate the specified number of outputs
  -s SEP, --sep SEP     use the specified separator after each output
  --seed SEED           specify a seed for the random generator so that
                        results are repeatable. Also compatible with 'repeat'
                        option
  -i [INCLUDE [INCLUDE ...]], --include [INCLUDE [INCLUDE ...]]
                        list of additional custom providers to user, given as
                        the import path of the module containing your Provider
                        class (not the provider class itself)

supported locales:

  ar_AA, ar_AE, ar_EG, ar_JO, ar_PS, ar_SA, az_AZ, bg_BG, bn_BD, bs_BA, cs_CZ, da_DK, de, de_AT, de_CH, de_DE, dk_DK, el_CY, el_GR, en, en_AU, en_CA, en_GB, en_IE, en_IN, en_NZ, en_PH, en_TH, en_US, es, es_CA, es_CO, es_ES, es_MX, et_EE, fa_IR, fi_FI, fil_PH, fr_CA, fr_CH, fr_FR, fr_QC, ga_IE, he_IL, hi_IN, hr_HR, hu_HU, hy_AM, id_ID, it_CH, it_IT, ja_JP, ka_GE, ko_KR, la, lb_LU, lt_LT, lv_LV, mt_MT, ne_NP, nl_BE, nl_NL, no_NO, or_IN, pl_PL, pt_BR, pt_PT, ro_RO, ru_RU, sk_SK, sl_SI, sv_SE, ta_IN, th, th_TH, tl_PH, tr_TR, tw_GH, uk_UA, zh_CN, zh_TW

  Faker can take a locale as an optional argument, to return localized data. If
  no locale argument is specified, the factory falls back to the user's OS
  locale as long as it is supported by at least one of the providers.
     - for this user, the default locale is en_US.

  If the optional argument locale and/or user's default locale is not available
  for the specified provider, the factory falls back to faker's default locale,
  which is en_US.

examples:

  $ faker address
  968 Bahringer Garden Apt. 722
  Kristinaland, NJ 09890

  $ faker -l de_DE address
  Samira-Niemeier-Allee 56
  94812 Biedenkopf

  $ faker profile ssn,birthdate
  {'ssn': u'628-10-1085', 'birthdate': '2008-03-29'}

  $ faker -r=3 -s=";" name
  Willam Kertzmann;
  Josiah Maggio;
  Gayla Schmitt;

Creación de un provider#

[9]:
#
# Crea un objeto
#
fake = Faker()

#
# Se importa un provider existente o se usa el por defecto
#
from faker.providers import BaseProvider

#
# Creación de una nueva clase
#
class MyProvider(BaseProvider):
    def foo(self) -> str:
        return 'bar'

#
# Se adiciona el nuevo provider
#
fake.add_provider(MyProvider)

#
# Uso
#
fake.foo()
[9]:
'bar'

Uso de un provider dinámico#

[10]:
from faker.providers import DynamicProvider

medical_professions_provider = DynamicProvider(
    provider_name="medical_profession",
    elements=["dr.", "doctor", "nurse", "surgeon", "clerk"],
)

fake = Faker()

#
# Adición del nuevo provider a la instancia de Faker
#
fake.add_provider(medical_professions_provider)

#
# Uso
#
fake.medical_profession()
[10]:
'surgeon'

Proveedor de texto#

[11]:
from faker import Faker

fake = Faker()

my_word_list = [
    "danish",
    "cheesecake",
    "sugar",
    "Lollipop",
    "wafer",
    "Gummies",
    "sesame",
    "Jelly",
    "beans",
    "pie",
    "bar",
    "Ice",
    "oat",
]

fake.sentence()
[11]:
'Moment environmental gun.'
[12]:
fake.sentence(ext_word_list=my_word_list)
[12]:
'Cheesecake Lollipop Lollipop Ice danish Jelly Lollipop.'

Factory boy#

import factory
from myapp.models import Book

class BookFactory(factory.Factory):
    class Meta:
        model = Book

    title = factory.Faker('sentence', nb_words=4)
    author_name = factory.Faker('name')

Random instance#

[14]:
fake = Faker()
fake.random
a, b, c = fake.random.getstate()
a
[14]:
3
[15]:
b[:3]
[15]:
(3488433372, 3008820493, 3616184552)
[16]:
c

Valores únicos#

[17]:
fake = Faker()
names = [fake.unique.first_name() for i in range(500)]
assert len(set(names)) == len(names)

Semilla del generador#

[19]:
fake = Faker()
Faker.seed(4321)

print(fake.name())
Jason Brown
[20]:
fake = Faker()
fake.seed_instance(4321)

print(fake.name())
Jason Brown