前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python自定义实现车牌生成

Python自定义实现车牌生成

作者头像
苦叶子
发布2019-05-30 14:47:07
3.2K0
发布2019-05-30 14:47:07
举报
文章被收录于专栏:开源优测

从事车险行业的朋友,在日常测试过程中,需要各种车牌号码用于测试,如果在自动化测试过程中,则更需要去生成各种合规的车牌号码,

福利 | 跟Facebook大佬学习Python3

这里笔者基于Python Faker写了一个Faker的Provider插件,来实现生成各种车牌:

- 普通车牌

- 特种车牌

- 新能源小型车车牌

- 新能源大型车车牌

- 可以自定义生成车牌

- 可以生成不同省市机构发型的车牌

总体而言,实现了各种合规的车牌数据,可以单独调用,也可以作为Faker Providers方式进行调用。

下面看下实现源码:

代码语言:javascript
复制
# coding=utf-8
from __future__ import unicode_literals

from faker import Faker
from faker.providers import BaseProvider

import string

localized = True
class InsurProvider(BaseProvider):
    license_plate_provinces = (
        "京","沪","浙","苏","粤","鲁","晋","冀",
        "豫","川","渝","辽","吉","黑","皖","鄂",
        "津","贵","云","桂","琼","青","新","藏",
        "蒙","宁","甘","陕","闽","赣","湘"
        )

    license_plate_last = ("挂","学","警","港","澳","使","领")

    license_plate_num = (
        "A","B","C","D","E","F","G","H",
        "J","K","L","M","N","P","Q","R",
        "S","T","U","V","W","X","Y","Z",
        "1","2","3","4","5","6","7","8","9","0"
        )

    def license_plate(self):
        """ 传统车牌 """
        plate = "{0}{1}{2}".format(
            self.random_element(self.license_plate_provinces),
            self.random_uppercase_letter(),
            "".join(self.random_choices(elements=self.license_plate_num, length=5))
        )
        return plate

    def special_license_plate(self):
        """ 特种车牌 """
        plate = "{0}{1}{2}{3}".format(
            self.random_element(self.license_plate_provinces),
            self.random_uppercase_letter(),
            "".join(self.random_choices(elements=self.license_plate_num, length=4)),
            self.random_element(self.license_plate_last)
        )
        return plate

    def custom_license_plate(self, prov, org, last=None):
        """
        prov: 省简称
        org: 发牌机关简称字母
        last: 特种车汉字字符
        """
        if last is None:
            plate = "{0}{1}{2}".format(prov, org, "".join(self.random_choices(elements=self.license_plate_num, length=5)))
        else:
            plate = "{0}{1}{2}{3}".format(prov, org, "".join(self.random_choices(elements=self.license_plate_num, length=4)),last)

        return plate

    def new_energy_license_plate(self, car_model=1):
        """ 
        新能源车牌 
        car_model: 车型,0-小型车,1-大型车
        """
        plate = ""
        if car_model == 0:
            # 小型车
            plate = "{0}{1}{2}{3}{4}".format(self.random_element(self.license_plate_provinces), self.random_uppercase_letter(),
                self.random_element(elements=("D", "F")), self.random_element(elements=self.license_plate_num), self.random_int(1000, 9999))
        else:
            # 大型车
            plate = "{0}{1}{2}{3}".format(self.random_element(self.license_plate_provinces), self.random_uppercase_letter(),
                self.random_int(10000, 99999), self.random_element(elements=("D", "F")))

        return plate

    def test_print(self):
        print(self.new_energy_license_plate())

if __name__ == "__main__":
    k = Faker()
    p = InsurProvider(k)
    
    # 随机生成普通车牌
    print(p.license_plate())

    # 随机生成特种车牌
    print(p.special_license_plate())

    # 自定义普通车牌
    print(p.custom_license_plate("湘", "A"))

    # 自定义特种车牌
    print(p.custom_license_plate("湘", "A", "挂"))

    # 随机生成新能源小型车车牌
    print(p.new_energy_license_plate(0))

    # 随机生成新能源大型车车牌
    print(p.new_energy_license_plate(1))

运行下结果生成结果如下:

完!

后续进一步完善,考虑全面的实现车险领域用的数据生成器

大数据测试过程、策略及挑战

大数据测试之ETL测试入门

软件测试工程师又一大挑战:大数据测试

jmeter入门系列v1.0电子版

Python3接口测试pdf+源码免费领

快学Python3系列

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

本文分享自 开源优测 微信公众号,前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档