Advertisement
am_dot_com

FP 2021-11-23

Nov 23rd, 2021 (edited)
649
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.58 KB | None | 0 0
  1. #UDT = User Defined Types
  2. class Contact:
  3.     #static
  4.     SEPARATOR = "\n" + "_.~^~."*10
  5.  
  6.     #data members / attributes / properties
  7.     #dynamic
  8.     def __init__(
  9.         self,
  10.         pName,
  11.         pTel,
  12.         pEmail,
  13.         pWeb
  14.     ):
  15.         self.mName = pName
  16.         self.mTel = pTel
  17.         self.mEmail = pEmail
  18.         self.mWeb = pWeb
  19.     #def __init__
  20.  
  21.     # dynamic
  22.     def hyperlinkForWebAddress(self):
  23.         strHyperlink = "<a href='%s'>%s</a>"%(self.mWeb, self.mWeb)
  24.         return strHyperlink
  25.     #def hyperlinkForWebAddress
  26.  
  27.     #behaviors
  28.     #there are special methods
  29.     #the "dunder" methods
  30.     #dunder = double underscore
  31.     #represents every instance of the class as a string
  32.     # dynamic
  33.     def __str__(self):
  34.         strFormat = "Name: %s\nTel: %s\ne-mail: %s\n@web: %s%s"\
  35.         %(
  36.             self.mName,
  37.             self.mTel,
  38.             self.mEmail,
  39.             #self.mWeb,
  40.             self.hyperlinkForWebAddress(),
  41.             Contact.SEPARATOR
  42.         )
  43.  
  44.         strFormatAlt = \
  45.         "Name: {}\nTel: {}\ne-mail: {}\n@web: {}{}"\
  46.         .format(self.mName, self.mTel, self.mEmail, self.mWeb, \
  47.                 Contact.SEPARATOR)
  48.  
  49.         #return strFormatAlt
  50.         return strFormat
  51.     #def __str__
  52.  
  53.     #__repr__ AKA "representation" is used, for example, by list
  54.     def __repr__(self):
  55.         return self.__str__()
  56.  
  57.     #teach Python how to relate any 2 instances of Contact
  58.     #explains the relational operator < "less than"
  59.     def __lt__(self, other):
  60.         return self.mTel<other.mTel
  61.  
  62.     #explains the relational operator == "equal to"
  63.     def __eq__(self, other):
  64.         return self.mTel==other.mTel
  65.  
  66.     #explains the relational operator > "greater than"
  67.     def __gt__(self, other):
  68.         return self.mTel>other.mTel
  69.  
  70.     def toTSVFile(self, pStrFileName):
  71.         fw = open(pStrFileName, "at") #append text
  72.         #fw.write(self.__str__()) #WRONG, TPC => TSV
  73.         fw.write(self.toTSV()) #CORRECT!
  74.         fw.close()
  75.     #def toTSVFile
  76. #class Contact
  77.  
  78. #instanciação
  79. cMaria = Contact("Maria", "123", "maria@gmail.com", "no address")
  80. cArt = Contact("Art", "321", "art@gmail.com", "http://art.net")
  81.  
  82. #print (cMaria) #thanks to __str__
  83. #print (cArt) #thanks to __str__
  84.  
  85. listContacts = [cMaria, cArt] #list of Contact
  86. #list2 = [cMaria.mName, cArt.mName] #list of string #No No No
  87. print ("Before sorting:")
  88. print (listContacts)
  89.  
  90. #sorting attempt
  91. listContacts.sort()
  92.  
  93. print ("After sorting:")
  94. print (listContacts)
  95.  
  96. cMaria.toTSVFile("contactos.tsv")
  97. cArt.toTSVFile("contactos.tsv")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement