OO Aplikacija

JSON: http://www.json.org DBM: https://pymotw.com/3/dbm/

Klasa: Database:

import dbm
import json
class Database:
    def __init__(self,dbname):
        try:
            self.db = dbm.open(dbname,'w')
        except:
            self._create(dbname)
    def _create(self,dbname='test'):
        self.db = dbm.open(dbname,'c')
    def open(self,dbname):
        self.db = dbm.open(dbname,'w')
    def read(self, key):
            if key in self.db:
                return json.loads(self.db.get(key).decode())
            else:
                return 'Key '+key+' not found.'
    def readAll(self):
        records = []
        k = self.db.firstkey()
        while k != None:
            records.append({k.decode():json.loads(self.db[k].decode())})
            k = self.db.nextkey(k)
        return records
    def write(self, key, text):
        self.db[key] = text
    def isKey(self,key):
        #print(self.db.get(key,'Key '+key+' not found.'))
        if key in self.db:
            return True
        else:
            return False
    def delete(self,key):
        try:
            del self.db[key]
        except:
            print('Key',key,'not found')
    def compact(self):
        self.db.reorganize()
    def close(self):
        self.db.close()

Klasa: FIS:

import json
import sys
class FacultyInformationSystem:
    def __init__(self,db):
        self.db = db
    def draw_line(self):
        print('*'*60)
    def toJSON(self,studentID,last_name,first_name):
        return json.dumps({'ID':studentID,
                           'last_name':last_name,'first_name':first_name,'exams':[]})
    def student_registration(self,studentID,last_name,first_name):
        self.db.write(studentID, self.toJSON(studentID,last_name,first_name))
    def find_student(self,studentID):
        return self.db.read(studentID)
    def delete_student(self,studentID):
        self.db.delete(studentID)
    def mark_registration(self,studentID,course,mark,date,professor):
        student = self.db.read(studentID)
        student['exams'].append({'course':course,'mark':mark,
                                  'date':date,'professor':professor})
        self.db.write(studentID,json.dumps(student))
    def marks_registration(self):
        while 1:
            studentID = input('Student ID: ')
            if studentID =='': break
            if self.db.isKey(studentID):
                while 1:
                    course = input('Course: ')
                    if course == '': break
                    mark = input('Mark: ')
                    date = input('Date: ')
                    professor = input('Professor: ')
                    self.mark_registration(studentID,course,mark,date,professor)
                    print(self.find_student(studentID))
    def students_registration(self):
        print('Students registration')
        while 1:
            studentID = input('Student ID: ')
            if studentID == '': break
            last_name=input('Last name: ')
            first_name = input('First name: ')
            self.student_registration(studentID,last_name,first_name)
    def student_overview(self):
        studentID = input('Sudent ID: ')
        student = self.find_student(studentID)
        self.draw_line()
        if type(student) == str:
            print(student)
        else:
            print(student['last_name']+' '+student['first_name'])
            for exam in student['exams']:
                print(exam['course'],exam['mark'],
                      exam['date'],exam['professor'])
        self.draw_line()
    def delete_student(self):
        studentID = input('Student ID: ')
        self.db.delete(studentID)
    def listAll(self):
        self.draw_line()
        for record in self.db.readAll():
            for key,data in record.items():
                print('{0:>10} {1} {2}'.format(key,data['last_name'],data['first_name']))
        self.draw_line()
    def quit(self):
        self.db.close()
        sys.exit(0)

Glavni program:

from dbclass import Database
from fis import FacultyInformationSystem

def menu():
    print('1 - Students registration')
    print('2 - Marks registrations')
    print('3 - Student overview')
    print('4 - Student deletion')
    print('5 - List of all students')
    print('6 - Quit')

db = Database('students')
do=FacultyInformationSystem(db)

proces = {'1':do.students_registration, '2': do.marks_registration,
          '3':do.student_overview,'4':do.delete_student,
          '5':do.listAll,'6':do.quit}
while 1:
    menu()
    oper = input('Choice: ')
    proces[oper]()