前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python的随机库也太好用啦

Python的随机库也太好用啦

作者头像
Wu_Candy
发布2024-04-15 17:01:43
640
发布2024-04-15 17:01:43
举报
文章被收录于专栏:无量测试之道无量测试之道
背景

在工作或学习中如果你有经常要使用一些生成器方法来生成随机数、手机号、名称以及连续数字等需求,那么Python的Faker随机库将是你的不二之选,它将极大提高你的工作或学习效率

Python实现代码
代码语言:javascript
复制
import random
from faker import Faker

fake = Faker('zh_cn')

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_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))
    print(random_company())
    print(random_name())
代码解释

Python实现代码中对于每个方法都有详细的中文说明,可以直接copy代码到本地运行起来,这个随机库的运用比较简单,但很好用~

如果今天的分享对你有帮助的话,请毫不犹豫:关注、分享、点赞、在看、收藏呀~ 你的鼓励将会是我创作的最大动力。

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 背景
    • Python实现代码
      • 代码解释
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
      http://www.vxiaotou.com