python中的datastructure帮助

时间:2017-04-27 05:02:18

标签: python data-structures

我是python中OOPS的新手。我创建了以下输入文件。我编写了python代码来获取输入文件到本地类变量的详细信息。我需要帮助创建一种更好的方式来安排和访问输入文件。

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                           

我在第一列中有我的值,在下一列中有我自己的值。

我创建了一个调用,其中包含第1列中所有元素的类变量,如数组,主机(字典),集群,fa_A(字典),fa_B(字典)。

我将它们存储在变量中,以便我可以访问它们以生成一些打印语句。

例如,切换命令功能将为主机生成切换命令

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

依此类推,我想知道最好的方法是什么。为输入文件中的所有主机创建类主机和实例化主机并生成等等是否会更好。

请在下面找到我的代码。

#!/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()

1 个答案:

答案 0 :(得分:0)

我假设在数以千计的数百万的记录中,记录的数量并不大。 如何解析并将规范化数据保存在内存中,如下所示。 在实施过程中,请致电相应的项目了解详情。

数据结构:

serials (list or set)
host (dictionary)
    {
    name: "aaaa"
    Fabric-A: [] (list or set)
    Fabric-B: [] (list or set)
    }
cluster (bool)
fab_A = (dictionary)
fab_B = (dictionary)

如果它不能帮助您解决问题,您能否详细说明您想要构建的内容。