Home > front end >  How to create objects while parsing a text file?
How to create objects while parsing a text file?

Time:05-03

I am trying to write a custom parser in Python to read the following text file.

mapping.txt

MAX_FREQ_GHZ: 1234
VRM: OPEN

RefDes: U8
.port   56
domain_spec: AVDD2_LP4_EMI_SOC 0.009
pwr_pins: U17 U19 V20 V18
gnd_pins: V19 U18 U20 W16 W18 W19 V15 V21 Y18 Y20 U22 Y22 V17
.endport

RefDes: U10
.port   123
domain_spec: AVDD2_LP4_EMI_DRAM 0.008
pwr_pins: D1 B3 B5 D5 F3 H1 K1 G2 K3 A4 F5 H5
gnd_pins: C1 E1 D2 A3 G3 D4 C5 E5 G1 K2 J3 K4 G5 J1
.endport

RefDes: U10
.port   56
domain_spec: AVDD2_LP4_EMI_DRAM 0.008
pwr_pins: B10 F10 D12 B8 D8  F8  H8  A9  K10 H12 K12
gnd_pins: C12 C8  E8  D9 A10 G10 D11 E12 J12 G8  J10 K11 G12 K9
.endport


RefDes: U4 ARM
.port   344
pwr_pins: 6
gnd_pins: 3 5
.endport

RefDes: U4 ARM
.port   344
pwr_pins: 6
gnd_pins: 3 5
.endport

RefDes: U4 ARM
.port   344
pwr_pins: 6
gnd_pins: 3 5
.endport

Below is my code:

   class PortMapping:
    
      def setFrequency(self, frequence):
        self.__frequency = frequence
      def getFrequency(self):
        return self.__frequency
    
      def setVRMType(self, vrmType):
        self.__vrmType = vrmType
      def getVRMType(self):
        return self.__vrmType
    
      def setSourceDevice(self, sourceDevice):
        self.__sourceDevice = sourceDevice
      def getSourceDevice(self):
        return self.__sourceDevice
    
      def setSinkDevice(self, sinkDevice):
        self.__sinkDevice = sinkDevice
      def getSinkDevice(self):
        return self.__sinkDevice
    
    
    class SinkDevice:
    
      def setReferenceDesignator(self, refDes):
        self.__refDes = refDes
      def getReferenceDesignator(self):
        return self.__refDes
    
      def setPort(self, port):
        self.__port = port
      def getPort(self):
        return self.__port
    
      def setDomainSpecification(self, domainSpec):
        self.__domainSpec = domainSpec
      def getDomainSpecification(self):
        return self.__domainSpec
    
      def setPowerPins(self, powerPins):
        self.__powerPins = powerPins
      def getPowerPins(self):
        return self.__powerPins
    
      def setGroundPins(self, groundPins):
        self.__groundPins = groundPins
      def getGroundPins(self):
        return self.__groundPins
    
    
    class SourceDevice:
    
      def setReferenceDesignator(self, refDes):
        self.__refDes = refDes
      def getReferenceDesignator(self):
        return self.__refDes
    
      def setPort(self, port):
        self.__port = port
      def getPort(self):
        return self.__port
    
      def setPowerPins(self, powerPins):
        self.__powerPins = powerPins
      def getPowerPins(self):
        return self.__powerPins
    
      def setGroundPins(self, groundPins):
        self.__groundPins = groundPins
      def getGroundPins(self):
        return self.__groundPins
    
    
    def main():
      with open("PinGroup_Map.TXT") as file:
        portMapping =  PortMapping()
        sinkDevices = list()
        for i, line in enumerate(file):
          if line.startswith("MAX_FREQ_GHZ"):
            portMapping.setFrequency(line.split(":")[1].strip())
          if line.startswith("VRM"):
            portMapping.setVRMType(line.split(":")[1].strip())
          if line.startswith("RefDes"):
            sd = buildSinkDevices(i)
            sinkDevices.append(sd)
    
        print(sinkDevices)
        print(portMapping.getFrequency())
        print(portMapping.getVRMType())
    
    
    def buildSinkDevices(i):
      with open("PinGroup_Map.TXT") as f:
        sinkDevice = []
        for j in range(i - 1):
            next(f)
        for line in f:
          while line.startswith(".endport"):
            sinkDevice.append(line)
      return sinkDevice
    
    if __name__=="__main__":
      main()

I am reading the mapping file and creating an object of PortMapping class first. After that my text file has a set of blocks (which are Devices objects). I am currently adding them to a list but will build objects later on.

the buildSinkDevices() builds the objects (currently adding them to list). What I am trying to do is sending the current counter to the buildSinkDevices() method and then opening the file from that counter and adding the lines to the list until it reaches .endport'. This defines one device block. The buildSinkDevices()` is not adding any line to the list. Appreciate any help?

I need to also create a collection of say Device A and Device B devices. The first three blocks in the mapping file are Device A and the last three blocks in the mapping file are Device B. I am checking in my code that if line starts with RefDes. The next six lines are basically one device. How do I create that objects?

CodePudding user response:

I think it would help if you separate reading data from the file from assigning the data. Here is some example code that will extract each dataset and turn it into its own nested list:

lines = []
with open("PinGroup_Map.TXT") as file:
    for line in file:
        line = line.strip()
        if line:
            lines.append(line)

subsets = [[]]
idx = 0
while idx < len(lines):
    if 'RefDes' in lines[idx]:
        subsets.append([lines[idx].split(':')])
    else:
        subsets[-1].append(lines[idx].split(':'))
    idx  = 1

for s in subsets:
    print(s)

Output:

[['MAX_FREQ_GHZ', ' 1234'], ['VRM', ' OPEN'], ['']]
[['RefDes', ' U8'], ['.port   56'], ['domain_spec', ' AVDD2_LP4_EMI_SOC 0.009'], ['pwr_pins', ' U17 U19 V20 V18'], ['gnd_pins', ' V19 U18 U20 W16 W18 W19 V15 V21 Y18 Y20 U22 Y22 V17'], ['.endport'], ['']]
[['RefDes', ' U10'], ['.port   123'], ['domain_spec', ' AVDD2_LP4_EMI_DRAM 0.008'], ['pwr_pins', ' D1 B3 B5 D5 F3 H1 K1 G2 K3 A4 F5 H5'], ['gnd_pins', ' C1 E1 D2 A3 G3 D4 C5 E5 G1 K2 J3 K4 G5 J1'], ['.endport'], ['']]
[['RefDes', ' U10'], ['.port   56'], ['domain_spec', ' AVDD2_LP4_EMI_DRAM 0.008'], ['pwr_pins', ' B10 F10 D12 B8 D8  F8  H8  A9  K10 H12 K12'], ['gnd_pins', ' C12 C8  E8  D9 A10 G10 D11 E12 J12 G8  J10 K11 G12 K9'], ['.endport'], [''], ['']]
[['RefDes', ' U4 ARM'], ['.port   344'], ['pwr_pins', ' 6'], ['gnd_pins', ' 3 5'], ['.endport'], ['']]
[['RefDes', ' U4 ARM'], ['.port   344'], ['pwr_pins', ' 6'], ['gnd_pins', ' 3 5'], ['.endport'], ['']]
[['RefDes', ' U4 ARM'], ['.port   344'], ['pwr_pins', ' 6'], ['gnd_pins', ' 3 5'], ['.endport']]

You only have to run this once per file, then you can collect your data from the subsets to make your objects.

  • Related