码迷,mamicode.com
首页 > 其他好文 > 详细

测试自动化学习4

时间:2019-04-18 23:22:42      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:strong   包含   access   put   格式化   特殊   次数   输入   实参   

元祖创建后不可修改

t = (a,b,c,a,a)
print(t[1])

print(t.count(a))
print(t.index(a))

 

集合 set 天生可以去重,而且集合里是无序的

l = [1,2,3,3,4,4]
print(set(l))
{1, 2, 3, 4}  但是不是字典

class1 = [lx,ddw,bbt]
class2 = [lx,ddw,aa,cc]



交集
print(set(class1).intersection(set(class2)))    # 两个集合,取交集
print(set(class1) & set(class2))    # 同上

要求 必须包含大写字母、小写字母、数字、特殊字符
非空即真,非0即真
import string
passwd = input(请输入密码:)
p_set = set(passwd)
if p_set & set(string.ascii_lowercase)        and p_set & set(string.ascii_uppercase)        and p_set & set(string.digits)        and p_set & set(string.punctuation):
    print(密码强度足够)
else:
    print(密码必须包含大写字母、小写字母、数字、特殊字符)



# 并集
s1 = {1,2,3,4}
s2 = {4,5,6,7}
print(s1.union(s2))
print(s1 | s2)

# 差集
print(s1.difference(s2))    # s1有,而s2没有的
print(s1 - s2)

# 对称差集
print(s1.symmetric_difference(s2))    # 把s1和s2都有的去掉
print(s1^s2)

# 增删
s1.add(adc)
print(s1)
s1.remove(adc)
print(s1)

文件读写

import time
import os

#打开文件
f = open(a.txt,r,encoding=utf-8)    # r读,文件不存在会报错 w写,文件不存在会创建,不能读,会情况原来文件 a追加,文件不存在会帮创建,换行要加\n
"""
r+ 读写模式,文件不存在报错,可以写(但是是替换第一个指针位置内容)
w+ 写读模式,文件不存在会创建,覆盖写,可以读不报错,但是文件已经被清空读不到东西
a+ 追加读模式,文件不存在会创建,追加写,可以读不报错,但是文件已经被清空读不到东西
"""
f = open(a.txt,a+,encoding=utf-8)
# print(f.readline())
# print(f.readline())
f.seek(0)    # 读取的指针移到位置0
print(f.read())
f.write(\naaa)    # 写的时候还是在追加的位置
print(----)
f.seek(0)
print(f.read())
f.close()

f.seek(0)
l = [123,456,789]
for i in range(len(l)):
    l[i] = l[i] + \n

f.writelines(l)    # 传一个list,把list里面每一个元素写入文件,记得加\n

f.truncate()    # 从文件指针位置往后删除内容
print(f.tell())    # 打印当前文件指针位置
f.close()

""" with open 打开两个文件 """
with open(a.txt) as f1,open(b.txt) as f2:
    f1.read()
    f2.read()
score = 0
with open(a.txt) as f:
    for line in f:
        print(line.split(,)[1])

#刷新文件
#
f.flush()

"""每隔60秒读取log,打印1分钟内访问超过20次的ip""" point = 0 while True: with open(access.log) as f: f.seek(point) #文件指针 ips = {} for line in f: ip = line.split( )[0] if ip not in ips: ips[ip] = 1 else: ips[ip] += 1 point = f.tell() for k,v in ips.items(): if v > 20: print(访问次数异常ip地址是:%s%k) print(----) time.sleep(5) """在内容为 name,score1,score2 的文件,统计分数并加到最后一列""" with open(a.txt) as f1,open(b.txt,w) as f2: for line in f1: list = line.strip().split(,) avg = (int(list[1]) + int(list[2]))/3 f2.writelines(%s,%s\n%(line.strip(\n),avg)) os.remove(a.txt) os.rename(b.txt,a.txt)

切片

