Student Management System
I believe that when you learn various languages, you will always write various management systems for practice. The management system mainly includes the addition, deletion, and modification of data. The principle is not difficult, and it is suitable as a small program for practicing.
The structure of the data
To save data, you need data structures, such as structures in c, lists, dictionaries, and classes in python are all commonly used data types
Here, I used a linked list as the data structure of the student data,
Namely Node class and Student_LinkList class, to realize the linked list
Data persistence
The data generated in the program is stored in the memory. Once the program exits, the data cannot be restored next time. Therefore, it is necessary to save the data in the memory to a file or database and store it. This process is Data persistence
This program uses the serialization methods dump() and load() provided by the python standard library pickle to achieve data persistence
Configuration file
Using configuration files can facilitate the use of different subclasses in the program,
This program uses configparser to parse configuration files
The configuration file of this program is named Student.ini
# Student.ini file
[ Student]
student = Student_LinkList
[ Persistence]
persistence = Persistence_Pickle
file = student.pik
Relationship between classes
Student #abstract class related to student data
±- Student_LinkList
Persistence #abstract classes related to persistence
±- Persistence_Pickle
MyConfigure #Classes related to configuration file reading
UI #Interaction-related parent class
±- Cmd_UI
Interface preview
Source code
'''
Student Management System Realized by Using Single Linked List
'''
import pickle
import abc
import configparser
classStudent(abc.ABC):'''
Abstract student class
'''
@ abc.abstractmethod
def add(self):'''
Increase student nodes
'''
pass
@ abc.abstractmethod
def ladd(self):'''
Add student nodes from the left
'''
pass
@ abc.abstractmethod
def delete(self,id_):'''
Delete a node based on the id value
'''
pass
@ abc.abstractmethod
def delete_name(self,name):'''
Delete a node based on name
'''
pass
@ abc.abstractmethod
def insert(self,idx,val):'''
Insert into the specified position
'''
pass
@ abc.abstractmethod
def show(self):'''
Show all student nodes
'''
pass
@ abc.abstractmethod
def search_id(self):'''
Query node based on id
'''
pass
@ abc.abstractmethod
def search_name(self):'''
Query node according to name
'''
@ abc.abstractmethod
def modity_id(self):'''
Find the node according to the id, then modify
'''
pass
classNode(object):'''
Student Link Node
'''
def __init__(self,id_: int,name: str,sex: str,age: int,score: int):
self.id = id_
self.name = name
self.sex = sex
self.age = age
self.score = score
self.next = None
def modity(self,id_,name,sex,age,score):'''
modify
'''
self.id = id_
self.name = name
self.sex = sex
self.age = age
self.score = score
def __str__(self):'''
For display output
'''
return f"[student:{self.id:^2}]-- name:{self.name:^10}sex:{self.sex:^10}age:{self.age:^10}score:{self.score:^10}"classStudent_LinkList(Student):'''
Student linked list
'''
def __init__(self):
self.head =Node(-1,'head','-1',-1,-1)
self.length =0
self.tail = self.head #The tail node is used for tail insertion
def add(self,id_,name,sex,age,score):'''
Add a student node,Tail plug
'''
# print('Current tail value',self.tail)
temp =Node(id_,name,sex,age,score)
self.tail.next = temp
self.tail = self.tail.next
self.length +=1print('[info]:Added successfully')
def ladd(self,id_,name,sex,age,score):'''
Add a student, head in
'''
temp =Node(id_,name,sex,age,score)
temp.next = self.head.next
self.head.next = temp
if self.tail == self.head:
self.tail = temp
self.length +=1print('[info]:Added successfully')
def delete(self,id_):'''
Delete a node based on the id value,Implement iteratively
'''
p = self.head
while p.next != None and p.next.id != id_:
p = p.next
if p.next == None:print('[error]:No id found')return-1else:
temp = p.next
p.next = temp.next
# If you delete the tail node, move tail
if temp.next == None:
self.tail = p
del temp
print('[info]:successfully deleted')
def delete_name(self,name):'''
According to the name to delete a node, use recursion to achieve
'''
def _func(node: Node,name: str):'''
recursive function
'''
# It’s the tail node, I haven’t found it yet
if node.next == None:print('[info]:Cannot find name')return False
elif node.next.name == name:
temp = node.next
node.next = temp.next
# If you delete the tail node, move tail
if temp.next == None:
self.tail = node
del temp
print('[info]:successfully deleted')return True
else:return_func(node.next,name)
t = self.head
return_func(t,name)
def insert(self,idx,id_,name,sex,age,score):'''
Insert data at the specified location
'''
if idx self.length or idx ==0:print(f'[error]:The index you entered is illegal(1-{self.length})')return0
p,cur = self.head,0while p != None and cur < idx-1:
p = p.next
if cur < idx-1:return-1else:
temp =Node(id_,name,sex,age,score)
temp.next = p.next
p.next = temp
return True
print('[info]:Inserted successfully')
def search_id(self,id_):'''
Query node based on id
'''
p = self.head
while p != None and p.id != id_:
p = p.next
if p == None:return-1else:return p
def search_name(self,name):'''
Query node according to name
'''
p = self.head
def _func(node: Node,name: str):'''
recursive function
'''
if node == None:return-1
elif node.name == name:return node
return_func(node.next,name)return_func(p,name)
def modity_id(self,id0,id_,name,sex,age,score):'''
Find the node according to the id, then modify
'''
node = self.search_id(id0)if node ==-1:print('[error]:The id cannot be found')return-1else:
node.modity(id_,name,sex,age,score)
def show(self):'''
Show all student nodes,Iteration
'''
print(f'\n{"-"*25}The following is the data in the system{"-"*25}')
temp =[]
p = self.head
while p != None:
temp.append(p)
p = p.next
return temp
classStudent_Array():'''
Use array to realize student data storage
'''
pass
classStudent_Queue():'''
Realize with queue
'''
pass
classStudent_Dict():'''
Realize with queue
'''
pass
classPersistence(abc.ABC):'''
Persistence of linked list data
'''
@ abc.abstractmethod
def save(self):'''
Save the object
'''
pass
@ abc.abstractmethod
def load(self):'''
Load object
'''
pass
classPersistence_Pickle(Persistence):'''
Use pickle to serialize
'''
def __init__(self,cls: Student,file_):
self.filename = file_
self.obj = None
self.cls = cls
def save(self):withopen(self.filename,'wb')as f:
pickle.dump(self.obj,f)
def load(self):try:withopen(self.filename,'rb')as f:
temp = pickle.load(f)
except:
temp =globals()[self.cls]()print('Return temp:',type(temp))
self.obj = temp
return temp
classPersistence_File(Persistence):'''
Use files to persist
'''
pass
classPersistence_Mysql(Persistence):'''
Use Mysql database to persist
'''
pass
classPersistence_Socket(Persistence):'''
Use remote socket persistence
'''
pass
classMyConfigure(object):'''
The class used to read the configuration file
'''
def __init__(self):
self.config = configparser.ConfigParser()
def save(self):'''
Save the configuration file
'''
withopen('Student.ini','w')as f:
self.config.write(f)
def load(self):'''
Load configuration file
'''
self.config.read('Student.ini')
def get_student_class(self):'''
Get which subclass of Student should use
'''
return self.config['Student']['student']
def get_persistence_class(self):'''
To obtain persistence, which class should be used,
If it is Pickle or file, there is file as the saved file name
'''
temp ={}
temp['persistence']= self.config['Persistence']['persistence']if'Persistence_Pickle'in temp['persistence']:
temp['file']= self.config['Persistence']['file']return temp
classUI(object):'''
Interface interaction
'''
def __init__(self):
self.config =MyConfigure()
self.config.load()
s_class = self.config.get_student_class()
p_class = self.config.get_persistence_class()
self.persistence =globals()[p_class['persistence']](s_class,p_class['file'])
self.student = self.persistence.load()print('Instantiated successfully:',self.student,self.persistence)
def save(self):'''
Save data
'''
self.persistence.save()
def quit(self):'''
drop out:先保存配置,然后drop out
'''
self.config.save()
self.save()
def _show(self):'''
Show all student nodes
'''
return self.student.show()
def _add(self,direction,*temp):'''
Increase student nodes,
direction 1 left add, 2 right add
'''
if direction ==1:
self.student.ladd(*temp)
elif direction ==2:
self.student.add(*temp)
def _delete(self,attribute: int,val: str):'''
Delete student node
attribute:According to which attribute needs to be deleted, 1.id or 2.name
'''
if attribute ==1:
self.student.delete(val)
elif attribute ==2:
self.student.delete_name(val)
def _insert(self,idx,*temp):'''
Insert the student node into the specified position
'''
self.student.insert(idx,*temp)
def _search(self,attribute,val):'''
Inquire
'''
if attribute ==1:return self.student.search_id(val)
elif attribute ==2:return self.student.search_name(val)
def _modity(self,attribute,id_,*temp):'''
modify
'''
if attribute ==1:
self.student.modity_id(id_,*temp)
elif attribute ==2:print('[info]:Because it didn’t happen, so do nothing')
pass #Modify according to name
classCmd_UI(UI):'''
Command line interactive interface
'''
def __init__(self):super(Cmd_UI,self).__init__()
def get_input_1_2(self,info: str):'''
Get input, return 1 or 2
info:Describe the information entered
'''
x = None
while x == None:
temp =input(info)if temp =='1':
x =1
elif temp =='2':
x =2else:print('You can only enter 1 or 2')return x
def get_input_arg(self):'''
Obtain user input to construct student node
'''
id_ =input('Please enter id')
name =input('Please type in your name')
sex =input('Please enter gender')
age =input('Please enter age')
score =input('Please enter grades')return(id_,name,sex,age,score)
def delete(self):'''
Delete node
'''
info ='According to which attribute do you want to delete the node:1.id 2.name'
attribute = self.get_input_1_2(info)
val =input('Enter the value you want to delete:')
self._delete(attribute,val)
def show(self):'''
display
'''
rel = self._show()for i in rel:print(i)
def add(self):'''
Increase student nodes
'''
info ='Where you want to insert:1.Left 2.right'
direction = self.get_input_1_2(info)
arg = self.get_input_arg()
self._add(direction,*arg)
def insert(self):'''
New student, inserted into the specified position
'''
idx =int(input('Enter the position to insert'))
temp = self.get_input_arg()
self._insert(idx,*temp)
def search(self):'''
Query student
'''
info ='Which attribute do you want to search for nodes:1.id 2.name'
attribute = self.get_input_1_2(info)
val =input('Enter the value you want to query:')print(self._search(attribute,val))
def modity(self):'''
Modify student information
'''
info ='Which attribute do you want to search for nodes:1.id 2.name'
attribute = self.get_input_1_2(info)
val_ =input('Enter the value to be queried:')
temp = self.get_input_arg()
self._modity(attribute,val_,*temp)
def main(self):'''
Main process
'''
info ='''
********************kalpa student management system**0.Display Data**1.Increase data**2.delete data**3.Query data**4.change the data**5.Save and exit********************'''
print(info)
a ='0'while a in['0','1','2','3','4','5']:if a =='0':
self.show()
elif a =='1':
self.add()
elif a =='2':
self.delete()
elif a =='3':
self.search()
elif a =='4':
self.modity()
elif a =='5':
self.quit()return
a =input(' ')if __name__ =="__main__":
ui =Cmd_UI()
ui.main()
Recommended Posts