Faker is an open source project that solves the problem of simulating (random) data in Python. I do not know that you have not used fake data in the work, especially the front and back end development personnel, should often use, front-end personnel page display, effect display. Back-end staff database data simulation. The project introduced to you today is a perfect solution to this solution, which can simulate data in multiple languages, such as Chinese, English, Japanese, Korean and so on. Interested partners can download to try.

_ _ - | | _ | _ | _ | _ | _ | _ _ - | | | - _ | _ _ - _ - | | | _ | _ _ - _ - | | | _ | _ _ - | | _ | _ _ - _ - | | | _ | _ _ - _ - | | | _ | _ _ - _ - | | | _ | _ _ - | | _ | _ _ - _ - | | | _ | _ _ - | | _ | _ _ - | |Copy the code

The installation

pip install Faker
Copy the code

Basic usage

Initialization with faker.faker () generates the data you want by accessing properties named for the data type you want.

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.'
Copy the code

Each call to the method fake.name() yields a different (random) result. This is because the counterfeiter forwards the faker.generator.method_name () call to faker.generator.format (method_name).

from faker import Faker
fake = Faker("zh-CN")

fake.name()
# 'Zhang Liu'

fake.address()
Room 461467, Block C, Jiangbei Li Road, Liaoyang County, Heilongjiang Province

fake.text()
# 'Continue browsing the music industry domestically. Anything at the same time professional but people and can't. Think everyone is updated.Data equipment Japan more. Life is generally published to the maximum. State of the game to get liked today. Professional can't work so culturally so feel educated. Full attention to the project. Now add women some Japanese feel however. Sensory cause total sensory representation. Quality so management but therefore. The pictures are all a registered representation of the major car effects. Quality like wants more about it. Already that more decided to run. The series name completely studies women without business.'
Copy the code

You can customize different languages, also can output a variety of languages, here we show Chinese and English, interested can go to check the official documents.

Generating an IP Address

from faker import Faker
from faker.providers import internet

fake = Faker()
fake.add_provider(internet)

print(fake.ipv4_private())
# '192.168.91.234'
Copy the code

Generate IP addresses randomly, producing different (random) results each time.

Command line usage

faker [-h] [--version] [-o output]
      [-l{bg_BG,cs_CZ,... ,zh_CN,zh_TW}] [-r REPEAT] [-sSEP] [-i {package.containing.custom_provider otherpkg.containing.custom_provider}] [fake] [fake argument [fake argument ]]...Copy the code

Where:

  • faker: is the script when installed in your environment, in development you could use python -m faker instead
  • -h, --help: Displays the help document
  • --version: Displays the version information
  • -o FILENAME: redirects output to the specified file name.
  • -l {bg_BG,cs_CZ,... ,zh_CN,zh_TW}: Allows custom languages, one or more.
  • -r REPEAT: Specifies the number of outputs
  • -s SEP: Generates the specified characters each time output is generated
  • -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: a pseudo-name used to generate output, such as name, address, or text
  • [fake argument ...]: Optional argument passed to fake
$ 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-10 85'.'birthdate': '2008-03-29'}

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

Custom data

If you don’t want to use the default data, you can provide your own word set.

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.'
Copy the code

Common examples

from faker import Faker
from faker.providers import internet

fake = Faker("zh-CN")

# Generate a random phone number
print(fake.phone_number())
# generate random names
print(fake.name())
# generate random address
print(fake.address())
# Randomly generate country names
print(fake.country())
# Randomly generate country codes
print(fake.country_code())
# Randomly generate the city name
print(fake.city_name())
# Randomly generate cities
print(fake.city())
# Randomly generate provinces
print(fake.province())
# Generate random email
print(fake.email())
Generate a random IPV4 address
print(fake.ipv4())
Generate a random string between the maximum and minimum length
print(fake.pystr(min_chars=0, max_chars=8))

Generate license plate number randomly
print(fake.license_plate())

# Generate color randomly
print(fake.rgb_color())  # rgb
print(fake.safe_hex_color())  # hexadecimal
print(fake.color_name())  # color name
print(fake.hex_color()) # hexadecimal

# Randomly generate the company name
print(fake.company())

# Randomly generate jobs
print(fake.job())
Generate passwords randomly
print(fake.password(length=10, special_chars=True, digits=True, upper_case=True, lower_case=True))
# generate uUID randomly
print(fake.uuid4())
# Generate sha1 randomly
print(fake.sha1(raw_output=False))
# generate MD5 randomly
print(fake.md5(raw_output=False))

