• 知识回顾


      1 #!/usr/bin/env python3
      2 #-*- coding:utf-8 -*-
      3 '''
      4 Administrator 
      5 2018/8/14 
      6 '''
      7 import pickle,os,uuid
      8 class School:
      9     def __init__(self,name):
     10         self.name=name
     11         self.school_id=uuid.uuid1()
     12 
     13     def save(self):
     14 
     15         file_address=os.path.join(os.path.dirname(os.path.abspath(__file__)),"accounts","db_school","%s.txt"%self.school_id)
     16         with open(file_address,"wb") as f:
     17             pickle.dump(self,f)
     18     @staticmethod
     19     def get_all():
     20         obj_list=[]
     21         address=os.path.join(os.path.dirname(os.path.abspath(__file__)),"accounts","db_school")
     22         for item in os.listdir(address): #/home/tom/下找所有的文件
     23             item=os.path.join(address,item)
     24             with open(item,"rb") as f:
     25                 obj=pickle.load(f)
     26             if type(obj) is School:#里面存的数据多。如果是学校类型,则筛选出来
     27                 obj_list.append(obj)
     28             # obj_list.append(obj)
     29         return obj_list
     30 
     31     def __str__(self):
     32         return "学校的名称是:%s"%self.name
     33 
     34 
     35 class Classes:
     36     def __init__(self,name):
     37         self.name=name
     38         self.nid=uuid.uuid1()
     39     def save(self):
     40         file_address=os.path.join(os.path.dirname(os.path.abspath(__file__)),"accounts","db_classes","%s.txt"%self.nid)
     41         with open(file_address,"wb") as f:
     42             pickle.dump(self,f)
     43     @staticmethod
     44     def get_all():
     45         obj_list = []
     46         address = os.path.join(os.path.dirname(os.path.abspath(__file__)), "accounts","db_classes")
     47         for item in os.listdir(address):  # /home/tom/下找所有的文件
     48             item = os.path.join(address, item)
     49             with open(item, "rb") as f:
     50                 obj = pickle.load(f)
     51             if type(obj) is Classes:  # 里面存的数据多。如果是学校类型,则筛选出来
     52                 obj_list.append(obj)
     53         return obj_list
     54     def __str__(self):
     55         return "班级:%s"%self.name
     56 
     57 
     58 class Student:
     59     def __init__(self,name,classes_obj):
     60         self.name=name
     61         # 班级名称可能存在重复,我们需要唯一字段,所以这里改成班级对象。这是唯一的。
     62         #如果保存班级,程序中,班级对象保存了两次。
     63         self.classesObj=classes_obj
     64         self.student_id=uuid.uuid1()
     65     def save(self):#此处班级对象保存了两次。数据安全性
     66         file_address = os.path.join(os.path.dirname(os.path.abspath(__file__)), "accounts","db_student","%s.txt" % self.student_id)
     67         with open(file_address, "wb") as f:
     68             pickle.dump(self, f)
     69 
     70     @staticmethod
     71     def get_all():
     72         obj_list = []
     73         address = os.path.join(os.path.dirname(os.path.abspath(__file__)), "accounts", "db_student")
     74         for item in os.listdir(address):  # /home/tom/下找所有的文件
     75             item = os.path.join(address, item)
     76             with open(item, "rb") as f:
     77                 obj = pickle.load(f)
     78             if type(obj) is Student:  # 里面存的数据多。如果是学校类型,则筛选出来
     79                 obj_list.append(obj)
     80         return obj_list
     81 
     82     def __str__(self):
     83         return "姓名:%s, %s" % (self.name,self.classesObj.get_obj_by_nid())
     84 
     85 class Nid:
     86     def __init__(self,class_nid):
     87         self.nid=class_nid
     88     def get_obj_by_nid(self):
     89         address = os.path.join(os.path.dirname(os.path.abspath(__file__)), "accounts")
     90         flag = True
     91         for i in os.listdir(address):
     92             file_address=os.path.join(address,i)
     93             if flag:
     94                 for j in os.listdir(file_address):
     95                     file_name=os.path.splitext(j)[0] #拿到文件名,不带扩展名
     96                     if file_name==str(self.nid):
     97                         new_address=os.path.join(file_address,j)
     98                         with open(new_address,"rb") as f:
     99                             data=pickle.load(f)
    100                         return data
    101 
    102 
    103 # s1=School("上海")
    104 # s1.save()
    105 # # # #/home/tom/1
    106 # s2=School("北京")
    107 # s2.save()
    108 # #/home/tom/2
    109 
    110 # School_obj_list=School.get_all()
    111 # for obj in School_obj_list:
    112 #     #print("学校名称:%s"%obj.name)
    113 #     print(obj)
    114 # c1=Classes("全栈1班")
    115 # c1.save()
    116 # c2=Classes("全栈2班")
    117 # c2.save()
    118 
    119 # Classes_obj_list=Classes.get_all()
    120 # for i in Classes_obj_list:
    121 #     #print("学校名称:%s"%obj.name)
    122 #     print(i)
    123 #
    124 # School_obj_list=School.get_all()
    125 # for obj in School_obj_list:
    126 #     #print("学校名称:%s"%obj.name)
    127 #     print(obj)
    128 ##########创建学员############
    129 # classes_obj_list=Classes.get_all()
    130 # print("chakan ",classes_obj_list)
    131 # for i,j in enumerate(classes_obj_list,1):
    132 #     print(i,">>>>",j)
    133 # # print(classes_obj_list[0])
    134 #
    135 # sel=input("请输入班级索引号:")
    136 # name=input("请输入学生姓名:")
    137 # if sel.isdigit():
    138 #     sel=int(sel)
    139 #     if sel>0 and sel<=len(classes_obj_list):
    140 #         class_obj=classes_obj_list[sel-1]
    141 #         student_obj=Student(name,Nid(class_obj.nid))#chuanjian
    142 #         print(student_obj)
    143 #         student_obj.save()
    144 
    145 # Nid(class_obj.nid).get_obj_by_nid()
    146 # obj.classesObj.get_obj_by_nid()
    147 
    148 Student_obj_list=Student.get_all()
    149 for obj in Student_obj_list:
    150     #print("学校名称:%s"%obj.name)
    151     print(obj)
    152     # print(obj.classesObj.get_obj_by_nid())
  • 相关阅读:
    awk线程号
    std::string::substr函数
    计数器表的简单使用
    vim + oh-my-zsh + git搭建开发环境
    <<代码大全>>阅读笔记之二 变量名的力量
    <<代码大全>>阅读笔记之一 使用变量的一般事项
    压测工具ab的简单使用
    nginx配置文件详解
    numba初体验
    Linux查找文件内容小技巧
  • 原文地址:https://www.cnblogs.com/Mengchangxin/p/9480117.html
Copyright © 2020-2023  润新知