Guest User

Untitled

a guest
Apr 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.69 KB | None | 0 0
  1.  
  2.  
  3. class hcm (object):
  4.     def __init__ (self, nca,nha,nff):
  5.         '''the number of carbon and hydrogen elements must be an integer while the name must be a string'''
  6.         self.__nca = nca
  7.         self.__nha = nha
  8.         self.__nff = []
  9.         self.__nff.append (nff)
  10.  
  11.     def __cmp__ (self,other):
  12.         if self.__nca == other.__nca and self.__nha == other.__nha:
  13.             return 0
  14.         else:
  15.             return 1
  16.     def getnca (self):
  17.         '''returns number of carbon atoms'''
  18.         return self.__nca
  19.  
  20.     def getnha (self):
  21.         '''returns number of hydrogen atoms'''
  22.         return self.__nha
  23.  
  24.  
  25.     def getnff (self):
  26.         '''returns name'''
  27.         return self.__nff
  28.  
  29.     def __str__ (self):
  30.         '''returns a string with the molecule info'''
  31.         string = ""
  32.         for n in self.__nff:
  33.             string += n
  34.         mstring = string + " " + "C"+ str(self.__nca) + "H" + str(self.__nha)
  35.         return mstring
  36.  
  37.     def modmol (self,other):
  38.         self.__nff.append (other.__nff)
  39.        
  40.  
  41.  
  42.  
  43.  
  44. def test ():
  45.     molly = hcm(3,4,"molly")
  46.     bigmolly = hcm(6,8,"bmolly")
  47.     print molly
  48.     print bigmolly
  49.  
  50. # End of Part 1-------------------------------------------------------------
  51.  
  52.  
  53. mollylist = [] # list for all to use
  54.  
  55.  
  56. def addtolist (lomo):
  57.    
  58.     mollylist.append (lomo[0])
  59.     for n in range(len(lomo)):
  60.         for m in mollylist:
  61.             if n > 0:
  62.                 if lomo[n] != m:
  63.                     mollylist.append(lomo[n])
  64.                     print mollylist    #for debugging
  65.                     raw_input ("debug") #for debugging
  66.                 elif lomo[n] == m:
  67.                     m.modmol (lomo[n])
  68.    
  69.     print mollylist
  70.  
  71.  
  72.  
  73. # End of part 2 -------------------------------------------------------------
  74.  
  75. nof = "atoms.txt"
  76. def readarl (fo):
  77.    
  78.     line = fo.readline()
  79.     return line
  80.  
  81. def manline (line):
  82.     split = line.split ()
  83.     nameofa = split[0]
  84.     stripped = split[1].strip("C")
  85.     nums = stripped.split("H")
  86.     carb = int(nums[0])
  87.     harb = int(nums[1])
  88.     return carb,harb,nameofa
  89.  
  90. def makearo (carb,harb,name):
  91.     mol = hcm(carb,harb,name)
  92.     return mol
  93.  
  94. def rar (nof):
  95.     listofobj = []
  96.     line = readarl (nof)
  97.    
  98.     while line !="":
  99.         carb,harb,nameofa = manline (line)
  100.         mobj = hcm(carb,harb,nameofa)
  101.         listofobj.append(mobj)
  102.        
  103.         line = readarl (nof)
  104.     return listofobj
  105.  
  106. def aamtl ():
  107.     fo = open(nof,"r")
  108.     lomo = rar (fo)
  109.     addtolist (lomo)
  110.  
  111.  
  112.  
  113. #End of part 3-------------------------------------------------
  114.  
  115. def test4():
  116.     h=hcm(3,5,"test")
  117.     b=hcm(3,4,"kest")
  118.     print h==b
Add Comment
Please, Sign In to add comment