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 = ['http://www.kaizong.com','http://www.baidu.com','http://www.jd.com','http://www.tengxun.com','http://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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章