Python作业之用户管理
作业
流程图没有画,懒,不想画
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()
Python作业之用户管理的更多相关文章
- Python作业之工资管理
作业之工资管理 工资管理实现要求: 工资管理系统 Alex 100000 Rain 80000 Egon 50000 Yuan 30000 -----以上是info.txt文件----- 实现效果: ...
- Python 42 mysql用户管理 、pymysql模块
一:mysql用户管理 什么是mysql用户管理 mysql是一个tcp服务器,应用于操作服务器上的文件数据,接收用户端发送的指令,接收指令时需要考虑到安全问题, ATM购物车中的用户认证和mysql ...
- python作业之用户管理程序
数据库的格式化如下 分别为姓名|密码|电话号码|邮箱|用户类型 admin|admin123.|28812341026|admin@126.com|1root|admin123.|1344566348 ...
- 老男孩Day18作业:后台用户管理
一.作业需求: 1.用户组的增删改查 2.用户增删该查 - 添加必须是对话框 - 删除必须是对话框 - 修改,必须显示默认值 3.比较好看的页面 二.博客地址:https://www.cnblogs. ...
- 【Python之路】特别篇--基于领域驱动模型架构设计的京东用户管理后台
一.预备知识: 1.接口: - URL形式 - 数据类型 (Python中不存在) a.类中的方法可以写任意个,想要对类中的方法进行约束就可以使用接口: b.定义一个接口,接口中定义一个方法f1: c ...
- python作业学员管理系统(第十二周)
作业需求: 用户角色,讲师\学员, 用户登陆后根据角色不同,能做的事情不同,分别如下 讲师视图 管理班级,可创建班级,根据学员qq号把学员加入班级 可创建指定班级的上课纪录,注意一节上课纪录对应多条学 ...
- Linux运维六:用户管理及用户权限设置
Linux 系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这个账号的身份进入系统.用户的账号一方面可以帮助系统管理员对使用系统的用户进行 ...
- python作业ATM(第五周)
作业需求: 额度 15000或自定义. 实现购物商城,买东西加入 购物车,调用信用卡接口结账. 可以提现,手续费5%. 支持多账户登录. 支持账户间转账. 记录每月日常消费流水. 提供还款接口. AT ...
- 【淘淘】Quartz作业存储与管理
一.Quartz作业管理和存储方式简介: 作业一旦被调度,调度器需要记住并且跟踪作业和它们的执行次数.如果你的作业是30分钟后或每30秒调用,这不是很有用.事实上,作业执行需要非常准确和即时调用在被调 ...
随机推荐
- luogu P1879 [USACO06NOV]玉米田Corn Fields
题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ...
- restful的认识和用法
目录 一.restful的认识 1.基本概念 2.规范和约束 3.使用标准的状态码 二.具体使用 1.简单概括 2.根据id查询一个员工 3.查询所有员工 4.保存一个员工 5.根据id修改员工 6. ...
- 一个jdbc connection连接对应一个事务
Spring保证在methodB方法中所有的调用都获得到一个相同的连接.在调用methodB时,没有一个存在的事务,所以获得一个新的连接,开启了一个新的事务. Spring保证在methodB方法中所 ...
- 【GitHub】给GitHub上的ReadMe.md文件中添加图片怎么做 、 gitHub创建文件夹
1.首先在github上的仓库上,创建一个空的文件夹,用于上传图片 上图看 要点击的按钮是创建新的文件,并不是创建新的文件夹,具体怎么?往下看 这个时候,下面的提交按钮才能提交 2.进入新创建的文件夹 ...
- iOS -- 开源项目和库
TimLiu-iOS 目录 UI 下拉刷新 模糊效果 AutoLayout 富文本 图表 表相关与Tabbar 隐藏与显示 HUD与Toast 对话框 其他UI 动画 侧滑与右滑返回手势 gif动画 ...
- 拦截器及 Spring MVC 整合
一.实验介绍 1.1 实验内容 本节课程主要利用 Spring MVC 框架实现拦截器以及 Spring MVC 框架的整合. 1.2 实验知识点 Spring MVC 框架 拦截器 1.3 实验环境 ...
- Tyvj3308毒药解药题解
题目大意 这些药都有可能在治愈某些病症的同一时候又使人患上某些别的病症--经过我天才的努力.最终弄清了每种药的详细性能,我会把每种药能治的病症和能使人患上的病症列一张清单给你们,然后你们要依据这张清单 ...
- C++简单介绍
一.怎样用C++的源文件产生一个可运行程序 一个C++程序由一个或者多个编译单元组成.每一个编译单元都是一个独立的源码文件.一般是一个带.cpp的文件,编译器每次编一个文件编译单元,生成一个以.obj ...
- 关于在 C#中无法静态库引用的解决方法
在VS中用C#写了个类库,后面想转成静态库发现没有直接的方法,原来在C++中可以,而C#中不支持. 但是有时候程序引用C#编写的动态库觉得用户体验不好太累赘,想要简单只发一个exe可执行程序给用户就好 ...
- xml实现AOP
1.使用<aop:config></aop:config> 2.首先我们需要配置<aop:aspect></aop:aspect>,就是配置切面 2.1 ...