Advertisement
Lucius_V

Student progress counter

Mar 27th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.18 KB | None | 0 0
  1. #!/usr/bin/env python2
  2. # -*- coding: utf-8 -*-
  3.  
  4. import sys
  5. import cPickle as pickle
  6.  
  7. class StudentsBase:
  8.     def __init__(self):
  9.         self.students = []
  10.            
  11.     def add_student(self):
  12.         name = str(raw_input("Enter new student name: "))
  13.         scores_mini = []
  14.         scores_homework = []
  15.         scores_test = []
  16.         scores_atboard = []
  17.         key_index = len(self.students) + 1
  18.         newStudent = Student(key_index,name,scores_mini,scores_homework,scores_test,scores_atboard)
  19.         self.students.append(newStudent)
  20.     def __repr__(self):
  21.         string = "\n"
  22.         for student in self.students:
  23.             string += str(student.key_index) + ": "
  24.             string += student.name + "\n"
  25.         return string
  26.     def delete_last_student(self):
  27.         self.students.pop()
  28.     def edit_student(self,key):
  29.         state = -1
  30.         for s in self.students:
  31.             if s.key_index == key:
  32.                 state,s = s.edit()
  33.             if state == 1:
  34.                 break
  35.         if state == -1:
  36.             print "No such student!"
  37.     def save(self,filename):
  38.         with open(filename,'wb') as dest:
  39.             pickle.dump(self,dest,-1)
  40.     def load(self,filename):
  41.         with open(filename,'rb') as src:
  42.             self = pickle.load(src)
  43.             print "Database loaded from file:" + filename
  44.             return self
  45.  
  46. class Student:
  47.     def __init__(self,key_index,name,scores_mini,scores_homework,scores_test,scores_atboard):
  48.         self.key_index = key_index
  49.         self.name = name
  50.         self.scores_mini = scores_mini
  51.         self.scores_homework = scores_homework
  52.         self.scores_test = scores_test
  53.         self.scores_atboard = scores_atboard
  54.     def edit(self):
  55.         no_changes = self
  56.         print "Editing student: " + self.name
  57.         MESSAGE = """What would you like to edit?
  58. 1 - name
  59. 2 - scores of mini tests
  60. 3 - scores of homework
  61. 4 - scores of tests
  62. 5 - scores of boards
  63. s - accept and save changes
  64. q - exit without saving changes\n"""
  65.         while True:
  66.             c = raw_input(MESSAGE)
  67.             if c == '1':
  68.                 self.name = raw_input("Enter new student name: ")
  69.                 continue
  70.             elif c == '2':
  71.                 print "Current scores of mini-tests: ",self.scores_mini
  72.                 editlist(self.scores_mini)
  73.                 continue
  74.             elif c == '3':
  75.                 print "Current scores of homework: ",self.scores_homework
  76.                 editlist(self.scores_homework)
  77.                 continue
  78.             elif c == '4':
  79.                 print "Current scores of tests: ",self.scores_test
  80.                 editlist(self.scores_test)
  81.                 continue
  82.             elif c == '5':
  83.                 print "Current scores for boards: ",self.scores_atboard
  84.                 editlist(self.scores_atboard)
  85.                 continue
  86.             elif c == 'q':
  87.                 return 1,no_changes
  88.             elif c == 's':
  89.                 break
  90.             else:
  91.                 print "Invalid option. Try again."
  92.                 continue
  93.         return 1,self
  94.  
  95. def editlist(list_):
  96.     MESSAGE = """Select an option:
  97. a - add score to list
  98. d - delete last score from list
  99. p - print list
  100. q - return to editing\n"""
  101.     while True:
  102.         c = raw_input(MESSAGE)
  103.         if c == 'a':
  104.             list_.append(int(raw_input("Enter score to add: ")))
  105.             continue
  106.         if c == 'd':
  107.             try:
  108.                 print "Deleted",list_.pop(),"from scores."
  109.             except IndexError:
  110.                 print "Nothing to delete there!"
  111.             continue
  112.         if c == 'p':
  113.             print list_
  114.             continue
  115.         if c == 'q':
  116.             return list_
  117.  
  118. def main():
  119.     Base = StudentsBase()
  120.     if len(sys.argv) >=3:
  121.         print "You listed too much input files. Working only with first (" + sys.argv[1] + ")"
  122.         Base = Base.load(sys.argv[1])
  123.     elif len(sys.argv) == 1:
  124.         print "No database loaded!"
  125.     else:
  126.         Base = Base.load(sys.argv[1])
  127.     MESSAGE = """What would you like to do?
  128. a - add student to database
  129. e - edit student
  130. d - delete last student
  131. l - load database
  132. p - print database
  133. s - save database
  134. q - quit\n"""
  135.     while True:
  136.         c = raw_input(MESSAGE)
  137.         if c == 'a':
  138.             Base.add_student()
  139.             continue
  140.         if c == 'e':
  141.             key = int(raw_input("Enter number of student to edit:"))
  142.             Base.edit_student(key)
  143.             continue
  144.         if c == 'd':
  145.             try:
  146.                 Base.delete_last_student()
  147.             except IndexError:
  148.                 print "No students to delete from!"
  149.             continue
  150.         if c == 'l':
  151.             filename = raw_input("Enter file to read:")
  152.             Base = Base.load(filename)
  153.             continue
  154.         if c == 'p':
  155.             print Base
  156.             continue
  157.         if c == 's':
  158.             filename = raw_input("Enter file to write:")
  159.             Base.save(filename)
  160.             continue
  161.         if c == 'q':
  162.             sys.exit()
  163.         else:
  164.             print("Invalid option. Try arain.")
  165.             continue
  166. if __name__ == "__main__":
  167.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement