A simple python Course Selection System and python Course Selection System

Source: Internet
Author: User

A simple python Course Selection System and python Course Selection System

The following describes your own python program. The main knowledge points are sys, OS, json, and pickle Module Applications, usage of python packages, and usage of classes.

The following is my program directory:

          

Bin stores some execution files course. py. The content of this file is as follows:

Import sys, OS # main directory BASE_DIR = OS. path. dirname (OS. path. dirname (OS. path. abspath (_ file _) print (BASE_DIR) # Add the environment variable sys. path. append (BASE_DIR) from core import mainif _ name _ = '_ main _': a = main. run (). interactive ()

This file is an application to the OS and sys modules;

BASE_DIR = OS. path. dirname (OS. path. dirname (OS. path. abspath (_ file __)))

Here OS. path. abspath (_ file _) is used to view the location of the current file. For example:

D: \ python project \ course Selection System \ course_system \ bin \ course. py

The OS. path. abspath is the directory of the upper level, for example, print (BASE_DIR)

D: \ python project \ Course Selection System \ course_system

Sys. path. append (BASE_DIR) contains the address referenced by the module. We can understand that this module is set to an environment variable.
From core import main import the main file in the core package

Here is the udi. py file in the core package
import hashlibimport timedef create_md():    m = hashlib.md5()    m.update(bytes(str(time.time()),encoding="utf-8"))    return m.hexdigest()

In python, hashlib provides common digest algorithms, such as MD5 and SHA1. It converts data of any length into a fixed-length data string (usually expressed in hexadecimal strings) through a function ). To generate a random, non-repeating uid for students.

 

Here is the content of the main. py file in the core package.

