一、Enum结构
- 从enum导入Enum,并让自定义的枚举类继承Enum
- 枚举类内的标签最好用大写字母来表示常量
from enum import Enum, unique @unique # @unique装饰器可以帮助我们检查保证没有重复值 class StoreApproachEnum(Enum): def __new__(cls, english, chinese, status,text): obj = object.__new__(cls) obj.english = english obj.chinese = chinese obj.status = status obj.text = text return obj SHOWAILOCK = "showAiLock", "智能锁", "1", "该门店有" SHOWDELIVERHOME = "showDeliverHome", "配送到家", "1", "该门店有" SHOWORDER = "showOrder", "订单", "1", "该门店有" SHOWOUTDOORBUY = "showOutDoorBuy", "自助购", "1", "该门店有" SHOWSELFBUY = "showSelfBuy", "智能锁", "1", "该门店有" SHOWSELFCHECKOUT = "showSelfCheckOut", "自由购", "1", "该门店有" SHOWSUPERHI = "showSuperHi", "起嗨", "1", "该门店有" STATUS = "status", "启用", "1", "该门店"
二、应用
2.1 打印所有标签
for i in StoreApproachEnum: print(i.name) # ---------------------------- SHOWAILOCK SHOWDELIVERHOME SHOWORDER SHOWOUTDOORBUY SHOWSELFBUY SHOWSELFCHECKOUT SHOWSUPERHI STATUS
2.2 打印枚举值
for i in StoreApproachEnum: # print(i.name) print(i.chinese) #----------------------- 智能锁 配送到家 订单 自助购 智能锁 自由购 起嗨 启用
2.3 打印枚举值
for i in StoreApproachEnum: # print(i.name) # print(i.chinese) if i.status == "1": print(i.text+i.chinese) #--------------------- 该门店有智能锁 该门店有配送到家 该门店有订单 该门店有自助购 该门店有智能锁 该门店有自由购 该门店有起嗨 该门店启用
三、接口自动化中的应用
3.1 枚举定义文件py
from enum import Enum, unique @unique # @unique装饰器可以帮助我们检查保证没有重复值 class StoreApproachEnum(Enum): def __new__(cls, english, chinese): obj = object.__new__(cls) obj.english = english obj.chinese = chinese return obj SHOWAILOCK = "showAiLock", "智能锁" SHOWDELIVERHOME = "showDeliverHome", "配送到家" SHOWORDER = "showOrder", "订单" SHOWOUTDOORBUY = "showOutDoorBuy", "自助购" SHOWSELFBUY = "showSelfBuy", "智能锁" SHOWSELFCHECKOUT = "showSelfCheckOut", "自由购" SHOWSUPERHI = "showSuperHi", "起嗨" STATUS = "status", "门店状态"
3.2 解析响应中调用
def parse_store_approach_from_homePage(resp=None): """ 门店配置数据解析 :param resp: :return: """ store_approach_dict = {} resCode = resp["code"] if resCode == "0000" and resp["data"]: store_approach_dict["showAiLock"] = resp["data"]["showAiLock"] store_approach_dict["showDeliverHome"] = resp["data"]["showDeliverHome"] store_approach_dict["showOrder"] = resp["data"]["showOrder"] store_approach_dict["showOutDoorBuy"] = resp["data"]["showOutDoorBuy"] store_approach_dict["showSelfBuy"] = resp["data"]["showSelfBuy"] store_approach_dict["showSelfCheckOut"] = resp["data"]["showSelfCheckOut"] store_approach_dict["showSuperHi"] = resp["data"]["showSuperHi"] store_approach_dict["status"] = resp["data"]["status"] # 拿到字典中的keys for i in list(store_approach_dict.keys()): # 拿到enum文件中的标签 for j in e: # 判断字典中的key值如果等于enum的标签 if i.upper() == j.name: # 删除字典中key对应的值,并赋给新的key,再次添加到字典中 store_approach_dict[j.chinese] = store_approach_dict.pop(i) # print(store_approach_dict) return store_approach_dict else: return None
3.3 测试类中调用解析响应方法
result = parse_wx_test_data.parse_store_approach_from_homePage(r_dict) for k,v in result.items(): # print(k,v) logger.info("{}-{}-{}-{}-{}{}".format(storeID[0],storeID[1],storeID[2],storeID[3],"启用了" if v else "未开启",k)) print("{}-{}-{}-{}-{}{}".format(storeID[0],storeID[1],storeID[2],storeID[3],"启用了" if v else "未开启",k),end=".") print()
输出:
1-北京超市-66666-沁山水店-启用了智能锁.1-北京超市-66666-沁山水店-启用了配送到家.1-北京超市-66666-沁山水店-启用了订单.1-北京超市-66666-沁山水店-未开启自助购.1-北京超市-66666-沁山水店-启用了自由购.1-北京超市-66666-沁山水店-未开启起嗨.1-北京超市-66666-沁山水店-启用了门店状态.