Python's artifact library for constructing data-Faker
During development or testing, we often need to construct data for functional verification, but it takes time to manually create data and the data is not standardized enough. And Python provides a super easy to use open source library for falsifying data-Faker.
Faker is a Python third-party open source project. The built-in method can quickly create most types of data, such as name, address, phone number, ID card, time, network information, text information, etc., and combined with python syntax, it can be added to the database Construct stress test data in batches.
github address: https://github.com/joke2k/faker

Official document: https://faker.readthedocs.io/en/master/
Installation
pip install Faker
Basic usage
from faker import Faker
# Create an object, the default generated data is in English, use zh_CN designated as Chinese
fake =Faker('zh_CN')print(fake.name())#Randomly generate names
print(fake.address())#Randomly generated address
print(fake.phone_number())#Randomly generate phone numbers
print(fake.pystr())#Randomly generate string
print(fake.email())#Randomly generate email address
for i inrange(10):print(fake.name())#Randomly generate 10 names
Output:
Xu Bo
Block P, Wuzhou Road, Bishan, Yu City, Yunnan Province 52302813039830591
RPHadhNxNMISoBTbQbQn
[email protected]
Zhang Shuying
Ye Yan
Chen Lin
Wang Jun
Hu Xiurong
Ruan Shuying
Xu Juan
Huang Dongmei
Liang Lihua
Yuan Qin
Common methods
1、 Geographic Information
- city_suffix(): city, county
- country(): Country
- country_code(): country code
- district(): District
- geo_coordinate(): geographic coordinates
- latitude(): geographic coordinates (latitude)
- longitude(): geographic coordinates (longitude)
- postcode(): postcode
- province(): province
- address(): detailed address
- street_address(): street address
- street_name(): street name
- street_suffix(): street, road
2、 Basic information
- ssn(): Generate ID number
- bs(): Random company service name
- company(): Random company name (long)
- company_prefix(): Random company name (short)
- company_suffix(): The nature of the company, such as'Information Limited'
- fake.credit_card_expire(start='now', end='+10y', date_format='%m/%y'): Random credit card expiration date such as '03/30'
- credit_card_full(): Generate complete credit card information
- credit_card_number(): credit card number
- credit_card_provider(): credit card type
- credit_card_security_code(): Credit card security code
- job(): Random job
- first_name_female(): female name
- first_name_male(): Male name
- name(): Randomly generate a full name
- name_female(): Full male name
- name_male(): Female full name
- phone_number(): randomly generate mobile phone number
- phonenumber_prefix(): randomly generate mobile phone number segment, such as 139
3、 Mailbox information
- ascii_company_email(): Random ASCII company email name
- ascii_email(): Random ASCII mailbox:
- company_email():
- email():
- safe_email(): safe email
4、 Basic Network Information
- domain_name(): Generate domain name
- domain_word(): domain word (ie, does not include suffix)
- ipv4(): Random IP4 address
- ipv6(): Random IP6 address
- mac_address(): random MAC address
- tld(): URL domain name suffix (.com,.net.cn, etc., not including.)
- uri(): random URI address
- uri_extension(): URL file extension
- uri_page(): URL file (not including suffix)
- uri_path(): URL file path (not including file name)
- url(): random URL address
- user_name(): Random username
- image_url(): Random URL address
5、 Browser information
- chrome(): randomly generate Chrome browser user_agent information
- firefox(): Randomly generate Firefox browser user_agent information
- internet_explorer(): randomly generate IE browser user_agent information
- opera(): randomly generate Opera's browser user_agent information
- safari(): randomly generate Safari browser user_agent information
- linux_platform_token(): random Linux information
- user_agent(): random user_agent information
6、 Digital information
- numerify(): Three random numbers
- random_digit(): 0~9 random number
- random_digit_not_null(): random number from 1 to 9
- random_int(): random number, default 0~9999, can be set by setting min, max
- random_number(): random number, the number of digits generated by the parameter digits setting
- pyfloat():
left_digits=5 #The number of generated integers, right_digits=2 #The number of decimals generated, positive=True #Whether there are only positive numbers
- pyint(): random Int number (refer to random_int() parameter)
- pydecimal(): random Decimal number (refer to pyfloat parameter)
7、 Text encryption
- pystr(): random string
- random_element(): random letters
- random_letter(): random letter
- paragraph(): randomly generate a paragraph
- paragraphs(): Generate multiple paragraphs randomly
- sentence(): randomly generate a sentence
- sentences(): randomly generate multiple sentences, similar to paragraphs
- text(): randomly generate an article
- word(): randomly generated words
- words(): randomly generate multiple words, usage and paragraphs, sentences, similar
- binary(): Randomly generate binary codes
- boolean():True/False
- language_code(): Randomly generate two-digit language code
- locale(): Randomly generate language/international information
- md5(): Generate MD5 randomly
- null_boolean():NULL/True/False
- password(): randomly generated password, optional parameters: length: password length; special_chars: whether special characters can be used; digits: whether to include numbers; upper_case: whether to include uppercase letters; lower_case: whether to include lowercase letters
- sha1(): random SHA1
- sha256(): random SHA256
- uuid4(): Random UUID
8、 Time information
- date(): random date
- date_between(): Randomly generate dates in the specified range, parameters: start_date, end_date
- date_between_dates(): Randomly generate dates in the specified range, the usage is the same as above
- date_object(): Randomly produce a random date from 1970-1-1 to the specified date.
- date_time(): randomly generate the specified time (January 1, 1970 to present)
- date_time_ad(): Generate a random time from 1 AD to the present
- date_time_between(): usage is the same as dates
- future_date(): future date
- future_datetime(): future time
- month(): random month
- month_name(): Random month (English)
- past_date(): Randomly generate the past date
- past_datetime(): Randomly generate the past time
- time(): Random 24-hour time
- timedelta(): get the time difference randomly
- time_object(): random 24-hour time, time object
- time_series(): Random TimeSeries object
- timezone(): random time zone
- unix_time(): random Unix time
- year(): random year
Custom provider
from faker import Faker
fake =Faker()from faker.providers import BaseProvider
# Create a custom class
classMyProvider(BaseProvider):
def fun1(self):return'xxxxxx'
# Call add_provider method to add a provider
fake.add_provider(MyProvider)>>>fake.fun1()>>>'xxxxxx'
For more usage, please refer to the official document: https://faker.readthedocs.io/en/master/