Import sys, OS import json # Here we will talk about the json module, which is also used in java, so some data can also be used in java. Import pickle from bin import coursefrom core import uid # database file path db_DIR = course. BASE_DIR + r "\ db" db_school = db_DIR + r "\ school" db_teacher = db_DIR + r "\ teacher" db_student = db_DIR + r "\ student" db_classes = db_DIR + r" \ classes "db_course = db_DIR + r" \ course "db_admin = db_DIR + r" \ admin "db_class_record = db_DIR + r" \ class_record "db_class_grade = db_DIR + r" \ class_grade "# basic class, it mainly includes file read and write operations class Baseclas S (object): # all new classes are available in python3, but we recommend that you write them like this. There are classic classes and new classes in python2. For example, class Baseclass: this is a classic class in python, while class Baseclass (object): This is a new class. I will explain their differences in the next blog. Def _ init _ (self): pass def save (self, type, dict): filename = uid. create_md () dict ['uid'] = filename file_path = "% s \ % s" % (db_DIR, type) AB _file = "% s \ % s" % (file_path, filename) if OS. path. isdir (file_path): with open (AB _file, "wb") as f: f. write (pickle. dumps (dict) if True: print ("-------", type, "created successfully", "-------") for key in dict: print (key, ": \ t ", dict [key]) def seek_list (self, type, list): filename = uid. create_md () file_path = "% s \ % s" % (db_DIR, type) AB _file = "% s \ % s" % (file_path, filename) if OS. path. isdir (file_path): with open (AB _file, "wb") as f: f. write (pickle. dumps (list) if True: print ("-------", type, "created successfully", "-------") for I in list: for key in I: print (key, I [key]) print ("\ n") return True def open (self, type): all_data = [] db_path = "% s \ % s" % (db_DIR, type) for I in OS. listdir (db_path): if OS. path. isfile (OS. path. join (db_path, I): db_file = OS. path. join (db_path, I) with open (db_file, "rb") as f: file_dict = pickle. load (f) all_data.append (file_dict) return all_data # admin class Admin (Baseclass): def _ init _ (self): Baseclass. _ init _ (self) def create_school (self): school_dict = {} school_name = input (" name:") school_address = input ("Address :") s1 = School (school_name, school_address) school_dict ["School name"] = s1.school _ name school_dict ["address"] = s1.school _ address Baseclass. save (self, "school", school_dict) def create_teacher (self): teacher_dict ={} teacher_name = input ("instructor name:") teacher_salary = input ("instructor salary :") teacher_school = input ("School:") t1 = Teacher (teacher_name, teacher_salary, teacher_school) principal ["name"] = t1.teacher _ name teacher_dict ["salary"] = t1.teacher _ salary teacher_dict ["school"] = t1.teacher _ school print (teacher_dict) Baseclass. save (self, "teacher", teacher_dict) def create_student (self): student_dict ={} student_name = input ("Student name:") student_sex = input ("Student gender :") student_school = input ("School:") student_classes = input ("Student class:") st1 = Student (student_name, student_sex, student_school, student_classes) remark ["name"] = st1.student _ name student_dict ["gender"] = st1.student _ sex student_dict ["school"] = st1.student _ school student_dict ["class"] = st1.student _ classes baseclass. save (self, "student", student_dict) def create_course (self): course_dict ={} course_name = input ("Course name:") course_period = input ("cycle :") course_prices = input ("Price:") c1 = Course (course_name, course_period, course_prices) course_dict ["Course name"] = c1.course _ name course_dict ["period"] = c1.course _ period course_dict ["price"] = course_prices Baseclass. save (self, "course", course_dict) def create_classes (self): classes_dict = {} classes_name = input ("Class Name:") classes_teachter = input ("responsible INSTRUCTOR :") classes_course = input ("Course:") cs1 = Classes (classes_name, classes_teachter, classes_course) classes_dict ["Class name"] = cs1.classes _ name classes_dict ["responsible instructor"] = cs1.classes _ teacher classes_dict ["course"] = cs1.classes _ course Baseclass. save (self, "classes", classes_dict) # School class School (Baseclass): def _ init _ (self, school_name, school_address): Baseclass. _ init _ (self) self. school_name = school_name self. school_address = school_address # teacher class Teacher (Baseclass): def _ init _ (self, teacher_name, teacher_salary, teacher_school): Baseclass. _ init _ (self) self. teacher_name = teacher_name self. teacher_salary = teacher_salary self. teacher_school = teacher_school def create_class_record (self): class_record = [] student_school = input ("select school:") student_classes = input ("select class:") student_times = input ("course: ") student_list = Baseclass. open (self, "student") for I in student_list: if I ["school"] = student_school and I ["class"] = student_classes: student_name = I ["name"] student_status = input ("% s class situation:" % student_name) I ["course situation"] = student_status I ["course times"] = student_times class_record.append (I) Baseclass. seek_list (self, "class_record", class_record) def create_class_grade (self): class_grade = [] student_school = input ("select school:") student_classes = input ("select class :") student_times = input ("Course:") student_list = Baseclass. open (self, "student") for I in student_list: if I ["school"] = student_school and I ["class"] = student_classes: student_name = I ["name"] student_grade = input ("% s score:" % student_name) I ["score"] = student_grade I ["course times"] = student_times class_grade.append (I) Baseclass. seek_list (self, "class_grade", class_grade) def teacher_view_grade (self): grade_list = [] student_school = input (" name:") student_class = input ("class :") student_times = input ("Course:") class_grade_list = Baseclass. open (self, "class_grade") for I in class_grade_list: for j in I: if j ["school"] = student_school and j ["class"] = student_class and j ["class"] = student_times: grade_list.append (j) for I in grade_list: for key in I: print (key, I [key]) print ("\ n") def tacher_view_record (self): record_list = [] student_school = input (" name :") student_class = input ("class:") student_times = input ("class:") class_record_list = Baseclass. open (self, "class_record") for I in class_record_list: for j in I: if j ["school"] = student_school and j ["class"] = student_class and j ["Course"] = student_times: record_list.append (j) for I in record_list: for key in I: print (key, I [key]) print ("\ n") # classes class Course (Baseclass): def _ init _ (self, course_name, course_period, course_prices): Baseclass. _ init _ (self) self. course_name = course_name self. course_period = course_period self. course_prices = course_prices # student class Student (Baseclass): def _ init _ (self, student_name, student_sex, student_school, student_classes): Baseclass. _ init _ (self) self. student_name = student_name self. student_sex = student_sex self. student_school = student_school self. student_classes = student_classes def student_registered (self): student_dict = {} print ("welcome to the student registration system") student_name = input ("Registration Name:") student_sex = input: ") student_school = input (" School: ") student_class = input (" class: ") st1 = Student (student_name, student_sex, student_school, student_class) remark ["name"] = st1.student _ name student_dict ["gender"] = st1.student _ sex student_dict ["school"] = st1.student _ school student_dict ["class"] = st1.student _ classes baseclass. save (self, "student", student_dict) def student_pay_fees (self): pass def student_view_grade (self): student_school = input ("school name:") student_class = input ("class: ") student_times = input (" Course: ") student_name = input (" name: ") class_grade_list = Baseclass. open (self, "class_grade") for I in class_grade_list: for j in I: if j ["school"] = student_school and j ["class"] = student_class and j ["Course"] = student_times \ and j ["name"] = student_name: for key in j: print (key, j [key]) print ("\ n") def student_view_record (self): student_school = input ("school name :") student_class = input ("class:") student_times = input ("Course:") student_name = input ("name:") class_record_list = Baseclass. open (self, "class_record") for I in class_record_list: for j in I: if j ["school"] = student_school and j ["class"] = student_class and j ["Course"] = student_times \ and j ["name"] = student_name: for key in j: print (key, j [key]) print ("\ n") # course class Classes (Baseclass): def _ init _ (self, classes_name, classes_teachter, classes_course): Baseclass. _ init _ (self) self. classes_name = classes_name self. classes_teacher = classes_teachter self. classes_course = classes_course # administrator view inherited from Admin class Admin_view (Admin): def _ init _ (self): Admin. _ init _ (self) def auth (self, username, password): admin_file = "% s/% s. json "% (db_admin, username) if OS. path. isfile (admin_file): with open (admin_file, 'R') as f: admin_data = json. load (f) if admin_data ["name"] = username and admin_data ["password"] = password: return True else: print ("incorrect user name or password ") def login (self): menu = u''' ------- welcome to management view --------- \ 033 [32; 1 m 1. campus Management 2. instructor Management 3. student Management 4. course Management 5. returns \ 033 [0m''' menu_dic = {'1': Admin_view.school_manager, '2': Comment, '3': Admin_view.student_manager, '4': Admin_view.course_manager, '5 ': "logout",} username = input ("input username :"). strip () password = input ("input password :"). strip () auth = Admin_view.auth (self, username, password) if auth: exit_flag = False while not exit_flag: print (menu) option = input ("select :"). strip () if option in menu_dic: if int (option) = 5: exit_flag = True else: print (menu_dic [option]) menu_dic [option] (self) else: print ("\ 033 [31; 1 m input error, re-input \ 033 [0 m") def school_manager (self): exit_flag = False while not exit_flag: print ("" ------- welcome to campus management --------- \ 033 [32; 1m1. create campus 2. create a Class 3. returns \ 033 [0 m ") option = input (" select :"). strip () if int (option) = 1: Admin. create_school (self) elif int (option) = 2: Admin. create_classes (self) else: exit_flag = True def teacher_manager (self): exit_flag = False while not exit_flag: print ("" ------- welcome to instructor management --------- \ 033 [32; 1 m 1. create a lecturer 2 .... 3. returns \ 033 [0 m ") option = input (" select :"). strip () if int (option) = 1: Admin. create_teacher (self) elif int (option) = 2: print ("extended") else: exit_flag = True def student_manager (self): exit_flag = False while not exit_flag: print ("" ------- welcome to student management --------- \ 033 [32; 1 m 1. create STUDENT 2 .... 3. returns \ 033 [0 m ") option = input (" select :"). strip () if int (option) = 1: Admin. create_student (self) elif int (option) = 2: print ("extended") else: exit_flag = True def course_manager (self): exit_flag = False while not exit_flag: print ("" ------- welcome to course management --------- \ 033 [32; 1 m 1. create course 2 .... 3. returns \ 033 [0 m ") option = input (" select :"). strip () if int (option) = 1: Admin. create_course (self) elif int (option) = 2: print ("") else: exit_flag = True # instructor View class, inheriting Teacher class Teacher_view (Teacher ,): def _ init _ (self, teacher_name, teacher_salary, teacher_school): Teacher. _ init _ (self, teacher_name, teacher_salary, teacher_school) def login (self): menu = u''' ------- welcome to the instructor view --------- \ 033 [32; 1 m 1. create course records 2. create student score 3. view course records 4. view score 5. returns \ 033 [0m''' menu_dic = {'1': Teacher. create_class_record, '2': Teacher. create_class_grade, '3': Teacher. tacher_view_record, '4': Teacher. teacher_view_grade, '5': "logout",} if True: exit_flag = False while not exit_flag: print (menu) option = input ("select :"). strip () if option in menu_dic: if int (option) = 5: exit_flag = True else: print (menu_dic [option]) menu_dic [option] (self) else: print ("\ 033 [31; 1 m input error, re-enter \ 033 [0 m") # Student View class, inheriting Student class Student_view (Student ): def _ init _ (self, student_name, student_sex, student_school, student_classes): Student. _ init _ (self, student_name, student_sex, student_school, student_classes) def login (self): menu = u''' ------- welcome to the student management view --------- \ 033 [32; 1 m 1. register 2. tuition fee 3. view course records 4. view job score 5. returns \ 033 [0m''' menu_dic = {'1': Student. student_registered, '2': Student. student_pay_fees, '3': Student. student_view_record, '4': Student. student_view_grade, '5': "logout",} if True: exit_flag = False while not exit_flag: print (menu) option = input ("select :"). strip () if option in menu_dic: if int (option) = 5: exit_flag = True else: menu_dic [option] (self) else: print ("\ 033 [31; 1 MB input error. re-enter \ 033 [0 m ") # program interaction class Run (object): def _ init _ (self): pass def interactive (self ): menu = u''' ------- welcome to the course selection system --------- \ 033 [32; 1 m 1. student View 2. instructor view 3. management View 4. exit \ 033 [0m''' menu_dic = {'1': Student_view, '2': Teacher_view, '3': Admin_view, # '4': logout ,} exit_flag = False while not exit_flag: print (menu) option_view = input ("select View :"). strip () if option_view in menu_dic: if int (option_view) = 4: exit_flag = True else: menu_dic [option_view]. login (self) else: print ("\ 033 [31; 1 m input error, re-enter \ 033 [0 m ")

When writing a program, it is best to write a framework first, and then enter something in it.

This is the effect of Program Execution

Not too many demos

After all, I just learned python, and I have also used the code from many netizens. I am not very grateful to these people.

 



 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.