Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python2
- # -*- coding: utf-8 -*-
- import sys
- import cPickle as pickle
- class StudentsBase:
- def __init__(self):
- self.students = []
- def add_student(self):
- name = str(raw_input("Enter new student name: "))
- scores_mini = []
- scores_homework = []
- scores_test = []
- scores_atboard = []
- key_index = len(self.students) + 1
- newStudent = Student(key_index,name,scores_mini,scores_homework,scores_test,scores_atboard)
- self.students.append(newStudent)
- def __repr__(self):
- string = "\n"
- for student in self.students:
- string += str(student.key_index) + ": "
- string += student.name + "\n"
- return string
- def delete_last_student(self):
- self.students.pop()
- def edit_student(self,key):
- state = -1
- for s in self.students:
- if s.key_index == key:
- state,s = s.edit()
- if state == 1:
- break
- if state == -1:
- print "No such student!"
- def save(self,filename):
- with open(filename,'wb') as dest:
- pickle.dump(self,dest,-1)
- def load(self,filename):
- with open(filename,'rb') as src:
- self = pickle.load(src)
- print "Database loaded from file:" + filename
- return self
- class Student:
- def __init__(self,key_index,name,scores_mini,scores_homework,scores_test,scores_atboard):
- self.key_index = key_index
- self.name = name
- self.scores_mini = scores_mini
- self.scores_homework = scores_homework
- self.scores_test = scores_test
- self.scores_atboard = scores_atboard
- def edit(self):
- no_changes = self
- print "Editing student: " + self.name
- MESSAGE = """What would you like to edit?
- 1 - name
- 2 - scores of mini tests
- 3 - scores of homework
- 4 - scores of tests
- 5 - scores of boards
- s - accept and save changes
- q - exit without saving changes\n"""
- while True:
- c = raw_input(MESSAGE)
- if c == '1':
- self.name = raw_input("Enter new student name: ")
- continue
- elif c == '2':
- print "Current scores of mini-tests: ",self.scores_mini
- editlist(self.scores_mini)
- continue
- elif c == '3':
- print "Current scores of homework: ",self.scores_homework
- editlist(self.scores_homework)
- continue
- elif c == '4':
- print "Current scores of tests: ",self.scores_test
- editlist(self.scores_test)
- continue
- elif c == '5':
- print "Current scores for boards: ",self.scores_atboard
- editlist(self.scores_atboard)
- continue
- elif c == 'q':
- return 1,no_changes
- elif c == 's':
- break
- else:
- print "Invalid option. Try again."
- continue
- return 1,self
- def editlist(list_):
- MESSAGE = """Select an option:
- a - add score to list
- d - delete last score from list
- p - print list
- q - return to editing\n"""
- while True:
- c = raw_input(MESSAGE)
- if c == 'a':
- list_.append(int(raw_input("Enter score to add: ")))
- continue
- if c == 'd':
- try:
- print "Deleted",list_.pop(),"from scores."
- except IndexError:
- print "Nothing to delete there!"
- continue
- if c == 'p':
- print list_
- continue
- if c == 'q':
- return list_
- def main():
- Base = StudentsBase()
- if len(sys.argv) >=3:
- print "You listed too much input files. Working only with first (" + sys.argv[1] + ")"
- Base = Base.load(sys.argv[1])
- elif len(sys.argv) == 1:
- print "No database loaded!"
- else:
- Base = Base.load(sys.argv[1])
- MESSAGE = """What would you like to do?
- a - add student to database
- e - edit student
- d - delete last student
- l - load database
- p - print database
- s - save database
- q - quit\n"""
- while True:
- c = raw_input(MESSAGE)
- if c == 'a':
- Base.add_student()
- continue
- if c == 'e':
- key = int(raw_input("Enter number of student to edit:"))
- Base.edit_student(key)
- continue
- if c == 'd':
- try:
- Base.delete_last_student()
- except IndexError:
- print "No students to delete from!"
- continue
- if c == 'l':
- filename = raw_input("Enter file to read:")
- Base = Base.load(filename)
- continue
- if c == 'p':
- print Base
- continue
- if c == 's':
- filename = raw_input("Enter file to write:")
- Base.save(filename)
- continue
- if c == 'q':
- sys.exit()
- else:
- print("Invalid option. Try arain.")
- continue
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement