作业

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

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

  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # __author__ == 'Djc'
  4.  
  5. LOGIN_IN = {'is_login': False} # 记录当前的普通登录用户
  6.  
  7. # 普通用户装饰器,此装饰器的功能是验证是否有用户登录
  8. def outer(func):
  9. def inner():
  10. if LOGIN_IN['is_login']: # 检验是否有用户登录了
  11. func()
  12. else:
  13. print("Please login!") # 若没有,跳转menu函数
  14. return inner
  15.  
  16. # 管理员装饰器
  17. def mag_outer(func):
  18. def inner(*args, **kwargs):
  19. if bool(int(LOGIN_IN['user_power'])):
  20. func(*args, **kwargs)
  21. else:
  22. print("Sorry,you can't running this function!")
  23. return inner
  24.  
  25. def login():
  26. usr = input("Please enter your name: ")
  27. pwd = input("Please enter your password: ")
  28.  
  29. f = open("user_information", 'r')
  30. for i in f:
  31. if usr == i.split("|")[0]: # 检验登录用户是否存在,不存在跳入注册函数
  32. break
  33. else:
  34. print("This usr is not exist!")
  35.  
  36. f.seek(0)
  37. # 遍历用户文件,登录成功将用户名放在全局字典中
  38. for line in f:
  39. # user_power = line.strip().split("|")[3]
  40. if usr == line.split("|")[0] and pwd == line.strip().split("|")[1]:
  41. user_power = line.strip().split("|")[3] # 获取用户权限
  42. print("Log in successful,welcome to you")
  43. LOGIN_IN['is_login'] = True
  44. LOGIN_IN['current'] = usr
  45. LOGIN_IN['password'] = pwd
  46. LOGIN_IN['user_power'] = user_power
  47. break
  48. else: # 否则提示密码错误,退出程序
  49. print("Sorry,your password is wrong!")
  50. f.close()
  51.  
  52. # 注册
  53. def register():
  54. usr = input("Please enter your name: ")
  55. pwd = input("Please enter your password: ")
  56. signature = input("Please enter your signature: ")
  57. f = open("user_information", 'r+')
  58. flag = True # 设立一个标识符
  59. for i in f:
  60. if i.split("|")[0] == usr: # 检测此用户名是否存在,存在则不允许注册
  61. print("Sorry,this name is exist,try again!")
  62. flag = False
  63. break
  64.  
  65. f.seek(0) # 将文件指针位置放回起始位置,这一步非常重要!!!!
  66. for i in f:
  67. if flag and i.strip():
  68. new_usr = '\n' + usr + "|" + pwd + '|' + signature + "|" + ''
  69. f.write(new_usr)
  70. print("Register successful")
  71. break
  72. f.close() # 关闭文件
  73.  
  74. # 用户查看自己信息,用装饰器装饰。具体可参考装饰器的应用
  75. @outer
  76. def usr_view():
  77. print("Welcome to you!")
  78. with open("user_information", 'r+') as f:
  79. for line in f:
  80. if line.strip().split("|")[0] == LOGIN_IN['current']: # 遍历文件,输出此时登录者的信息
  81. print(line)
  82.  
  83. # 退出当前用户
  84. @outer
  85. def usr_exit():
  86. LOGIN_IN['is_login'] = False
  87.  
  88. # 用户改变密码函数
  89. @outer
  90. def change_pwd():
  91. print("Welcome to you!")
  92. new_pwd = input("Please enter the password that you want to modify: ") # 新密码
  93. in_put = open("user_information", 'r')
  94. out_put = open("user_information", 'r+')
  95. for line in in_put:
  96. if LOGIN_IN['password'] == line.strip().split("|")[1]: # 验证是否是用户本人想修改密码
  97. break
  98. else:
  99. print("Sorry,you may not own!")
  100. menu()
  101. in_put.seek(0) # 将文件指针返回文件起始位置
  102. for line in in_put:
  103. if "|" in line and LOGIN_IN['current'] in line and LOGIN_IN['is_login']:
  104. temp = line.split("|")
  105. temp2 = temp[0] + '|' + new_pwd + '|' + temp[2] + '|' + temp[3] # 将新密码写入原文件
  106. out_put.write(temp2)
  107. else:
  108. out_put.write(line) # 将不需要做改动的用户写入原文件
  109. print("Yeah,modify successful")
  110. out_put.close()
  111. in_put.close()
  112.  
  113. def user_log():
  114. pass
  115.  
  116. '''
  117. -----------------------------------------------------
  118. * 管理员用户登录
  119. * 可以登录,注册,查看本用户信息
  120. * 删除,添加普通用户
  121. * 查看所有普通用户,按照指定关键字搜索用户信息(模糊搜索)
  122. * 提高普通用户权限?
  123. -----------------------------------------------------
  124. '''
  125.  
  126. @mag_outer
  127. def manager():
  128. print("Welcome to you,our manager!")
  129. ma_pr = '''
  130. (V)iew every user
  131. (D)elete user
  132. (A)dd user
  133. (S)ercher user
  134. (I)mprove user power
  135. (B)ack
  136. (Q)exit
  137. Enter choice:'''
  138. while True:
  139. try:
  140. choice = input(ma_pr).strip()[0].lower()
  141. except (KeyError, KeyboardInterrupt):
  142. choice = 'q'
  143.  
  144. if choice == 'v':
  145. view_usr()
  146. if choice == 'd':
  147. name = input("Please enter the name that you want to delete? ")
  148. count = 0
  149. with open('user_information', 'r') as f:
  150. for line in f:
  151. if line.strip().split("|")[0] == name:
  152. delete_usr(count)
  153. break
  154. count += 1
  155.  
  156. if choice == 'a':
  157. add_user()
  158. if choice == 's':
  159. find_user()
  160. if choice == 'i':
  161. improve_user()
  162. if choice == 'b':
  163. menu()
  164. if choice == 'q':
  165. exit()
  166.  
  167. # 查看所有用户的信息
  168. @mag_outer
  169. def view_usr():
  170. with open("user_information", 'r+') as f:
  171. for line in f:
  172. print(line, end='')
  173.  
  174. # 管理员删除用户
  175. @mag_outer
  176. def delete_usr(lineno):
  177. fro = open('user_information', "r") # 文件用于读取
  178.  
  179. current_line = 0
  180. while current_line < lineno:
  181. fro.readline()
  182. current_line += 1 # 将文件指针定位到想删除行的开头
  183.  
  184. seekpoint = fro.tell() # 将此时文件指针的位置记录下来
  185. frw = open('user_information', "r+") # 文件用于写入,与用于读取的文件是同一文件
  186. frw.seek(seekpoint, 0) # 把记录下来的指针位置赋到用于写入的文件
  187.  
  188. # read the line we want to discard
  189. fro.readline() # 读入一行进内内存 同时! 文件指针下移实现删除
  190.  
  191. # now move the rest of the lines in the file
  192. # one line back
  193. chars = fro.readline() # 将要删除的下一行内容取出
  194. while chars:
  195. frw.writelines(chars) # 写入frw
  196. chars = fro.readline() # 继续读取,注意此处的读取是按照fro文件的指针来读
  197.  
  198. print("Delete successful!")
  199. fro.close()
  200. frw.truncate() # 截断,把frw文件指针以后的内容清除
  201. frw.close()
  202.  
  203. @mag_outer
  204. def find_user():
  205. name = input("Please enter the name that you want to find: ")
  206. with open("user_information", "r") as f:
  207. for line in f:
  208. if line.strip().split("|")[0] == name:
  209. print(line)
  210. break
  211.  
  212. @mag_outer
  213. def improve_user():
  214. name = input("Please enter the name that you want to find: ")
  215. power = input("What's power do you want to give ?")
  216. in_put = open("user_information", 'r')
  217. out_put = open("user_information", 'r+')
  218. for line in in_put:
  219. if line.strip().split("|")[0] == name:
  220. temp = line.split("|")
  221. temp1 = temp[0] + '|' + temp[1] + '|' + temp[2] + '|' + power + '\n'
  222. out_put.write(temp1)
  223. else:
  224. out_put.write(line)
  225. in_put.close()
  226. out_put.close()
  227.  
  228. @mag_outer
  229. def add_user():
  230. register()
  231.  
  232. @mag_outer
  233. def manager_log():
  234. import logging
  235.  
  236. logger = logging.getLogger(LOGIN_IN['current']) # 设立当前登录的管理员为logger
  237. logger.setLevel(logging.DEBUG) # 设立日志输出等级最低为DEBUG
  238.  
  239. file_handler = logging.FileHandler('user_log') # 创建向文件输出日志的handler
  240. file_handler.setLevel(logging.DEBUG) # 文件中日志输出等级最低为DEBUG
  241.  
  242. formatter = logging.Formatter("%(asctime)s %(name)s %(levelname)s %(message)s") # 设立日志输出格式
  243. file_handler.setFormatter(formatter) # 将格式添加到handler中
  244.  
  245. logger.addHandler(file_handler) # 将handler注册到logger
  246.  
  247. logger.debug()
  248.  
  249. # 菜单函数
  250. def menu():
  251. pr = '''
  252. (L)ogin
  253. (R)egister
  254. (U)ser view
  255. (C)hange pwd
  256. (M)anager
  257. (E)xit usr
  258. (Q)exit
  259. Enter choice:'''
  260. while True:
  261. try:
  262. choice = input(pr).strip()[0].lower()
  263. except (KeyboardInterrupt, KeyError):
  264. choice = 'q'
  265.  
  266. if choice == 'l':
  267. login()
  268. if choice == 'r':
  269. register()
  270. if choice == 'u':
  271. usr_view()
  272. if choice == 'c':
  273. change_pwd()
  274. if choice == 'm':
  275. manager()
  276. if choice == 'e':
  277. usr_exit()
  278. if choice == 'q':
  279. exit()
  280.  
  281. if __name__ == '__main__':
  282. menu()