# Randomly generate female names
print(fake.name_female())
# Male name
print(fake.name_male())
# Generate names randomly
print(fake.name())

# Generate basic information
print(fake.profile(fields=None, sex=None))
print(fake.simple_profile(sex=None))

Generate the browser header user_agent randomly
print(fake.user_agent())

# Random generation time month
print(fake.month_name())
# 'May'
print(fake.date_time_this_century(before_now=True, after_now=False, tzinfo=None))
# 2005-10-08 08:32:02
print(fake.time_object(end_datetime=None))
# 06:45:11
print(fake.date_time_between(start_date="-10y", end_date="now", tzinfo=None))
# 2012-08-25 03:07:38
print(fake.future_date(end_date="+30d", tzinfo=None))
# 2020-04-25
print(fake.date_time(tzinfo=None, end_datetime=None))
# 2002-09-01 18:27:45
print(fake.date(pattern="%Y-%m-%d", end_datetime=None))
# '1998-08-02'
print(fake.date_time_this_month(before_now=True, after_now=False, tzinfo=None))
# 2020-04-03 16:03:21
print(fake.timezone())
# 'Africa/Addis_Ababa'
print(fake.date_time_this_decade(before_now=True, after_now=False, tzinfo=None))
# 2020-01-09 01:15:08
print(fake.month())
# '04'
print(fake.day_of_week())
# 'Sunday'
print(fake.iso8601(tzinfo=None, end_datetime=None))
# '1988-02-28T09:22:29'
print(fake.date_object(end_datetime=None))
# 2017-06-26
print(fake.date_this_decade(before_today=True, after_today=False))
# 2020-03-30
fake.date_this_century(before_today=True, after_today=False)
# datetime.date(2000, 6, 1)
fake.date_this_month(before_today=True, after_today=False)
# datetime.date(2018, 6, 13)
fake.am_pm()
# 'AM'
fake.past_datetime(start_date="-30d", tzinfo=None)
# datetime.datetime(2018, 6, 25, 7, 41, 34)
fake.date_this_year(before_today=True, after_today=False)
# datetime.date(2018, 2, 24)
fake.date_time_between_dates(datetime_start=None, datetime_end=None, tzinfo=None)
# datetime.datetime(2018, 6, 26, 14, 40, 5)
fake.date_time_ad(tzinfo=None, end_datetime=None)
# datetime.datetime(673, 1, 28, 18, 17, 55)
fake.date_between_dates(date_start=None, date_end=None)
# datetime.date(2018, 6, 26)
fake.future_datetime(end_date="+30d", tzinfo=None)
# datetime.datetime(2018, 7, 4, 10, 53, 6)
fake.century()
# 'IX'
fake.past_date(start_date="-30d", tzinfo=None)
# datetime.date(2018, 5, 30)
fake.time(pattern="%H:%M:%S", end_datetime=None)
# '01:32:14'
fake.day_of_month()
19 '#'
fake.unix_time(end_datetime=None, start_datetime=None)

fake.date_time_this_year(before_now=True, after_now=False, tzinfo=None)
# datetime.datetime(2018, 5, 24, 11, 25, 25)
fake.date_between(start_date="-30y", end_date="today")
# datetime.date(2003, 1, 11)
fake.year()
# '1993'
fake.time_series(start_date="-30d", end_date="now", precision=None, distrib=None, tzinfo=None)
# <generator object time_series at 0x7f44e702a620>

Generate files randomly
fake.file_extension(category=None)
# 'xls'
print(fake.file_name(category=None, extension=None))
# so. Mov
fake.file_path(depth=1, category=None, extension=None)
# '/ education/client.js'
fake.unix_device(prefix=None)
# '/dev/sdf'
print(fake.unix_partition(prefix=None))
# '/dev/vdv4'
print(fake.mime_type(category=None))
# 'message/imdn+xml'

Copy the code

Open source: github.com/joke2k/fake… Document of the project: faker. Readthedocs. IO/en/master /

Do you like today’s recommendation? If you like the words, please leave a message at the bottom of the article or like, to express my support, your message, like, forward attention is I continue to update the power oh!

Concern public number reply: “1024”, free to receive a large wave of learning resources, first come, first served oh!