Python2.7: <= Faker 3.0.1
Faker 是一个为你生成假数据的Python包。 无论您是需要引导数据库、创建美观的 XML 文档、填写持久性以对其进行压力测试,还是对从生产服务获取的数据进行匿名化,Faker 都适合您。
Faker is heavily inspired by PHP Faker, Perl Faker, and by Ruby Faker.
_|_|_|_| _|
_| _|_|_| _| _| _|_| _| _|_|
_|_|_| _| _| _|_| _|_|_|_| _|_|
_| _| _| _| _| _| _|
_| _|_|_| _| _| _|_|_| _|
For more details, see the extended docs.
Install with pip:
pip install Faker
注意:这个包以前被称为 fake-factory.
使用 faker.Faker() 创建并初始化 faker 生成器,它可以通过访问以所需数据类型命名的属性来生成数据。
from faker import Faker
fake = Faker()
fake.name()
# 'Lucy Cechtelar'
fake.address()
# "426 Jordy Lodge
# Cartwrightshire, SC 88120-6700"
fake.text()
# Sint velit eveniet. Rerum atque repellat voluptatem quia rerum. Numquam excepturi
# beatae sint laudantium consequatur. Magni occaecati itaque sint et sit tempore. Nesciunt
# amet quidem. Iusto deleniti cum autem ad quia aperiam.
# A consectetur quos aliquam. In iste aliquid et aut similique suscipit. Consequatur qui
# quaerat iste minus hic expedita. Consequuntur error magni et laboriosam. Aut aspernatur
# voluptatem sit aliquam. Dolores voluptatum est.
# Aut molestias et maxime. Fugit autem facilis quos vero. Eius quibusdam possimus est.
# Ea quaerat et quisquam. Deleniti sunt quam. Adipisci consequatur id in occaecati.
# Et sint et. Ut ducimus quod nemo ab voluptatum.
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).
for _ in range(10):
print(fake.name())
# Adaline Reichel
# Dr. Santa Prosacco DVM
# Noemy Vandervort V
# Lexi O'Conner
# Gracie Weber
# Roscoe Johns
# Emmett Lebsack
# Keegan Thiel
# Wellington Koelpin II
# Ms. Karley Kiehn V
每个生成器属性 (like name, address, and lorem) 都称为 "fake". A faker generator has many of them, packaged in "providers".
Check the extended docs for a list of bundled providers and a list of community providers.
faker.Factory 可以将语言环境作为参数,以返回本地化数据。 如果未找到本地化提供程序,工厂将回退到默认的 en_US 区域设置。
from faker import Faker
fake = Faker('it_IT') # fake = Faker(locale='zh_CN')
for _ in range(10):
print(fake.name())
> Elda Palumbo
> Pacifico Giordano
> Sig. Avide Guerra
> Yago Amato
> Eustachio Messina
> Dott. Violante Lombardo
> Sig. Alighieri Monti
> Costanzo Costa
> Nazzareno Barbieri
> Max Coppola
您可以在源代码中的providers包下检查可用的Faker语言环境。 Faker 的本地化是一个持续的过程,为此我们需要您的帮助。 请立即为您自己的语言环境创建本地化提供程序并提交拉取请求 (PR)。
Included localized providers:
安装后,您可以从命令行调用 faker:
faker [-h] [--version] [-o output]
[-l {bg_BG,cs_CZ,...,zh_CN,zh_TW}]
[-r REPEAT] [-s SEP]
[-i {package.containing.custom_provider otherpkg.containing.custom_provider}]
[fake] [fake argument [fake argument ...]]
Where:
faker: is the script when installed in your environment, in development you could use python -m faker instead-h, --help: shows a help message--version: shows the program's version number-o FILENAME: redirects the output to the specified filename-l {bg_BG,cs_CZ,...,zh_CN,zh_TW}: allows use of a localized provider-r REPEAT: will generate a specified number of outputs-s SEP: will generate the specified separator after each generated output-i {my.custom_provider other.custom_provider} list of additional custom providers to use. Note that is the import path of the package containing your Provider class, not the custom Provider class itself.fake: is the name of the fake to generate an output for, such as name, address, or text[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)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;
from faker import Faker
fake = Faker()
# first, import a similar Provider or use the default one
from faker.providers import BaseProvider
# create new provider class
class MyProvider(BaseProvider):
def foo(self):
return 'bar'
# then add new provider to faker instance
fake.add_provider(MyProvider)
# now you can use:
fake.foo()
> 'bar'
如果您不想使用默认的 lorem ipsum,您可以提供自己的单词集。 以下示例展示了如何使用从 cakeipsum 中选取的单词列表来执行此操作:
from faker import Faker
fake = Faker()
my_word_list = [
'danish','cheesecake','sugar',
'Lollipop','wafer','Gummies',
'sesame','Jelly','beans',
'pie','bar','Ice','oat' ]
fake.sentence()
#'Expedita at beatae voluptatibus nulla omnis.'
fake.sentence(ext_word_list=my_word_list)
# 'Oat beans oat Lollipop bar cheesecake.'
Factory Boy 已经集成了Faker。 只需使用factory_boy的factory.Faker方法即可:
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 属性返回用于生成值的 random.Random 实例:
from faker import Faker
fake = Faker()
fake.random
fake.random.getstate()
By default all generators share the same instance of random.Random, which can be accessed with from faker.generator import random. Using this may be useful for plugins that want to affect all faker instances.
默认情况下,所有生成器共享同一个 random.Random 实例,可以通过 from faker.generator import random 访问该实例。 对于想要影响所有 faker 实例的插件来说,使用此功能可能很有用。
当使用 Faker 进行单元测试时,您通常会希望生成相同的数据集。 为了方便起见,生成器还提供了 seed() 方法,该方法为共享随机数生成器提供种子。 使用相同版本的 faker 和种子调用相同的方法会产生相同的结果。
from faker import Faker
fake = Faker()
fake.seed(4321)
print(fake.name())
> Margaret Boehm
每个生成器还可以通过使用 seed_instance() 方法切换到自己的 random.Random 实例,与共享实例分开,其作用方式相同。 例如:
from faker import Faker
fake = Faker()
fake.seed_instance(4321)
print(fake.name())
> Margaret Boehm
请注意,由于我们不断更新数据集,因此无法保证补丁版本之间的结果保持一致。 如果您在测试中对结果进行硬编码,请确保将 Faker 的版本固定到补丁号。
Installing dependencies:
$ pip install -r tests/requirements.txt
Run tests:
$ python setup.py test
or
$ python -m unittest -v tests
Write documentation for providers:
$ python -m faker > docs.txt