Python作业之用户管理的更多相关文章

  1. Python作业之工资管理

    作业之工资管理 工资管理实现要求: 工资管理系统 Alex 100000 Rain 80000 Egon 50000 Yuan 30000 -----以上是info.txt文件----- 实现效果: ...

  2. Python 42 mysql用户管理 、pymysql模块

    一:mysql用户管理 什么是mysql用户管理 mysql是一个tcp服务器,应用于操作服务器上的文件数据,接收用户端发送的指令,接收指令时需要考虑到安全问题, ATM购物车中的用户认证和mysql ...

  3. python作业之用户管理程序

    数据库的格式化如下 分别为姓名|密码|电话号码|邮箱|用户类型 admin|admin123.|28812341026|admin@126.com|1root|admin123.|1344566348 ...

  4. 老男孩Day18作业:后台用户管理

    一.作业需求: 1.用户组的增删改查 2.用户增删该查 - 添加必须是对话框 - 删除必须是对话框 - 修改,必须显示默认值 3.比较好看的页面 二.博客地址:https://www.cnblogs. ...

  5. 【Python之路】特别篇--基于领域驱动模型架构设计的京东用户管理后台

    一.预备知识: 1.接口: - URL形式 - 数据类型 (Python中不存在) a.类中的方法可以写任意个,想要对类中的方法进行约束就可以使用接口: b.定义一个接口,接口中定义一个方法f1: c ...

  6. python作业学员管理系统(第十二周)

    作业需求: 用户角色,讲师\学员, 用户登陆后根据角色不同,能做的事情不同,分别如下 讲师视图 管理班级,可创建班级,根据学员qq号把学员加入班级 可创建指定班级的上课纪录,注意一节上课纪录对应多条学 ...

  7. Linux运维六:用户管理及用户权限设置

    Linux 系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这个账号的身份进入系统.用户的账号一方面可以帮助系统管理员对使用系统的用户进行 ...

  8. python作业ATM(第五周)

    作业需求: 额度 15000或自定义. 实现购物商城,买东西加入 购物车,调用信用卡接口结账. 可以提现,手续费5%. 支持多账户登录. 支持账户间转账. 记录每月日常消费流水. 提供还款接口. AT ...

  9. 【淘淘】Quartz作业存储与管理

    一.Quartz作业管理和存储方式简介: 作业一旦被调度,调度器需要记住并且跟踪作业和它们的执行次数.如果你的作业是30分钟后或每30秒调用,这不是很有用.事实上,作业执行需要非常准确和即时调用在被调 ...

