Python--小程序

1.

# 如果同一个ip地址60s之内访问超过200次,那么就把ip加入黑名单
# 需求分析
# 1.60s读一次文件
# 2.分割,取第一个元素,ip地址
# 3.把所有ip加入到一个list里面,如果ip次数超过200次加入黑名单

import time

point = 0 # 文件指针
while True:
blk_set = set() # 定义空集合,存放需要加入黑名单的ip
ips = [] # 存放所有ip地址
with open('access.log') as f:
f.seek(point)
for line in f:
ip = line.split()[0]
ips.append(ip)
if ips.count(ip) > 200:
blk_set.add(ip)
for ip in blk_set: # 去重
print('已把%s加入黑名单' % ip)
point = f.tell()
time.sleep(60)

2.判断是否为小数
# 小数点的个数,小数点的个数是否等于1
# 按照小数点分割


def check_float(s):
s = str(s)
if s.count('.') == 1:
s_list = s.split('.')
# 1.2 [1, 2]
# -5.3 [-5, 4]
left = s_list[0]
right = s_list[1]
if left.isdigit() and right.isdigit(): # 正小数
return True
elif left.startswith('-') and left.count('-') == 1: # 负小数
if left.split('-')[-1].isdigit() and right.isdigit():
return True
return False

3.对比字典内容
dict1 = {
"version": "9.0.0",
"is_test": True,
"store": "",
"urs": "",
"device": {
"os": "android",
"ime1": "99001062198893",
"device_id": "CQliMWEyYTEzNTYyYzk5MzJmCTJlNmY3Zjkx",
"mac": "02:00:00:00:00:00",
"galaxy_tag": "CQliMWEyYTEzNTYyYzk5MzJmCTJlNmY3Zjkx",
"udid": "a34b1f67dd5797df93fdd8b072f1fb8110fd0db6",
"network_status": "wifi"
},
"adunit": {
"category": "VIDEO",
"location": "1",
"app": "7A16FBB6",
"blacklist": ""
},
"ext_param": {
"is_start": 0,
"vId": "VW0BRMTEV"
}
}

dict2 = {
"version": "9.0.0",
"is_test": True,
"urs": "",
"store": "",
"device": {
"os": "android",
"imei": "99001062298893",
"device_id": "CQliMWEyYTEzNTYyYzk5MzJmCTJlNmY3Zjkx",
"mac": "02:00:00:00:00:00",
"galaxy_tag": "CQliMWEyYTEzNTYyYzk5MzJmCTJlNmY3Zjkx",
"udid": "a34b1f67dd5797da93fdd8b072f1fb8110fd0db6",
"network_status": "wifi"
},
"adunit": {
"category": "VIDEO",
"location": "1",
"app": "7A16FBB6",
"blacklist": ""
}, "ext_param": {
"is_start": 0,
"vid": "VW0BRMTEV"
}
}


# http://www.nnzhp.cn/archives/433 第15题 提示:判断类型加递归
# 判断两个字典里面不一样的key,value
# 只需判断d1里面有
# 循环d1,通过key去d2里面取值
# 判断通过key取到的类型是什么,如果是字典的话,继续循环
# 然后再判断key和value

def compare(d1, d2):
for key in d1:
value1 = d1.get(key)
value2 = d2.get(key, '不存在')
if type(value1) == dict:
compare(value1, value2)
else:
if value1 != value2 and value2 == '不存在':
print('value不一样的key:%s,value1:%s,value2:%s' % (key, value1, value2))

res = set(d1.keys()).symmetric_difference(set(d2.keys())) # 对称差集,两个字典里面不一样的key
if res:
print('key不一样的是', ','.join(res))


compare(dict1, dict2)

4.商品管理小程序
# 作业2
# 写一个添加商品的程序
# 1.添加商品 商品名称/价格/数量/颜色 非空,价格校验,数量校验
# 2.查看商品
# 3.删除商品
# 5个函数 写文件一个 上面三个 判断价格


# 定义goods字典,存放商品信息
goods = {}


# 如果文件存在内容取文件内容。
def read_file(file_name):
global goods
with open(file_name, 'a+') as f:
f.seek(0)
if f.read():
f.seek(0)
goods = eval(f.read())
return goods


# 查看商品函数
def list_goods():
if goods:
print('商品包括:')
for key in goods:
print(key)
value = goods[key]
for v in value:
if v == '价格':
print('\t价格:{}'.format(value[v]))
elif v == '数量':
print('\t数量:{}'.format(value[v]))
elif v == '颜色':
print('\t颜色:{}'.format(value[v]))
else:
print('商品库中暂无商品!^_^')


# 校验价格
def check_price(price):
price = str(price).strip()
if price.isdigit():
return True
elif price.count('.') == 1:
s_list = price.split('.')
left = s_list[0]
right = s_list[1]
if left.isdigit() and right.isdigit(): # 正小数
return True
else:
return False
return False


# 判断数量
def check_number(number):
number = str(number).strip()

if number.isdigit():
if int(number) > 0:
return True
else:
return False
else:
return False


# 添加商品
def add_goods(file_name):
value = {} # 为字典的value定义一个空字典,存放数据
print('添加商品程序启动...')
print('如果想退出此程序请输入quit,^_^')
while True:
goods_name = input('请输入商品名称:').strip()
goods_price = input('请输入商品价格:').strip()
goods_number = input('请输入商品数量:').strip()
goods_colour = input('请输入商品颜色:').strip()
if goods_name == 'quit':
print('已退出添加商品!')
break
elif goods_name and check_price(goods_price) and check_number(goods_number) and goods_colour:
if goods_name in goods:
print('商品已存在!')
else:
value['价格'] = goods_price
value['数量'] = goods_number
value['颜色'] = goods_colour
goods[goods_name] = value
with open(file_name, 'w') as f_write:
f_write.write(str(goods))
print('恭喜,添加商品%s成功!' % goods_name)
break
else:
print('商品信息输入有误!')


# 删除商品
def remove_goods(file_name):
print('删除商品程序启动...')
while True:
goods_name = input('请输入商品名称:').strip()
if goods_name:
if goods_name in goods:
goods.pop(goods_name)
with open(file_name, 'w') as f_write:
f_write.write(str(goods))
print('%s已删除!' % goods_name)
break
else:
print('商品不存在哦,^_^')
else:
print('商品名称不可为空哦,^_^')


# 操作程序
def os_main(file_name):
read_file(file_name)
print('欢迎进入商品管理系统,^_^\n退出系统请输入:quit')
print('1.查看商品 2.添加商品 3.删除商品')
while True:
choice = input('请输入您的选项:').strip()
if choice.isdigit():
if int(choice) == 1:
list_goods()
elif int(choice) == 2:
add_goods(file_name)
elif int(choice) == 3:
remove_goods(file_name)
else:
print('输入有误!')
else:
if choice == 'quit':
break
else:
print('输入有误!')


os_main('商品.txt')
posted @ 2018-01-09 14:42  王思磊  阅读(256)  评论(0编辑  收藏  举报