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

2、 Basic information

3、 Mailbox information

4、 Basic Network Information

5、 Browser information

6、 Digital information

7、 Text encryption

8、 Time information

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/

Recommended Posts

Python's artifact library for constructing data-Faker