随机推荐

  1. free如何知道释放内存长度:vs与glibc分配内存时编译器内部处理

    鉴于网上这个资料实在太少,将以前整理过却未完全的一篇文章贴出来,希望大牛指正vs下内存管理方式.可联系gaoshiqiang1987@163.com vs分配内存 vs没有源码,编译器在分配内存时,分 ...

  2. 【ZJOI2016】大♂森林

    题目描述 小Y家里有一个大森林,里面有 $n$ 棵树,编号从 $1$ 到 $n$ .一开始这些树都只是树苗,只有一个节点,标号为 $1$ .这些树都有一个特殊的节点,我们称之为生长节点,这些节点有生长 ...

  3. SQL SERVER 内存

    http://www.cnblogs.com/CareySon/archive/2012/08/16/HowSQLServerManageMemory.html

  4. SpringMVC中 Controller的 @ResponseBody注解分析

    需求分析:需要 利用    out 对象返回给财付通是否接收成功 .那么将需要如下代码: /** * 返回处理结果给财付通服务器. * @param msg: Success or fail. * @ ...

  5. EventBus3.0使用笔记.md

    事件总线这个其实没什么好说的,除了已经ondestroy的fragment或者activity不能接受外,只要定义了的都能接收消息 代码如下,需要注意的一点就是接收的监听事件必须用public修饰并且 ...

  6. 邁向IT專家成功之路的三十則鐵律 鐵律十三:IT人理財之道-知足

    身為一位專業的IT人士,工作上不僅要做到滿足興趣與專業熱忱,當然也要做到能夠滿足荷包.現代人賺錢不是問題,但花錢卻出了很大問題,親愛的IT朋友們,請不要將您辛苦賺來的錢花在想要的東西上,實際上需要的卻 ...

  7. Linux Distribution

    来自为知笔记(Wiz)

  8. 微信公众平台SDK for node

    实现了下面特性: 1.开启开发人员模式 2.解析微信请求參数 3.验证消息来源 4.被动回复文字消息 5.被动回复图文消息 6.获取access_token 7.创建自己定义菜单 地址:wechat ...

  9. 关于Adapter对数据库的查询、删除操作

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzIxMDYyMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  10. mac os PHP 访问MSSQL

    写在前: 项目的数据库是sql server,但是自己的系统是mac os.这样导致了需要一个烦人的系统环境搭建过程.目前要在mac 上的php环境中支持mssql环境访问,经过自己了解,有两种方式: ...