Student Course Selection System
Programming Core: Encapsulating objects in objects
Directory structure:
1.administrator.py
Import Randomimport Osimport syssys.path.append (Os.path.dirname (os.path.dirname)) Import __file__ Picklefrom lib Import modelsfrom config import settingsfrom lib.models import * # all Import def login (user, PWD): "' Login, After successful login: Choose to create a teacher, create a course:p Aram User::p Aram pwd:: Return: Returns 1: for password error, return 2: User does not exist "if os.path.exists (Os.path.join ( Settings. Base_admin_dir, user): Admin_obj = pickle.load (open (Os.path.join (settings). Base_admin_dir, user), ' RB ') # Get object if Admin_obj.login (user, PWD): print (' login successful ') while True : sel = input (' 1. Create a teacher; 2. Create a course ') if sel = = ' 1 ': Create_teacher (admin_obj) Elif sel = = ' 2 ': Create_course (admin_obj) else:break Else:return 1 else:return 0def Register (user, PWD): "Registered:p Aram User::p Aram PW D:: Return: "' Admin_obj = models." Admin () Admin_obj.regIster (User, PWD) def create_teacher (admin_obj): Teacher_list = [] While true:teacher_name = input (' Please enter the teacher's name (q Exit): ' If teacher_name = = ' Q ': Break teacher_age = input (' Please enter the teacher's age: ') obj = models. Teacher (Teacher_name, Teacher_age, admin_obj) teacher_list.append (obj) # Plug the Teacher object into the list, and then serialize if os.path.exists (sett Ings. Teacher_de_dir): Exists_list = pickle.load (open (settings. Teacher_de_dir, ' RB ')) Teacher_list.extend (exists_list) # Adds a list that already exists in bulk to the current list pickle.dump (teacher_list, open (sett Ings. Teacher_de_dir, ' WB ') def Create_course (admin_obj): Teacher_list = pickle.load (open (settings. Teacher_db_dir, ' RB '))) for NUM, item in enumerate (Teacher_list, 1): Print (num, item.name, Item.age, Item.create_ Time, item.create_admin.username) course_list = [] While true:name = input (' Please enter course name (Q exit) ') if name = = ' Q ': Break cost = input (' Input lesson fee ') num = input (' Select teacher (serial number) ') obj = models. CoUrse (name, cost, Teacher_list[int (num)-1], admin_obj) course_list.append (obj) if os.path.exists (settings. Course_db_dir): Exists_list = pickle.load (open (settings. Course_db_dir, ' RB ')) Course_list.extend (exists_list) # Adds a list that already exists in bulk to the current list pickle.dump (course_list, open (setting S.course_db_dir, ' WB ') def Main (): INP = input (' 1. Admin login 2. admin registration; \ n >>> ') user = input (' Please enter user name: ') pwd = Input (' Please enter password: ') if InP = = ' 1 ': ret = login (user, pwd) if ret = = 1:print (' password error ') Elif R ET = = 0:print (' User not present ') elif INP = = ' 2 ': Register (user, pwd) if __name__ = = ' __main__ ': Main ()
2.student.py
Import Sysimport Osimport picklesys.path.append (Os.path.dirname (Os.path.dirname)) from Lib Import Modelsfrom Config import settingsfrom lib.models import teacherfrom lib.models import adminfrom lib.models import Coursede F Course_info (Student_obj): For item in Student_obj.course_list:print (Item.course_name, item.teacher.name) def s Elect_course (student_obj): Course_list = pickle.load (open (settings. Course_db_dir, ' RB '))) for NUM, item in enumerate (Course_list, 1): Print (num, item.course_name, Item.cost, item.t Eacher.name) While true:num = input (' Please select course: (Q exit) ') if num = = ' Q ': Break Select_course_ obj = course_list[int (num)-1] If select_course_obj in student_obj.course_list: ' ' Same as skip "Pass Student_obj.course_list.append (select_course_obj) pickle.dump (student_obj, open (OS). Path.join (settings. Base_students_dir, Student_obj.username), ' WB ') def login (user, PWD): If Os.path.exists (Os.path.join (settings. Base_students_dir, user): Student_obj = pickle.load (open (Os.path.join (settings). Base_students_dir, user), ' RB ')) if Student_obj.login (user, PWD): while TRUE:INP = input (' 1 Elective Course 2 Class 3 View course Information \n>>> ') if InP = = ' 1 ': Select_course (student_obj) elif INP = = ' 3 ': Course_info (student_obj) Else:break else: Print (' password error ') else:print (' User not present ') def register (user, pwd): obj = models. Student () obj.register (user, PWD) def main (): INP = input (' 1. Login; 2. Register \ n >>> ') user = input (' User name: ') pwd = input (' Password: ') if InP = = ' 1 ': Login (user, pwd) elif INP = = ' 2 ': Register (user, pwd) if __name__ = = ' __m Ain__ ': Main ()
3.settings.py
# configuration file Open closed principle, do not change the source code, changing the configuration file import Osbase_dir = Os.path.dirname (Os.path.dirname (__file__)) Base_admin_dir = Os.path.join (Base_dir, ' db ', ' admin ') Base_students_dir = Os.path.join (Base_dir, ' db ', ' STUDENTS ') Teacher_db_dir = Os.path.join (Base_dir, ' db ', ' teacher_list ') Course_db_dir = Os.path.join (Base_dir, ' db ', ' course_list ') Log_path = '/ home/alex/' #日志路径
4.models.py
Import randomimport osimport timeimport picklefrom config import settingsclass Teacher: "The information about the package teacher" Def __init__ (self, name, age, admin): Self.name = name # Teacher's name self.age = Age # Teacher's ages self.__assets = 0 # Teacher's Asset Self.create_time = Time.strftime ('%y-%m-%d%h:%m:%s ') # creation time Self.create_admin = admin # Create User's admin Member Def gain (self, cost): "Earn money:p Aram Cost:: Return:" ' Pass def Decrease (self, cost): ' ' Spend:p aram costs:: ' return: ' ' Passclass admin:def __init__ ( Self): Self.username = None Self.password = None def login (self, user, pwd): ' Admin Login :p Aram User::p Aram pwd:: return: "If self.username = = User and Self.password = = pwd: Return True Else:return False def register (self, user, pwd): "' admin registered :p Aram User::p ARam pwd:: return: ' self.username = user Self.password = pwd Path = os.path.join (sett Ings. Base_admin_dir, Self.username) pickle.dump (self, open [path, ' XB ')] "serializes the object into a file so that it can be pulled out later to be written in XB Read, if the file exists, then the error "Class Course: ' Course Information ' ' Def __init__ (self, course_name, cost, teacher_obj, admin): Self.course_name = Course_name Self.cost = Cost Self.teacher = Teacher_obj Self.create_time = Time.strftime ('%y-%m-%d%h:%m:%s ') self.create_admin = Adminclass Student: ' Student-related information ' Def __init__ (self): Self.username = None Self.password = None self.course_list = [] self.studey_dict = {} def login (self, user, pwd): if self.username = = User and Self.password = = Pwd:return True els E:return False def register (self, user, pwd): self.username = user Self.password = pwd Path = Os.path.joiN (Settings. Base_students_dir, Self.username) pickle.dump (self, open (path, ' XB '))
3.python Small Project: Students ' Course selection system