python day 12 2019/10/18 學習資料來自老男孩教育 [TOC] 1. 通過類來創建選課系統 看完視頻之後,自己折騰了一天才重新寫出來,算是理解了。 1.1 類庫models.py 2. 配置文件setting.py 3. administrator.py 4. student. ...
目錄
python day 12
2019/10/18
學習資料來自老男孩教育
1. 通過類來創建選課系統
看完視頻之後,自己折騰了一天才重新寫出來,算是理解了。
1.1 類庫models.py
import os, sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
import pickle
import time
from config import settings
import random
class Admin(object):
'''封裝管理員的相關功能
'''
def __init__(self):
self.username = None
self.passwd = None
self.create_time = None
def login(self, user, pwd):
if self.username == user and self.passwd == pwd:
return True
else:
return False
def register(self, user, pwd):
'''管理員註冊
:param user:
:param pwd:
:return:None
'''
self.username = user
self.passwd = pwd
self.create_time = time.strftime('%Y-%m-%d %H:%M:%S')
path = os.path.join(settings.BASE_ADMIN_DIR, user)
pickle.dump(self, open(path, 'wb'))
class Teacher(object):
'''
封裝老師相關信息
'''
def __init__(self, name, sex, age, create_admin, asset=0):
self.name = name
if sex in ('male', 'female'):
self.sex = sex
self.age = age
self.asset = asset
self.create_time = time.strftime('%Y-%m-%d %H:%M:%S')
self.create_admin = create_admin
def lose_asset(self, account):
'''
減少老師資產
:param account:
:return:
'''
self.asset -= account
return self.asset
def gain_asset(self, account):
'''
增加資產
:param account:
:return:
'''
self.asset += account
list2 = []
t_list = pickle.load(open(settings.TEACHER_DB_DIR,'rb'))
for t in t_list:
if t.name == self.name:
t = self
list2.append(t)
pickle.dump(list2,open(settings.TEACHER_DB_DIR,'wb'))
return self.asset
class Course(object):
'''
封裝課程的相關信息
'''
def __init__(self, name, cost, teacher_obj, create_admin):
self.course_name = name
self.cost = cost
self.teacher = teacher_obj
self.create_time = time.strftime('%Y-%m-%d %H:%M:%S')
self.create_admin = create_admin
def have_lesson(self):
'''
課程上課,自動給相關聯的任課老師增加資產
:return: 課程內容返回給上課者
'''
self.teacher.gain_asset(self.cost)
content = random.randrange(10, 100)
r = time.strftime('%Y-%m-%d %H:%M:%S')
temp = '課程:%s,老師:%s,內容:%s,時間:%s' % (self.course_name, self.teacher.name, content, r)
return temp
def absence(self):
'''
教學事故
:return:
'''
self.teacher.lose_asset(self.cost * 2)
class Student(object):
'''
封裝學生相關信息
'''
def __init__(self):
self.username = None
self.passwd = None
self.course_list = set()
self.study_dict = {}
self.total_fee = 0
def select_course(self, course_obj):
'''
學生選課
:param course:
:return:
'''
self.course_list.add(course_obj)
def study(self, course_obj):
'''
學生上課
:param course_obj:
:return:
'''
class_result = course_obj.have_lesson()
self.total_fee += course_obj.cost
if course_obj in self.study_dict.keys():
self.study_dict[course_obj].append(class_result)
else:
self.study_dict[course_obj] = [class_result, ]
def login(self, user, pwd):
if self.username == user and self.passwd == pwd:
return True
else:
return False
def register(self, user, pwd):
self.username = user
self.passwd = pwd
path = os.path.join(settings.STUDENTS_DB_DIR, user)
pickle.dump(self, open(path, 'wb'))
2. 配置文件setting.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
BASE_ADMIN_DIR= os.path.join(BASE_DIR,'db','admin')
TEACHER_DB_DIR = os.path.join(BASE_DIR,'db','teacher_list')
COURSE_DB_DIR= os.path.join(BASE_DIR,'db','course_list')
STUDENTS_DB_DIR= os.path.join(BASE_DIR,'db','students')
3. administrator.py
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from config import settings
from lib.models import *
import pickle
def create_teacher(admin_obj):
teacher_list = []
while True:
teacher_name = input("enter teacher's name(q to quit):>>> ").strip()
if teacher_name.lower() == 'q':
break
teacher_sex = input('teacher sex:>>>').strip()
teacher_age = int(input('teacher age:>>>').strip())
teacher_obj = Teacher(teacher_name, teacher_sex,teacher_age, admin_obj)
teacher_list.append(teacher_obj)
if os.path.exists(settings.TEACHER_DB_DIR):
list1 = pickle.load(open(settings.TEACHER_DB_DIR, 'rb'))
teacher_list.extend(list1)
pickle.dump(teacher_list, open(settings.TEACHER_DB_DIR, 'wb'))
def create_course(admin_obj):
course_list = []
teacher_list = pickle.load(open(settings.TEACHER_DB_DIR, 'rb'))
for k, i in enumerate(teacher_list, 1):
print(k, i.name, i.sex, i.age, i.create_admin, i.create_time)
while True:
course_name = input('course name(q to quit):>>>').strip()
if course_name.lower() == 'q':
break
cost = int(input('course cost:>>>').strip())
teacher_obj = teacher_list[int(input('teacher number:>>>').strip()) - 1]
course_obj = Course(course_name, cost, teacher_obj, admin_obj)
course_list.append(course_obj)
if os.path.exists(settings.COURSE_DB_DIR):
list2 = pickle.load(open(settings.COURSE_DB_DIR, 'rb'))
course_list.extend(list2)
pickle.dump(course_list, open(settings.COURSE_DB_DIR, 'wb'))
def check_tinfo(admin_obj):
if os.path.exists(settings.TEACHER_DB_DIR):
t_list = pickle.load(open(settings.TEACHER_DB_DIR, 'rb'))
inp = input('請輸入要查看的老師名字:>>>')
for i in range(len(t_list)):
if t_list[i].name == inp:
# sex, age, create_admin, asset = 0
print(t_list[i].name, t_list[i].age, t_list[i].create_admin.username, t_list[i].create_time, t_list[i].asset)
return True
def check_sinfo(admin_obj):
if os.path.exists(os.path.join(settings.STUDENTS_DB_DIR, 'xiao li')):
stu_obj = pickle.load(open(os.path.join(settings.STUDENTS_DB_DIR, 'xiao li'), 'rb'))
print(stu_obj.username, stu_obj.course_list, stu_obj.study_dict, stu_obj.total_fee)
def login(user, pwd):
path = os.path.join(settings.BASE_ADMIN_DIR, user)
if os.path.exists(path):
admin_obj = pickle.load(open(path, 'rb'))
if admin_obj.login(user, pwd):
print('登陸成功')
while True:
inp = input('1:創建老師;2:創建課程(q退出);3:查看老師信息;4:查看學生信息:\n>>>')
if inp == '1':
create_teacher(admin_obj)
elif inp == '2':
create_course(admin_obj)
elif inp == '3':
check_tinfo(admin_obj)
elif inp == '4':
check_sinfo(admin_obj)
else:
break
else:
return 1
else:
return 0
def register(user, pwd):
admin_obj = Admin()
admin_obj.register(user, pwd)
def main():
print('welcome to the course system')
inp = input('1:管理員登陸;2:管理員註冊(q退出)\n:>>>').strip()
user = input('enter username:>>>').strip()
pwd = input('enter password:>>>').strip()
if inp == '1':
ret = login(user, pwd)
if ret == '1':
print('密碼錯誤')
elif ret == '0':
print('沒有此用戶,請先註冊')
elif inp == '2':
register(user, pwd)
elif inp == 'q':
exit()
else:
print('輸入非法,請重新輸入')
if __name__ == '__main__':
main()
4. student.py
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from config import settings
from lib.models import *
import pickle
def check_cinfo(stu_obj):
course_list = stu_obj.course_list
print('%s %s %s %s ' % ('序號', '名稱', '費用', '老師'))
for k, v in enumerate(course_list, 1):
print(k, v.course_name, v.cost, v.teacher.name)
def select_course(stu_obj):
if os.path.exists(settings.COURSE_DB_DIR):
course_list = pickle.load(open(settings.COURSE_DB_DIR, 'rb'))
for k, v in enumerate(course_list, 1):
print(k, v.course_name, v.cost, v.teacher.name)
while True:
inp = (input('請輸入課程式列號進行選課:>>>').strip())
if inp == 'q':
break
stu_obj.select_course(course_list[int(inp) - 1])
path = os.path.join(settings.STUDENTS_DB_DIR, stu_obj.username)
pickle.dump(stu_obj, open(path, 'wb'))
def study(stu_obj):
if stu_obj.course_list:
for k, v in enumerate(list(stu_obj.course_list), 1):
print(k, v.course_name,v.teacher.name)
while True:
inp = (input('請輸入課程式列號進行上課:>>>').strip())
if inp == 'q':
break
stu_obj.study(list(stu_obj.course_list)[int(inp) - 1])
path = os.path.join(settings.STUDENTS_DB_DIR, stu_obj.username)
pickle.dump(stu_obj, open(path, 'wb'))
def login(user, pwd):
if os.path.exists(os.path.join(settings.STUDENTS_DB_DIR, user)):
stu_obj = pickle.load(open(os.path.join(settings.STUDENTS_DB_DIR, user), 'rb'))
if stu_obj.login(user, pwd):
print('登錄成功')
while True:
inp = input('1:選課;2:上課,3:查看課程信息:(q to quit);\n:>>>').strip()
if inp == '1':
select_course(stu_obj)
elif inp == '2':
study(stu_obj)
elif inp == '3':
check_cinfo(stu_obj)
else:
break
else:
return 1
else:
return 0
def register(user, pwd):
student = Student()
student.register(user, pwd)
def main():
inp = input('1:學生登錄;2:學生註冊(q退出)\n:>>>').strip()
user = input('enter name:>>>').strip()
pwd = input('enter password:>>>').strip()
if inp == '1':
login(user, pwd)
elif inp == '2':
register(user, pwd)
else:
exit()
if __name__ == '__main__':
main()