Python develops object-oriented thinking--Class Association

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.