1. 学生管理系统
学生管理系统的数据结构设置:
一个学生信息对应一个字典
整个系统的所有学生:所有学生对应一个列表,列表中的元素全是字典
整个系统:一个字典, 字典中有一个key(all_student)
对应的值是所有学生;
这个字典需要做数据持久化,将数据存储到json文件中, 文件名就是当前登录的账号名
{
'all_student': [
{'name': '张三', 'age': 89, 'tel': '237837293'},
{'name': '张三', 'age': 89, 'tel': '237837293'},
{'name': '张三', 'age': 89, 'tel': '237837293'},
{'name': '张三', 'age': 89, 'tel': '237837293'}
],
'num': 4
}
file_manage:
读取JSON文件
import json
base_dir = './files/'
def read_text(file_name):
try:
with open(base_dir + file_name, 'r', encoding='utf8') as file:
msg = file.read()
return msg
except FileNotFoundError:
print('读取文件失败,请检查路径')
return ''
def read_json(json_name, non_data):
try:
with open(base_dir + json_name, 'r', encoding='utf8') as file:
msg = file.read()
return json.loads(msg)
except FileNotFoundError:
return non_data
def write_json(json_name, data):
with open(base_dir + json_name, 'w', encoding='utf8') as file:
json.dump(data, file)
index:
登录与注册
import file_manager
import model
import student_manager
import tools
USERS_FILE = 'teachers.json'
def login():
all_user = file_manager.read_json(USERS_FILE, {})
user_name = input('请输入账号:')
if user_name not in all_user:
print('登录失败!该账号没有注册!')
else:
password = input('请输入密码:')
if tools.encrypt_password(password) == all_user[user_name]:
# print('登录成功!')
student_manager.USER_NAME = user_name
student_manager.show_system()
def register():
while True:
user_name = input('请输入账号(3~6位):')
if 3 <= len(user_name) <= 6:
break
print('账号不符合要求,请重新输入!')
all_user = file_manager.read_json(USERS_FILE, {})
if user_name in all_user:
print("注册失败!该账号已经注册过!")
return
while True:
password = input('请输入密码(6~12位):')
if 6 <= len(password) <= 12:
break
print('密码不符合要求,请重新输入!')
s = model.Teacher(user_name, password)
all_user[s.name] = s.password
file_manager.write_json(USERS_FILE, all_user)
def start():
content = file_manager.read_text('welcome.txt')
while True:
print(content)
value = input('请选择(1-3):')
if value == '1':
login()
elif value == '2':
register()
elif value == '3':
print('退出成功!')
exit()
else:
print('输入有误!')
if __name__ == '__main__':
start()
model:
import tools
class Teacher(object):
def __init__(self, name, password):
self.name = name
self.password = tools.encrypt_password(password)
class Student(object):
def __init__(self, s_id, name, age, gender, tel):
self.s_id = s_id
self.name = name
self.age = age
self.gender = gender
self.tel = tel
student_manage:
删除学生;查找学生;增加学生;获取学生信息
import file_manager
import model
USER_NAME = ''
ALL_STUDENT = 'all_student'
COUNT = 'count'
def del_student():
system_data = file_manager.read_json(USER_NAME + '.json', {})
all_student = system_data.get(ALL_STUDENT, [])
if not all_student:
print('该账号没有可管理的学生!请先添加学生!')
return
print('1.按姓名删\n2.按学号删\n其他:返回')
del_studens = []
choice = input('请选择:')
key = value = ''
if choice == '1':
key = 'name'
value = input('请输入要删除的学生名字:')
elif choice == '2':
key = 's_id'
value = input('请输入要删除的学生id:')
else:
return
# 找到名字和删除学生名字相同的学生
for stu in all_student:
if stu[key] == value:
del_studens.append(stu)
if not del_studens:
print('没有该学生!')
return
length = len(del_studens)
for index, s in enumerate(del_studens):
print(index, get_stu_message(s))
try:
choice = int(input('请输入需要删除的学生的标号(0~%d),q-返回:' % (length - 1)))
except ValueError:
return
if not 0 <= choice <= length - 1:
return
# 删除对应的学生
all_student.remove(del_studens[choice])
system_data[ALL_STUDENT] = all_student
file_manager.write_json(USER_NAME + '.json', system_data)
print('删除成功!')
def add_student():
system_data = file_manager.read_json(USER_NAME + '.json', {})
all_student = system_data.get(ALL_STUDENT, [])
while True:
# 1.输入学生信息
name = input('请输入姓名:')
age = input('请输入年龄:')
gender = input('请输入性别:')
tel = input('请输入电话:')
# 创建学号
count = system_data.get(COUNT, 0)
count += 1
s_id = 'stu' + str(count).zfill(4)
# 2.创建学生对象
s = model.Student(s_id, name, age, gender, tel)
# 4. 更新数据
all_student.append(s.__dict__)
system_data[ALL_STUDENT] = all_student
system_data[COUNT] = count
# 5. 更新文件
file_manager.write_json(USER_NAME + '.json', system_data)
print('添加成功!')
# 6.给出选择
value = input('1.继续\n2.返回\n请选择(1-2):')
if value != '1':
break
def get_stu_message(stu):
return '学号:{s_id}, 姓名:{name},性别:{gender},年龄:{age}, ☎:{tel}'.format(**stu)
def find_student():
all_student = file_manager.read_json(USER_NAME + '.json', {}).get(ALL_STUDENT, [])
if not all_student:
print('该账号没有可管理的学生!请先添加学生!')
return
print('1.查看所有学生\n2.根据姓名查找\n3.根据学号查找\n其他: 返回')
value = input('请选择:')
if value == '1':
# 查看所有学生
for stu in all_student:
print(get_stu_message(stu))
elif value == '2':
# 根据姓名查找
name = input('请输入学生名字:')
flag = True # 是否没有这个名字的学生
for stu in all_student:
if stu['name'] == name:
print(get_stu_message(stu))
flag = False
if flag:
print('没有该学生!')
elif value == '3':
# 根据学号查找
study_id = input('请输入学生学号:')
for stu in all_student:
if stu['s_id'] == study_id:
print(get_stu_message(stu))
break
else:
print('没有该学生!')
else:
return
def show_system():
content = file_manager.read_text('students_page.txt') % USER_NAME
while True:
print(content)
value = input('请选择(1-5):')
if value == '1':
add_student()
elif value == '2':
find_student()
elif value == '3':
print('修改学生信息')
elif value == '4':
del_student()
elif value == '5':
return
else:
print('输入有误!')
tools:
密码加密
import hashlib
def encrypt_password(pwd):
sha = hashlib.sha256()
sha.update(pwd.encode('utf8'))
return sha.hexdigest()
2. hashlib模块
import hashlib
import hmac
# 这两个模块都是用来进行数据加密
# hashlib模块里主要支持两个算法 md5 和 sha 加密
# 加密方式: 单向加密:只有加密的过程,不能解密md5/sha 对称加密 非对称加密rsa
# 需要将要加密的内容转换成为二进制
x = hashlib.md5('y239ym23r9b!'.encode('utf8')) # 生成一个md5对象
# x.update('y239ym23r9b!'.encode('utf8'))
print(x.hexdigest()) # 900150983cd24fb0d6963f7d28e17f72
# 'abc' ==> 900150983cd24fb0d6963f7d28e17f72
h1 = hashlib.sha1('123456'.encode())
print(h1.hexdigest()) # 7c4a8d09ca3762af61e59520943dc26494f8941b
h2 = hashlib.sha224('123456'.encode()) # 224位 一个十六进制占4位
print(h2.hexdigest()) # f8cdb04495ded47615258f9dc6a3f4707fd2405434fefc3cbf4ef4e6
h3 = hashlib.sha256('123456'.encode())
print(h3.hexdigest()) # 8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92
h4 = hashlib.sha384('123456'.encode())
print(
h4.hexdigest()) # 0a989ebc4a77b56a6e2bb7b19d995d185ce44090c13e2984b7ecc6d446d4b61ea9991b76a4c2f04b1b4d244841449454
(日常美图时间)