l = [xiaohei,100,89,73]
# 切片的时候顾头不顾尾,写 1:4 才能取到位置 3
print(l[1:4])    # 等于 l[1:] 末尾不写等于取到最末尾
print(l[:3])    # 等于从头取到 位置 2
print(l[:])

print(sum(l[1:]))    # list 内置了sum函数

nums = list(range(1,21))
print(nums[:10])    # 取1到10
print(nums[:10:2])    # 步长为2,从1到10 每隔1个取一次 取出 1 3 5 7 9
print(nums[::-1])    # 步长为-1,从右往左取值,从20打印到1
print(nums[0:10:-2])    # 从0开始取,但是往右取,所以取不到值
print(nums[:10:-2])    # 从位置-1开始,位置 -2 -3
print(nums.reverse())    #直接把list反转

"""打印不定列表长度的倒数5个元素"""
t = [3,6,35,8,9,65,43,22,2,678,21]
print(t[:-6:-1])    # :-6:-1 表示步长为 -1,结束位置为倒数 -6的位置(倒数起始位置为 -1),切片顾头不顾尾,所以 -6位置往右一位取到 -5

函数:一个函数不要超过20行,需要实现多个功能的拆分开写

def ggg():
    print(这是一个函数)

def StrongPassWord(passwd):
    import string
    p_set = set(passwd)
    if p_set & set(string.ascii_lowercase)            and p_set & set(string.ascii_uppercase)            and p_set & set(string.digits)            and p_set & set(string.punctuation):
        return True
    else:
        return False

# print(StrongPassWord(‘ddaD1$‘))

def calc(a,b):    # 形参,形式参数
    return a + b

print(calc(1,2))    #实参,实际参数

# if StrongPassWord(‘ddaD1$‘):
#     print(‘ok‘)
# else:
#     print(‘not‘)


num = -1.223
if num.count(.) == 1 and num.split(.)[0].isdigit() and num.split(.)[1].isdigit():
    print(yy)
elif num.startswith(-) and num.count(.) == 1 and num.strip(-).split(.)[0].isdigit() and num.strip(-).split(.)[1].isdigit():
    print(yy-)

# 函数遇到 return 立即结束
def Check_Float(num):
    num = str(num)
    if num.count(.) == 1 and num.split(.)[0].isdigit() and num.split(.)[1].isdigit():
        return True
    elif num.startswith(-) and num.count(.) == 1 and num.strip(-).split(.)[0].isdigit() and             num.strip(-).split(.)[1].isdigit():
        return True
    return False

json模块

import json

#存在文件里面的东西读出来后都是字符串

# d = {"nhy":"123456","ylm":"4545566",‘yyd‘:‘哼哼哈嘿‘}

""" 
indent=4写入的时候加上4个空格,json内容会被格式化显示,更好看
ensure_ascii=False 写入中文的时候不会被显示成Unicode编码
"""
# res = json.dumps(d,indent=4,ensure_ascii=False)    # json.dumps 把字典转成json串,json里面只有",没有‘,会把‘都替换成"
# print(type(res))
#
# with open(‘users.txt‘,‘w+‘) as f:
#     f.write(res)

# with open(‘users.txt‘,‘r+‘) as f:
#     res = json.load(f)    # json.load 把字符串转成字典,如果用loads,则是转成字符串,需要json.loads(f.read())
#     print(res)
#     print(res[‘nhy‘])


d = {
        "error_code": 0,
        "stu_info": [
                {
                        "id": 8485,
                        "name": "kqs2",
                        "sex": "middle",
                        "age": 38,
                        "addr": "木星",
                        "grade": "-5",
                        "phone": "19888",
                        "gold": 100
                }
        ]
}

with open(users.json,w+) as f:
    json.dump(d,f,indent=4,ensure_ascii=False)

 

测试自动化学习4

标签:strong   包含   access   put   格式化   特殊   次数   输入   实参   

原文地址:https://www.cnblogs.com/fatenet/p/10732971.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!