Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #UDT = User Defined Types
- class Contact:
- #static
- SEPARATOR = "\n" + "_.~^~."*10
- #data members / attributes / properties
- #dynamic
- def __init__(
- self,
- pName,
- pTel,
- pEmail,
- pWeb
- ):
- self.mName = pName
- self.mTel = pTel
- self.mEmail = pEmail
- self.mWeb = pWeb
- #def __init__
- # dynamic
- def hyperlinkForWebAddress(self):
- strHyperlink = "<a href='%s'>%s</a>"%(self.mWeb, self.mWeb)
- return strHyperlink
- #def hyperlinkForWebAddress
- #behaviors
- #there are special methods
- #the "dunder" methods
- #dunder = double underscore
- #represents every instance of the class as a string
- # dynamic
- def __str__(self):
- strFormat = "Name: %s\nTel: %s\ne-mail: %s\n@web: %s%s"\
- %(
- self.mName,
- self.mTel,
- self.mEmail,
- #self.mWeb,
- self.hyperlinkForWebAddress(),
- Contact.SEPARATOR
- )
- strFormatAlt = \
- "Name: {}\nTel: {}\ne-mail: {}\n@web: {}{}"\
- .format(self.mName, self.mTel, self.mEmail, self.mWeb, \
- Contact.SEPARATOR)
- #return strFormatAlt
- return strFormat
- #def __str__
- #__repr__ AKA "representation" is used, for example, by list
- def __repr__(self):
- return self.__str__()
- #teach Python how to relate any 2 instances of Contact
- #explains the relational operator < "less than"
- def __lt__(self, other):
- return self.mTel<other.mTel
- #explains the relational operator == "equal to"
- def __eq__(self, other):
- return self.mTel==other.mTel
- #explains the relational operator > "greater than"
- def __gt__(self, other):
- return self.mTel>other.mTel
- def toTSVFile(self, pStrFileName):
- fw = open(pStrFileName, "at") #append text
- #fw.write(self.__str__()) #WRONG, TPC => TSV
- fw.write(self.toTSV()) #CORRECT!
- fw.close()
- #def toTSVFile
- #class Contact
- #instanciação
- cMaria = Contact("Maria", "123", "maria@gmail.com", "no address")
- cArt = Contact("Art", "321", "art@gmail.com", "http://art.net")
- #print (cMaria) #thanks to __str__
- #print (cArt) #thanks to __str__
- listContacts = [cMaria, cArt] #list of Contact
- #list2 = [cMaria.mName, cArt.mName] #list of string #No No No
- print ("Before sorting:")
- print (listContacts)
- #sorting attempt
- listContacts.sort()
- print ("After sorting:")
- print (listContacts)
- cMaria.toTSVFile("contactos.tsv")
- cArt.toTSVFile("contactos.tsv")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement