#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    datastructure in python


    Hi,

    I am new to OOPS in python.
    My Object is to create some commands depending up on the input file.
    I have create following input file which is stored as .csv..
    I written python code to get the details from input file to local class variable.
    I need help in creating a better way we can arrange and access the input file.

    DETAILS INPUT FABRIC -A FABRIC -A FABRIC -A FABRIC-A FABRIC -B FABRIC -B FABRIC -B FABRIC-B
    ARRAY_SERIAL # T663xt AB12343
    HOST_NAME aaaaa 10000090FA87EC7A 10000090FA87EC7B 10000090FA87EC7C 10000090FA87EC7C SEPERATION 10000091FA87EC7B 10000091FA87EC7C 10000091FA87EC7D 10000091FA87EC7D
    HOST_NAME bbbbb 10000091FA87ED8A 10000091FA87FC8B 10000091FA87EC8C 10000091FA87EC8D SEPERATION 10000092FA87ED8E 10000092FA87FC8F 10000092FA87EC8G 10000092FA87EC8H
    HOST_NAME ccccc 10000093FA87ED8D 10000093FA87FC8 10000093FA87EC8C 10000093FA87EC8C SEPERATION 10000093FA87ED8D 10000093FA87FC8D 10000093FA87EC8C 10000093FA87EC8C
    CLUSTER YES
    FAB_A_FA 1E:0 2F:0 3G:0
    FAB_B_FA 16E:0 15F:0 14G:0

    I have my values in column one and its respective input values in next columns.

    Written class to check if input file exists in the location and then parse the in put file.
    I have stored values in values and dictionary. cluster, fa_A (dictionary), fa_B (dictionary).

    I have all the storage values in variables so that I can access them to generate some print statements.

    For example switch commands functions will generate switch commands for hosts

    alicreate "aaaaa_hba1", "10000090FA87EC7A"
    alicreate "aaaaa_hba2", "10000090FA87EC7B"
    alicreate "aaaaa_hba3", "10000090FA87EC7C"
    zonecreate "aaaaa_hba1_T663xt_1E_0", "aaaaa_hba1;T663xt_1E_0"

    and so on I was wondering what would be best way to do this. Will it be better to create class host and instantiate hosts for all the hosts in the input file and generate etc..

    please find my code below.
    Code:
        #!/usr/bin/python
        import os.path
        import fileinput
        import re
        
        #print "Hello World!!"
        class checkingInputFile():
        	array	= []	
        	host	= {}
        	cluster = 0
        	fabricAFas = {}
        	fabricBFas = {}
        
        	def __init__( self ):
        		print "Initializing file class"
        	
        	def checkFile ( self, file ):
        		self.file = file
        		#print ("I am in checkFile "+ file )
        		try :
        			if os.path.isfile ( self.file ) :
        				print ( "File"+ self.file +" exists \n")
        			else:
        				print "file does not exists"
        		except :
        			print ( "File " + self.file + "does not exists" )
        	
        	def parsingFile ( self, file ):
        		self.file	= 	file
        
        		print ( "I am in switch command function" + self.file ) 
        		for line in fileinput.input ( self.file ):
        			#print ( line )		
        			arrayTemp = re.search ( r'^ARRAY_SERIAL #,(\w+),(\w+)?,(\w+)?(\w+)?,(\w+)?,.*', line, re.M|re.I ) 
        			if arrayTemp is not None :
        				#print ("array lenght " + str(len(arrayTemp.group())) )
        				count = 0
        				try:
        					while arrayTemp.group() is not None:
        						#print arrayTemp.group( count+1 )
        						#print "count value is ", count
        						if arrayTemp.group( count+1 ) is not None :
        						#	print "count value inside if loop ",arrayTemp.group( count+1)
        							checkingInputFile.array.append( arrayTemp.group( count+1 ) )
        							print  " After appending ",checkingInputFile.array
        						count = count + 1
        					else:
        						print " END OF ARRAYS line \n"
        				except IndexError:
        					print " No More Array Values"
        			### Creating host Dictionary
        			hostTemp = re.search ( r'^HOST_NAME,(\w+)?,?(w+)?,?(\w+)?,?(\w+)?,?(\w+)?,?(\w+)?,?,(\w+),(w+)?,?(\w+)?,?(\w+)?,?(\w+)?,?(\w+)?,', line, re.M|re.I )
        			if hostTemp is not None:
        					#print "host values", hostTemp.group()
        					count = 0
        					try:
        						#while hostTemp.group() is not None:
        							#print hostTemp.group( count+1 )
        							#print "count value is ", count
        							if hostTemp.group( count+1 ) is not None :
        							#   print "count value inside if loop ",hostTemp.group( count+1)
        								temp = hostTemp.group().split(",")
        								del temp[0]
        								checkingInputFile.host [ hostTemp.group(count + 1 )] = temp
        								print checkingInputFile.host[hostTemp.group(count + 1 )] 
        							#count = count + 1
        					except IndexError:
        						print " No More Host Values"
        
        			clusterTemp = re.search ( r'^cluster,(\w+),?', line, re.M|re.I )
        			if clusterTemp is not None :
        					#print "host values", clusterTemp.group()
        					count = 0
        					try:
        						#while clusterTemp.group() is not None:
        							#print clusterTemp.group( count+1 )
        							#print "count value is ", count
        							if clusterTemp.group( count+1 ) is not None :
        							#   print "count value inside if loop ",clusterTemp.group( count+1)
        								temp = clusterTemp.group().split(",")
        								del temp[0]
        								checkingInputFile.cluster = clusterTemp.group ( count + 1)
        								print checkingInputFile.cluster
        							#count = count + 1
        					except IndexError:
        						print " No More Cluster Values"
        			fabricATemp = re.search ( r'^(FAB_A_FA),([\w:]+),?([\w:]+)?,?([\w:]+),?([\w:]+)?,?', line, re.M|re.I )
        			if fabricATemp is not None :
        					print fabricATemp.group()
        					count = 0
        					try:
        						#while fabricATemp.group() is not None:
        							#print fabricATemp.group( count+1 )
        							#print "count value is ", count
        							if fabricATemp.group( count ) is not None :
        							#   print "count value inside if loop ",fabricATemp.group( count+1)
        								temp = fabricATemp.group().split(",")
        								del temp[0]
        								checkingInputFile.fabricAFas [ fabricATemp.group(count + 1)] = temp
        								print checkingInputFile.fabricAFas
        							#count = count + 1
        					except IndexError:
        						print " No More Cluster Values"
        			fabricBTemp = re.search ( r'^(FAB_B_FA),([\w:]+),?([\w:]+)?,?([\w:]+),?([\w:]+)?,?', line, re.M|re.I )
        			if fabricBTemp is not None :
        					print fabricBTemp.group()
        					count = 0
        					try:
        						#while fabricBTemp.group() is not None:
        							#print fabricBTemp.group( count+1 )
        							#print "count value is ", count
        							if fabricBTemp.group( count ) is not None :
        							#   print "count value inside if loop ",fabricBTemp.group( count+1)
        								temp = fabricBTemp.group().split(",")
        								del temp[0]
        								checkingInputFile.fabricBFas [ fabricBTemp.group(count + 1)] = temp
        								print checkingInputFile.fabricBFas
        							#count = count + 1
        					except IndexError:
        						print " No More Cluster Values"
        		fileinput.close
        class switchCommandGenerator(checkingInputFile) :
        	
        	def __init__(self):
        		print " I am inside switchCommandGenerator Initilazation"
        	#def broacadeCommand (self,*kargs[]):
        
        
        def main ():
        	InputFile = "/home/pradeep/Documents/Scripts/input.csv"
        	F1 = checkingInputFile ()
        	F1.checkFile( InputFile )
        
        	#SW	=	switchCommandGenerator()
        	F1.parsingFile ( InputFile )
        	
        
        
        if __name__ == "__main__":
        	main()
    Any help is greatly appreciated.
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,888
    Rep Power
    509
    What are the switch commands?
    I haven't thought of a rule which generates


    alicreate "aaaaa_hba1", "10000090FA87EC7A"
    alicreate "aaaaa_hba2", "10000090FA87EC7B"
    alicreate "aaaaa_hba3", "10000090FA87EC7C"
    zonecreate "aaaaa_hba1_T663xt_1E_0", "aaaaa_hba1;T663xt_1E_0"

    from the input file.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo