作业

流程图没有画,懒,不想画

readme没有写,懒,不想写。看注释吧233333

 #! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ == 'Djc' LOGIN_IN = {'is_login': False} # 记录当前的普通登录用户 # 普通用户装饰器,此装饰器的功能是验证是否有用户登录
def outer(func):
def inner():
if LOGIN_IN['is_login']: # 检验是否有用户登录了
func()
else:
print("Please login!") # 若没有,跳转menu函数
return inner # 管理员装饰器
def mag_outer(func):
def inner(*args, **kwargs):
if bool(int(LOGIN_IN['user_power'])):
func(*args, **kwargs)
else:
print("Sorry,you can't running this function!")
return inner def login():
usr = input("Please enter your name: ")
pwd = input("Please enter your password: ") f = open("user_information", 'r')
for i in f:
if usr == i.split("|")[0]: # 检验登录用户是否存在,不存在跳入注册函数
break
else:
print("This usr is not exist!") f.seek(0)
# 遍历用户文件,登录成功将用户名放在全局字典中
for line in f:
# user_power = line.strip().split("|")[3]
if usr == line.split("|")[0] and pwd == line.strip().split("|")[1]:
user_power = line.strip().split("|")[3] # 获取用户权限
print("Log in successful,welcome to you")
LOGIN_IN['is_login'] = True
LOGIN_IN['current'] = usr
LOGIN_IN['password'] = pwd
LOGIN_IN['user_power'] = user_power
break
else: # 否则提示密码错误,退出程序
print("Sorry,your password is wrong!")
f.close() # 注册
def register():
usr = input("Please enter your name: ")
pwd = input("Please enter your password: ")
signature = input("Please enter your signature: ")
f = open("user_information", 'r+')
flag = True # 设立一个标识符
for i in f:
if i.split("|")[0] == usr: # 检测此用户名是否存在,存在则不允许注册
print("Sorry,this name is exist,try again!")
flag = False
break f.seek(0) # 将文件指针位置放回起始位置,这一步非常重要!!!!
for i in f:
if flag and i.strip():
new_usr = '\n' + usr + "|" + pwd + '|' + signature + "|" + ''
f.write(new_usr)
print("Register successful")
break
f.close() # 关闭文件 # 用户查看自己信息,用装饰器装饰。具体可参考装饰器的应用
@outer
def usr_view():
print("Welcome to you!")
with open("user_information", 'r+') as f:
for line in f:
if line.strip().split("|")[0] == LOGIN_IN['current']: # 遍历文件,输出此时登录者的信息
print(line) # 退出当前用户
@outer
def usr_exit():
LOGIN_IN['is_login'] = False # 用户改变密码函数
@outer
def change_pwd():
print("Welcome to you!")
new_pwd = input("Please enter the password that you want to modify: ") # 新密码
in_put = open("user_information", 'r')
out_put = open("user_information", 'r+')
for line in in_put:
if LOGIN_IN['password'] == line.strip().split("|")[1]: # 验证是否是用户本人想修改密码
break
else:
print("Sorry,you may not own!")
menu()
in_put.seek(0) # 将文件指针返回文件起始位置
for line in in_put:
if "|" in line and LOGIN_IN['current'] in line and LOGIN_IN['is_login']:
temp = line.split("|")
temp2 = temp[0] + '|' + new_pwd + '|' + temp[2] + '|' + temp[3] # 将新密码写入原文件
out_put.write(temp2)
else:
out_put.write(line) # 将不需要做改动的用户写入原文件
print("Yeah,modify successful")
out_put.close()
in_put.close() def user_log():
pass '''
-----------------------------------------------------
* 管理员用户登录
* 可以登录,注册,查看本用户信息
* 删除,添加普通用户
* 查看所有普通用户,按照指定关键字搜索用户信息(模糊搜索)
* 提高普通用户权限?
-----------------------------------------------------
''' @mag_outer
def manager():
print("Welcome to you,our manager!")
ma_pr = '''
(V)iew every user
(D)elete user
(A)dd user
(S)ercher user
(I)mprove user power
(B)ack
(Q)exit
Enter choice:'''
while True:
try:
choice = input(ma_pr).strip()[0].lower()
except (KeyError, KeyboardInterrupt):
choice = 'q' if choice == 'v':
view_usr()
if choice == 'd':
name = input("Please enter the name that you want to delete? ")
count = 0
with open('user_information', 'r') as f:
for line in f:
if line.strip().split("|")[0] == name:
delete_usr(count)
break
count += 1 if choice == 'a':
add_user()
if choice == 's':
find_user()
if choice == 'i':
improve_user()
if choice == 'b':
menu()
if choice == 'q':
exit() # 查看所有用户的信息
@mag_outer
def view_usr():
with open("user_information", 'r+') as f:
for line in f:
print(line, end='') # 管理员删除用户
@mag_outer
def delete_usr(lineno):
fro = open('user_information', "r") # 文件用于读取 current_line = 0
while current_line < lineno:
fro.readline()
current_line += 1 # 将文件指针定位到想删除行的开头 seekpoint = fro.tell() # 将此时文件指针的位置记录下来
frw = open('user_information', "r+") # 文件用于写入,与用于读取的文件是同一文件
frw.seek(seekpoint, 0) # 把记录下来的指针位置赋到用于写入的文件 # read the line we want to discard
fro.readline() # 读入一行进内内存 同时! 文件指针下移实现删除 # now move the rest of the lines in the file
# one line back
chars = fro.readline() # 将要删除的下一行内容取出
while chars:
frw.writelines(chars) # 写入frw
chars = fro.readline() # 继续读取,注意此处的读取是按照fro文件的指针来读 print("Delete successful!")
fro.close()
frw.truncate() # 截断,把frw文件指针以后的内容清除
frw.close() @mag_outer
def find_user():
name = input("Please enter the name that you want to find: ")
with open("user_information", "r") as f:
for line in f:
if line.strip().split("|")[0] == name:
print(line)
break @mag_outer
def improve_user():
name = input("Please enter the name that you want to find: ")
power = input("What's power do you want to give ?")
in_put = open("user_information", 'r')
out_put = open("user_information", 'r+')
for line in in_put:
if line.strip().split("|")[0] == name:
temp = line.split("|")
temp1 = temp[0] + '|' + temp[1] + '|' + temp[2] + '|' + power + '\n'
out_put.write(temp1)
else:
out_put.write(line)
in_put.close()
out_put.close() @mag_outer
def add_user():
register() @mag_outer
def manager_log():
import logging logger = logging.getLogger(LOGIN_IN['current']) # 设立当前登录的管理员为logger
logger.setLevel(logging.DEBUG) # 设立日志输出等级最低为DEBUG file_handler = logging.FileHandler('user_log') # 创建向文件输出日志的handler
file_handler.setLevel(logging.DEBUG) # 文件中日志输出等级最低为DEBUG formatter = logging.Formatter("%(asctime)s %(name)s %(levelname)s %(message)s") # 设立日志输出格式
file_handler.setFormatter(formatter) # 将格式添加到handler中 logger.addHandler(file_handler) # 将handler注册到logger logger.debug() # 菜单函数
def menu():
pr = '''
(L)ogin
(R)egister
(U)ser view
(C)hange pwd
(M)anager
(E)xit usr
(Q)exit
Enter choice:'''
while True:
try:
choice = input(pr).strip()[0].lower()
except (KeyboardInterrupt, KeyError):
choice = 'q' if choice == 'l':
login()
if choice == 'r':
register()
if choice == 'u':
usr_view()
if choice == 'c':
change_pwd()
if choice == 'm':
manager()
if choice == 'e':
usr_exit()
if choice == 'q':
exit() if __name__ == '__main__':
menu()

最新文章

  1. Java部署_IntelliJ创建一个可运行的jar包(实践)
  2. 【转】使用:after清除浮动
  3. dedecms为后台自定义菜单的完整方法
  4. Aptana快捷键(方便查询)
  5. C++ 虚函数表 多重继承
  6. 四、自动化平台搭建-Django-如何做验证码
  7. 【CentOS】自定义服务添加
  8. Machine learning | 机器学习中的范数正则化
  9. 集群容器管理之swarm ---集群部署
  10. 2018.1.7java转型
  11. Anaconda(python3.6)中使用python2.7
  12. 四种List实现类的对比总结
  13. modbus.c
  14. 在PC上像普通winform程序调试WINCE程序
  15. webpack流程图
  16. Tomcat之Windows环境下配置多个服务器
  17. Hbase和Hive的异同
  18. C# 对象引擎,以路径形式访问对象属性(data.Product[1].Name)
  19. php curl常用的5个例子
  20. C/C++ 读取16进制文件

热门文章

  1. 公共返回JSON信息的方法
  2. 期望DP初步
  3. pt-pmp :pt toolkit
  4. 【jar】JDK将单个的java文件打包为jar包,并引用到项目中使用【MD5加密】
  5. Linux Distribution
  6. php-fpm.conf配置说明(重点要改动和优化的地方)
  7. python 图像识别转文字
  8. maven的学习系列(二)—maven的文件夹结构
  9. [3 Jun 2015 ~ 9 Jun 2015] Deep Learning in arxiv
  10. POJ 2309 BST