前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >一个超级神奇的 Python 第三方库

一个超级神奇的 Python 第三方库

作者头像
Wu_Candy
发布2022-12-06 20:55:27
2500
发布2022-12-06 20:55:27
举报
文章被收录于专栏:无量测试之道无量测试之道

前言

前几天我被领导分配了一个项目需求,要求如下:

1.模拟一批数据,用于项目实验或者作为测试数据,因为真实数据不能展示。

2.模拟数据字段包括:

姓名、所在省份、详细地址、手机号、身份证号、出生年月、邮箱、人物、颜色、公司、银行信用卡、时间日期、文件、乱数假文、用户代理、社会安全码等。

一批数据手动一行行创建太不靠谱,因为无法保证创建数据的准确性,同时手动创建效率太低。

经过一番思考与查找,发现 Python 有个三方库 Faker能解决我的问题。

Faker 使用指南
Step1: 安装
代码语言:javascript
复制
# 使用以下命令进行安装
pip install Faker
Step2: 导入
代码语言:javascript
复制
from faker import Faker
Step3: 实例化faker
代码语言:javascript
复制
fake = Faker('zh_cn') //指定生成数据是什么语言
Step4: 实战代码
代码语言:javascript
复制
def random_name():
    """随机姓名"""
    return fake.name()


def random_name_male():
    '''男性姓名'''
    return fake.name_male()


def random_name_female():
    '''女性姓名'''
    return fake.name_female()


def random_email():
    """随机email"""
    return fake.email()


def random_password(length=10, special_chars=True, digits=True, upper_case=True, lower_case=True):
    '''
    随机密码(可指定密码策略)
    @param length:密码长度;
    @param special_chars:是否能使用特殊字符;
    @param digits:是否包含数字;
    @param upper_case:是否包含大写字母;
    @param lower_case:是否包含小写字母
    '''
    return fake.password(length=length, special_chars=special_chars, digits=digits, upper_case=upper_case, lower_case=lower_case)


def random_phone_number():
    """随机手机号"""
    return fake.phone_number()


def random_ean8():
    '''8位条码'''
    return fake.ean8()


def random_ean13():
    '''13位条码'''
    return fake.ean13()


def random_company():
    '''公司名'''
    return fake.company()


def random_currency_code():
    '''货币代码'''
    return fake.currency_code()


def random_date_time():
    '''随机日期时间'''
    return fake.date_time()


def random_date_time_this_month(before_now=True, after_now=False, tzinfo=None):
    '''
    本月的某个日期
    @param before_now 是否在今天之前的日期
    @param after_now 是否在今天之后的日期
    @param tzinfo
    '''
    return fake.date_time_this_month(before_now=before_now, after_now=after_now, tzinfo=tzinfo)


def random_date_time_this_year(before_now=True, after_now=False, tzinfo=None):
    '''
    本年的某个日期
    @param before_now 是否在今天之前的日期
    @param after_now 是否在今天之后的日期
    @param tzinfo
    '''
    return fake.date_time_this_year(before_now=before_now, after_now=after_now, tzinfo=tzinfo)


def random_date_time_this_decade(before_now=True, after_now=False, tzinfo=None):
    '''
    本年代内的一个日期
    @param before_now 是否在今天之前的日期
    @param after_now 是否在今天之后的日期
    @param tzinfo
    '''
    return fake.date_time_this_decade(before_now=before_now, after_now=after_now, tzinfo=tzinfo)


def random_date_time_this_century(before_now=True, after_now=False, tzinfo=None):
    '''
    本世纪一个日期
    @param before_now 是否在今天之前的日期
    @param after_now 是否在今天之后的日期
    @param tzinfo
    '''
    return fake.date_time_this_century(before_now=before_now, after_now=after_now, tzinfo=tzinfo)


def random_date_time_between(start_date="-30y", end_date="now", tzinfo=None):
    '''
    两个时间间的一个随机时间
    @param start_date 开始时间  -30y表示三十年内
    @param end_date 结束日期 now表示现在的时间
    @param tzinfo
    '''
    return fake.date_time_between(start_date=start_date, end_date=end_date, tzinfo=tzinfo)


def random_timezone():
    '''时区'''
    return fake.timezone()


def random_time(pattern="HH:MM:SS"):
    '''随机时间,格式:HH:MM:SS'''
    return fake.time(pattern=pattern)


def random_am_pm():
    '''随机上午下午'''
    return fake.am_pm()


def random_month():
    '''随机月份'''
    return fake.month()


def random_year():
    '''随机年'''
    return fake.year()


def random_day_of_week():
    '''随机星期几'''
    return fake.day_of_week()


def random_day_of_month():
    '''随机月中某一天'''
    return fake.day_of_month()


def random_time_delta():
    '''随机时间延迟'''
    return fake.time_delta()


def random_date_object():
    '''随机日期对象'''
    return fake.date_object()


def random_time_object():
    '''随机时间对象'''
    return fake.time_object()


def random_unix_time():
    '''随机unix时间(时间戳)'''
    return fake.unix_time()


def random_date(pattern="YY-mm-dd"):
    '''随机日期,格式:YY-mm-dd'''
    return fake.date(pattern=pattern)


def random_date_time_ad():
    '''公元后随机日期'''
    return fake.date_time_ad(tzinfo=None)


def random_uuid4():
    '''随机uuid'''
    return fake.uuid4()


def random_uuid():
    '''随机uuid'''
    return fake.uuid4().replace("-", "")


def random_locale():
    '''随机本地代码'''
    return fake.locale()


def random_phonenumber_prefix():
    '''运营商号段,手机号码前三位'''
    return fake.phonenumber_prefix()


def random_user_agent():
    '''伪造UA'''
    return fake.user_agent()


def random_ipv4():
    """随机IPV4地址"""
    return fake.ipv4()


def random_str(min_chars=0, max_chars=8):
    """长度在最大值与最小值之间的随机字符串"""
    return fake.pystr(min_chars=min_chars, max_chars=max_chars)


def random_num(length):
    """随机数字"""
    return fake.random_number(length)


if __name__ == '__main__':
    print(random_str(min_chars=5, max_chars=5))
    print(random_company())
    print(random_phone_number())
    print(random_num(length=5))
    print(random_date_time())
    print(random_unix_time())
    print(random_date_time_between(start_date="-5h", end_date="now"))
    print(random_password(length=6, special_chars=False))
运行结果

PS:运行结果中只输出了部分函数返回的数据信息,感兴趣的小伙伴可以动手实操起来,学会使用Faker,以后批量创建数据就很容易啦,关键是简单易学好上手。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-11-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 无量测试之道 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
    • Faker 使用指南
      • Step1: 安装
      • Step2: 导入
      • Step3: 实例化faker
      • Step4: 实战代码
      • 运行结果
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档