in order to correlate individual properties. The UUID work number attribute is used. Each object has a unique property. In addition to ensure that the object is unique. I gave each object a hash of the properties and generated a unique property.
The hash process was found not to be a hash object. Hash can only be a immutable object. Second, the object is removed using the class static method, so that all the objects can be taken out at once, and then the user chooses to save the UUID to the corresponding associated attributes.
if you want to query the UUID corresponding to the object is who can go through the UUID to open the corresponding file. The corresponding object can then be obtained. All objects of the mask can be linked together.
#!/usr/bin/env python
#-*-Coding:utf-8-*-
Import Hashlib
Import Pickle,os
def create_uuid (info):
M=HASHLIB.MD5 ()
For value in info:
M.update (Value.encode (' Utf-8 '))
Return M.hexdigest ()
Class Basecls:
def __init__ (self,name):
Self.name=name
def uuid (Self,info):
return Create_uuid (Info)
def save (self):
Pickle.dump (Self,open (Os.path.join (SELF.DB_PATHDIR,SELF.UUID), ' WB ')
@classmethod
def get_obj_all_by_uuid (CLS):
Res_l=[]
For file_name in Os.listdir (cls.db_pathdir):
F=open (Os.path.join (cls.db_pathdir,file_name), ' RB ')
Res_l.append (Pickle.load (f))
F.close ()
Return res_l
@classmethod
def get_obj_by_uuid (cls,filename):
F=open (Os.path.join (cls.db_pathdir,filename), ' RB ')
Res=pickle.load (f)
F.close ()
return res
Class Couser (BASECLS):
Db_pathdir= ' Couser '
def __init__ (self,name):
Super (). __INIT__ (name)
Self.uuid = Self.uuid ([self.name])
Class Teacher (BASECLS):
Db_pathdir= ' Teacher '
def __init__ (Self,name,course):
Super (). __INIT__ (name)
Self.course=course
Self.uuid = Self.uuid ([Self.name, Self.course])
Class Student:
def __init__ (Self,name,teacher,course):
Super (). __INIT__ (name)
Self.teacher=teacher
Self.course=course
Self.uuid=self.uuid ([Self.name,self.teacher,self.course])
C1=couser (' python ')
C2=couser (' Linux ')
C3=couser (' Go ')
C1.save ()
C2.save ()
C3.save ()
Out= ' Output course information and UUID list '
Print (out)
res=[(obj.name,obj.__dict__) for obj in Couser.get_obj_all_by_uuid ()]
Print (RES)
T1=teacher (' Alex ', ' 34d1f91fb2e514b8576fab1a75a89a6b ')
res=[(obj.name,obj.__dict__) for obj in Teacher.get_obj_all_by_uuid ()]
Print (RES)
Python develops object-oriented thinking--Class Association