python每日作业4/8

1、练习上课作业讲解的面向对象代码,明天默写
2、基于上课作业讲解的面向对象代码,扩写Student类
3、加入序列化与反序列化操作
4、对象之间的关联采用id号
5、可以通过id找到对应的文件,然后从文件中反序列化出执行的学校、班级、课程、学生对象

 

import uuid
import pickle
import os

class School():
    def __init__(self,name,addr):
        self.name = name
        self.addr = addr
        self.classes_uid = []
        self.uid = uuid.uuid3(uuid.NAMESPACE_DNS, name)

    def add_class(self,class_obj):
        self.classes_uid.append(class_obj.uid)

    def show_school(self):
        print(self.name.center(50,'='))
        for class_file in self.classes_uid:
            if os.path.exists('{}.pkl'.format(class_file)):
                with open('{}.pkl'.format(class_file),'rb') as f:
                    class1 = pickle.load(f)
                class1.show_class()

    def save(self):
        with open('{}.pkl'.format(self.uid), 'wb') as f:
            pickle.dump(self, f)
        with open('uid','a',encoding='utf-8') as f:
            f.write('{}\n'.format(str({self.name:self.uid})))

class Class():
    def __init__(self,name):
        self.uid = uuid.uuid3(uuid.NAMESPACE_DNS,name)
        self.name = name
        self.courses_uid = []
        self.students = []
    def add_course(self,course_obj):
        self.courses_uid.append(course_obj.uid)
    def show_class(self):
        print(self.name)
        for cou1 in self.courses_uid:
            if os.path.isfile('{}.pkl'.format(cou1)):
                with open('{}.pkl'.format(cou1), 'rb') as f:
                    cou = pickle.load(f)
                cou.show_course()


    def save(self):
        with open('{}.pkl'.format(self.uid), 'wb') as f:
            pickle.dump(self, f)


class Course():
    def __init__(self,name,price,time):
        self.uid = uuid.uuid3(uuid.NAMESPACE_DNS,name)
        self.name = name
        self.price = price
        self.time = time
    def show_course(self):
        print('课程名称:{} 课程价格:{} 课程周期:{}'.format(self.name,self.price,self.time))

    def save(self):
        with open('{}.pkl'.format(self.uid), 'wb') as f:
            pickle.dump(self,f)

class Student():
    def __init__(self,name,age,sex):
        self.uid = uuid.uuid3(uuid.NAMESPACE_DNS,name)
        self.name = name
        self.age = age
        self.sex = sex
        self.classes = []
        self.cores = None
    def choose_class(self,class_obj):
        self.classes.append(class_obj)
    def show_class(self):
        for i in self.classes:
            i.show_class()

    def save(self):
        with open('{}.pkl'.format(self.uid), 'wb') as f:
            pickle.dump(self, f)


class Teacher():
    def __init__(self,name):
        self.uid = uuid.uuid3(uuid.NAMESPACE_DNS,name)
        self.name = name
    def set_core(self,stu_obj,core):
        str_obj.cores = core

    def save(self):
        with open('{}.pkl'.format(self.uid), 'wb') as f:
            pickle.dump(self, f)

school_shanghai = School('魔都校区','上海')
class1 = Class('上海14期')
class2 = Class('上海15期')
course1 = Course('python全栈',20000,'6 mons')
course2 = Course('linux运维',18000,'6 mons')
class1.add_course(course1)
class2.add_course(course2)

school_shanghai.add_class(class1)
school_shanghai.add_class(class2)
stu1=Student("egon",18,"man")
stu1.choose_class(class1)


course1.save()
course2.save()
class1.save()
class2.save()
stu1.save()
school_shanghai.show_school()
stu1.show_class()


'''
执行结果
=======================魔都校区=======================
上海14期
课程名称:python全栈 课程价格:20000 课程周期:6 mons
上海15期
课程名称:linux运维 课程价格:18000 课程周期:6 mons
上海14期
课程名称:python全栈 课程价格:20000 课程周期:6 mons
'''

 

posted @ 2020-04-08 20:37  耗油炒白菜  阅读(226)  评论(0编辑  收藏  举报