Python语言的简单实用小工具
小标 2018-12-10 来源 : 阅读 1164 评论 0

摘要:本文主要向大家介绍了Python语言的简单实用小工具,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。

本文主要向大家介绍了Python语言的简单实用小工具,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。

在python进行自动化编写的过程中,常常需要造一些数据,比如,获取随机的合法IP,随机的字符串,当前的时间等,下面的一些方法应该可以用到,希望对你有所帮助

#!/user/bin/env python
#coding=utf-8

import random
import socket
import string
import struct
import os
import datetime
import copy
import time
from framework.logger import Logger
logger = Logger(logger="rcpUtils").getlog()

BASE_DIR = os.path.dirname(os.path.dirname(file))
DC_PATH = BASE_DIR +  r"\config\districtcode.txt"

visaPrefixList = [
['4', '5', '3', '9'],
['4', '5', '5', '6'],
['4', '9', '1', '6'],
['4', '5', '3', '2'],
['4', '9', '2', '9'],
['4', '0', '2', '4', '0', '0', '7', '1'],
['4', '4', '8', '6'],
['4', '7', '1', '6'],
['4']]
mastercardPrefixList = [
['5', '1'], ['5', '2'], ['5', '3'], ['5', '4'], ['5', '5']]
amexPrefixList = [['3', '4'], ['3', '7']]
discoverPrefixList = [['6', '0', '1', '1']]
dinersPrefixList = [
['3', '0', '0'],
['3', '0', '1'],
['3', '0', '2'],
['3', '0', '3'],
['3', '6'],
['3', '8']]
enRoutePrefixList = [['2', '0', '1', '4'], ['2', '1', '4', '9']]
jcbPrefixList = [['3', '5']]
voyagerPrefixList = [['8', '6', '9', '9']]

MerchantNameList = ['凯总','澄邈','德泽','海超','海阳','海荣','海逸','海昌','瀚钰','瀚文','涵亮','昌盛','恨桃','依秋','依波','香巧','紫萱','涵易','忆之','幻巧','巧兰','惜蕊','雪晴','曼彤','宛秋','碧菡','若松','向秋','涵蕾','冰蝶','沛凝']
BankCardNumberlist = ['378288287735133','378271420218126','378238188701205','378207606238884','378275326032713','378258788874244','378210884152447','378224480016435','378288478747376','378232055211786']
MerchantUrlList_http = ['//www.kaizong.com','//www.baidu.com','//www.jd.com','//www.tengxun.com','//www.alibaba.com']
MerchantUrlList_https = ['https://www.kaizong.com','https://www.baidu.com','https://www.jd.com','https://www.tengxun.com','https://www.alibaba.com']
ITList = ['441411100101148','4245245469','4245245463','4245245467','441411100101149','4245245461','441411100101146',
'4245245462','441411100101147','56214714892','111111111111','56214714890']
generator = random.Random()
generator.seed()

'''工具类'''
class rcpUtils():

def __init__(self):

    pass

def AutoGeneratedString(self,number):

    '''随机生成字符串方法,主要用于输入框不能超过多少字符串的场景,此一次性产生的最大的字符串是62个'''

    return ''.join(random.sample(string.ascii_letters + string.digits, number))

def AutoGeneratedNumber(self,number):

    return ''.join(random.sample(string.digits, number))

def get_random_ip(self):

    RANDOM_IP_POOL=['192.168.10.222/0']

    '''随机生成合法的IP'''

    str_ip = RANDOM_IP_POOL[random.randint(0,len(RANDOM_IP_POOL) - 1)]

    str_ip_addr = str_ip.split('/')[0]

    str_ip_mask = str_ip.split('/')[1]

    ip_addr = struct.unpack('>I',socket.inet_aton(str_ip_addr))[0]

    mask = 0x0

    for i in range(31, 31 - int(str_ip_mask), -1):

        mask = mask | ( 1 << i)

    ip_addr_min = ip_addr & (mask & 0xffffffff)

    ip_addr_max = ip_addr | (~mask & 0xffffffff)

    return socket.inet_ntoa(struct.pack('>I', random.randint(ip_addr_min, ip_addr_max)))

def getMerchantName(self):

    #先对list去重

    list(set(MerchantNameList))

    return random.sample(MerchantNameList, 1)[0]

def getBankCardNumber(self):

    #先对list去重

    list(set(BankCardNumberlist))

    return random.sample(BankCardNumberlist, 1)[0]

def getCurrentTime(self):

    #2017-10-26 18:28:04,日期格式

    return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())

def getTime(self):

    return time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))

def getTime_YMD(self):

    '''获取当前的时间,年月日'''

    return time.strftime('%Y%m%d',time.localtime(time.time()))

def getMerchantUrl_http(self):

    list(set(MerchantUrlList_http))

    return random.sample(MerchantUrlList_http, 1)[0]

def getMerchantUrl_https(self):

    list(set(MerchantUrlList_https))

    return random.sample(MerchantUrlList_https, 1)[0]

def getIT(self):

    list(set(ITList))

    return random.sample(ITList, 1)[0]

def get_id(self,system_id,order_id):

    return order_id + '_' + system_id

def Cleaning_data_without_finger(self,RiskEventData):

    if type(RiskEventData) == dict:

        if RiskEventData:

            NoVerificationList = ['finger_id','create_time','json','create_date','process_des','process_date','alarm_flag','id','processor_name','processor_id']

            logger.info('不需要检查的字段为:%s'%NoVerificationList)

            for i in NoVerificationList:

                del RiskEventData[i]

            logger.info('做数据清洗后的数据为:%s'%RiskEventData)

        else:

            logger.exception('数据没有入库!')

    else:

        logger.exception('传进来的不是字典类型')

    return RiskEventData

def Cleaning_data_with_finger(self, RiskEventData):

    if type(RiskEventData) == dict:

        if RiskEventData:

            NoVerificationList = ['create_time', 'json', 'create_date', 'process_des', 'process_date',

                                  'alarm_flag', 'id', 'processor_name', 'processor_id']

            logger.info('不需要检查的字段为:%s' % NoVerificationList)

            for i in NoVerificationList:

                del RiskEventData[i]

            logger.info('做数据清洗后的数据为:%s' % RiskEventData)

        else:

            logger.exception('风险事件没有入库!')

    else:

        logger.exception('传进来的不是字典数据类型')

    return RiskEventData

本文由职坐标整理并发布,希望对同学们学习Python有所帮助,更多内容请关注职坐标编程语言Python频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程