Config parser module

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • qqcq6s59@gmail.com

    #1

    Config parser module

    Hi all
    I am a newbie and I just saw a ongoing thread on Fileprocessing which
    talks abt config parser.
    I have writen many pyhton program to parse many kind of text files by
    using string module and regex. But after reading that config parser
    thread I feel stunned.

    Can somebody tell me some intro info how to parse huge data (most of
    them are input data to application softwares like nastran, abaqus etc)

    Recently I saw a great work by John on Nastran file parser (i am still
    trying to understand the program,


    An example of dat may be like this, (part of)
    (say point id coordinateSysNo x,y,z ...)
    GRID 1 12478.0 0.0 256.75 1
    GRID 2 12357.25 0.0 256.75 1
    GRID 3 12357.25 0.0 199.0 1
    (say Elementtype id property point1 point 2 point3 point4 etc)
    CQUAD4 7231 21 5691 5700 5701 56920.0

    CQUAD4 7232 21 5692 5701 5702 56930.0

    CQUAD4 7233 21 5693 5702 5703 56940.0

    the data file is very complex if i consider all complexities)

    Is is possible to use config parser module insome way for this. I also
    have few perl parser (for some part for some particular tasks) and now
    changing them to python. (I feel perl regex combination is very easy to
    learn and very powerfull)

    Any information will be appreciated.

    -jiro

  • Dale Strickland-Clark

    #2
    Re: Config parser module

    The Config Parser module is for extracting data from and writing to a
    specific format of file, generally known as a config file. As explained in
    the documentation, a config file is in the same format as .ini files
    frequently found on windows - especially used by older software.

    It is a very handy file format for all sorts of jobs, more so on Linux these
    days than Windows, probably. However, it isn't much use for your particular
    need as described here.

    qqcq6s59@gmail. com wrote:
    [color=blue]
    > Hi all
    > I am a newbie and I just saw a ongoing thread on Fileprocessing which
    > talks abt config parser.
    > I have writen many pyhton program to parse many kind of text files by
    > using string module and regex. But after reading that config parser
    > thread I feel stunned.
    >
    > Can somebody tell me some intro info how to parse huge data (most of
    > them are input data to application softwares like nastran, abaqus etc)
    >
    > Recently I saw a great work by John on Nastran file parser (i am still
    > trying to understand the program,
    > http://jrfonseca.dyndns.org/svn/phd/.../Data/Nastran/
    >
    > An example of dat may be like this, (part of)
    > (say point id coordinateSysNo x,y,z ...)
    > GRID 1 12478.0 0.0 256.75 1
    > GRID 2 12357.25 0.0 256.75 1
    > GRID 3 12357.25 0.0 199.0 1
    > (say Elementtype id property point1 point 2 point3 point4 etc)
    > CQUAD4 7231 21 5691 5700 5701 56920.0
    >
    > CQUAD4 7232 21 5692 5701 5702 56930.0
    >
    > CQUAD4 7233 21 5693 5702 5703 56940.0
    >
    > the data file is very complex if i consider all complexities)
    >
    > Is is possible to use config parser module insome way for this. I also
    > have few perl parser (for some part for some particular tasks) and now
    > changing them to python. (I feel perl regex combination is very easy to
    > learn and very powerfull)
    >
    > Any information will be appreciated.
    >
    > -jiro[/color]

    --
    Dale Strickland-Clark
    Riverhall Systems www.riverhall.co.uk
    We're recruiting Python programmers. See web site.

    Comment

    • Larry Bates

      #3
      Re: Config parser module

      ConfigParser is for parsing configuration files of the format:

      [section1]
      option1=<inform ation>
      option2=<inform ation>
      ..
      ..
      ..
      optionN=<inform ation>

      [section2]
      option1=<inform ation>
      option2=<inform ation>
      ..
      ..
      ..
      optionN=<inform attion>

      Your data doesn't fit that format.

      Looks to me like you should write a small class object for
      each different type of record that can be found in the file
      that knows how to parse that record and put the information
      from that record in to class attributes for easy access.

      Something like:

      class gridclass:
      def __init__(self, recordToParse):
      elements=record ToParse.split(' ')
      self.type=eleme nts[0]
      self.pointId=in t(elements[1])
      self.coordinate SysNo=float(ele ments[2])
      self.x=float(el ements[3])
      self.y=float(el ements[4])
      self.z=int(elem ents[5])


      Then read the lines, determine the line type and create a class
      instance for each one.

      -larry
      qqcq6s59@gmail. com wrote:[color=blue]
      > Hi all
      > I am a newbie and I just saw a ongoing thread on Fileprocessing which
      > talks abt config parser.
      > I have writen many pyhton program to parse many kind of text files by
      > using string module and regex. But after reading that config parser
      > thread I feel stunned.
      >
      > Can somebody tell me some intro info how to parse huge data (most of
      > them are input data to application softwares like nastran, abaqus etc)
      >
      > Recently I saw a great work by John on Nastran file parser (i am still
      > trying to understand the program,
      > http://jrfonseca.dyndns.org/svn/phd/.../Data/Nastran/
      >
      > An example of dat may be like this, (part of)
      > (say point id coordinateSysNo x,y,z ...)
      > GRID 1 12478.0 0.0 256.75 1
      > GRID 2 12357.25 0.0 256.75 1
      > GRID 3 12357.25 0.0 199.0 1
      > (say Elementtype id property point1 point 2 point3 point4 etc)
      > CQUAD4 7231 21 5691 5700 5701 56920.0
      >
      > CQUAD4 7232 21 5692 5701 5702 56930.0
      >
      > CQUAD4 7233 21 5693 5702 5703 56940.0
      >
      > the data file is very complex if i consider all complexities)
      >
      > Is is possible to use config parser module insome way for this. I also
      > have few perl parser (for some part for some particular tasks) and now
      > changing them to python. (I feel perl regex combination is very easy to
      > learn and very powerfull)
      >
      > Any information will be appreciated.
      >
      > -jiro
      >[/color]

      Comment

      • qqcq6s59@gmail.com

        #4
        Re: Config parser module

        Thanks a lot for Dale and Larry,

        I have not tried the OOP way yet like you mentioned,
        I will try to create a parser soon,
        maybe I will post it in the net after that, it may be useful to others
        too,

        Thanks again
        -Jiro

        Comment

        • Paul McGuire

          #5
          Re: Config parser module

          <qqcq6s59@gmail .com> wrote in message
          news:1127489369 .091513.206040@ g49g2000cwa.goo glegroups.com.. .[color=blue]
          > Hi all
          > I am a newbie and I just saw a ongoing thread on Fileprocessing which
          > talks abt config parser.
          > I have writen many pyhton program to parse many kind of text files by
          > using string module and regex. But after reading that config parser
          > thread I feel stunned.
          >
          > Can somebody tell me some intro info how to parse huge data (most of
          > them are input data to application softwares like nastran, abaqus etc)
          >
          > Recently I saw a great work by John on Nastran file parser (i am still
          > trying to understand the program,
          > http://jrfonseca.dyndns.org/svn/phd/.../Data/Nastran/
          >
          > An example of dat may be like this, (part of)
          > (say point id coordinateSysNo x,y,z ...)
          > GRID 1 12478.0 0.0 256.75 1
          > GRID 2 12357.25 0.0 256.75 1
          > GRID 3 12357.25 0.0 199.0 1
          > (say Elementtype id property point1 point 2 point3 point4 etc)
          > CQUAD4 7231 21 5691 5700 5701 56920.0
          >
          > CQUAD4 7232 21 5692 5701 5702 56930.0
          >
          > CQUAD4 7233 21 5693 5702 5703 56940.0
          >
          > the data file is very complex if i consider all complexities)
          >
          > Is is possible to use config parser module insome way for this. I also
          > have few perl parser (for some part for some particular tasks) and now
          > changing them to python. (I feel perl regex combination is very easy to
          > learn and very powerfull)
          >
          > Any information will be appreciated.
          >
          > -jiro
          >[/color]

          Here's some sample code that might give you some ideas.

          -- Paul


          data = """\
          GRID 1 12478.0 0.0 256.75 1
          GRID 2 12357.25 0.0 256.75 1
          GRID 3 12357.25 0.0 199.0 1
          CQUAD4 7231 21 5691 5700 5701 56920.0
          CQUAD4 7232 21 5692 5701 5702 56930.0
          CQUAD4 7233 21 5693 5702 5703 56940.0
          """

          class Node(object):
          def __init__(self,s ):
          self.__dict__.u pdate( zip(self.getIni tVarNames(), s.split()) )

          def __str__(self):
          return "%s %s" % (self.__class__ .__name__, self.__dict__)

          class GridNode(Node):
          def getInitVarNames (self):
          return "id,x,y,z,other ".split(',' )

          class Cquad4Node(Node ):
          def getInitVarNames (self):
          return "id,p1,p2,p3,p4 ,other".split(' ,')


          # define mapping of leading keyword to class name
          typeNodeClassMa p = {
          "GRID" : GridNode,
          "CQUAD4" : Cquad4Node,
          }

          def makeNode(s):
          nodeType,nodeAr gs = s.split(" ",1)
          nodeClass = typeNodeClassMa p[nodeType]
          return nodeClass( nodeArgs )

          for line in data.split("\n" ):
          if line:
          n = makeNode(line)
          print n



          Comment

          • qqcq6s59@gmail.com

            #6
            Re: Config parser module

            wow,
            Thanks alex, this rocks really, [ i am new to OOP style in python]
            I am trying to implement it on similar lines,
            I`ll comeback if I encounter any trouble.

            thanks again
            -Jiro



            Paul McGuire wrote:[color=blue]
            > <qqcq6s59@gmail .com> wrote in message
            > news:1127489369 .091513.206040@ g49g2000cwa.goo glegroups.com.. .[color=green]
            > > Hi all
            > > I am a newbie and I just saw a ongoing thread on Fileprocessing which
            > > talks abt config parser.
            > > I have writen many pyhton program to parse many kind of text files by
            > > using string module and regex. But after reading that config parser
            > > thread I feel stunned.
            > >
            > > Can somebody tell me some intro info how to parse huge data (most of
            > > them are input data to application softwares like nastran, abaqus etc)
            > >
            > > Recently I saw a great work by John on Nastran file parser (i am still
            > > trying to understand the program,
            > > http://jrfonseca.dyndns.org/svn/phd/.../Data/Nastran/
            > >
            > > An example of dat may be like this, (part of)
            > > (say point id coordinateSysNo x,y,z ...)
            > > GRID 1 12478.0 0.0 256.75 1
            > > GRID 2 12357.25 0.0 256.75 1
            > > GRID 3 12357.25 0.0 199.0 1
            > > (say Elementtype id property point1 point 2 point3 point4 etc)
            > > CQUAD4 7231 21 5691 5700 5701 56920.0
            > >
            > > CQUAD4 7232 21 5692 5701 5702 56930.0
            > >
            > > CQUAD4 7233 21 5693 5702 5703 56940.0
            > >
            > > the data file is very complex if i consider all complexities)
            > >
            > > Is is possible to use config parser module insome way for this. I also
            > > have few perl parser (for some part for some particular tasks) and now
            > > changing them to python. (I feel perl regex combination is very easy to
            > > learn and very powerfull)
            > >
            > > Any information will be appreciated.
            > >
            > > -jiro
            > >[/color]
            >
            > Here's some sample code that might give you some ideas.
            >
            > -- Paul
            >
            >
            > data = """\
            > GRID 1 12478.0 0.0 256.75 1
            > GRID 2 12357.25 0.0 256.75 1
            > GRID 3 12357.25 0.0 199.0 1
            > CQUAD4 7231 21 5691 5700 5701 56920.0
            > CQUAD4 7232 21 5692 5701 5702 56930.0
            > CQUAD4 7233 21 5693 5702 5703 56940.0
            > """
            >
            > class Node(object):
            > def __init__(self,s ):
            > self.__dict__.u pdate( zip(self.getIni tVarNames(), s.split()) )
            >
            > def __str__(self):
            > return "%s %s" % (self.__class__ .__name__, self.__dict__)
            >
            > class GridNode(Node):
            > def getInitVarNames (self):
            > return "id,x,y,z,other ".split(',' )
            >
            > class Cquad4Node(Node ):
            > def getInitVarNames (self):
            > return "id,p1,p2,p3,p4 ,other".split(' ,')
            >
            >
            > # define mapping of leading keyword to class name
            > typeNodeClassMa p = {
            > "GRID" : GridNode,
            > "CQUAD4" : Cquad4Node,
            > }
            >
            > def makeNode(s):
            > nodeType,nodeAr gs = s.split(" ",1)
            > nodeClass = typeNodeClassMa p[nodeType]
            > return nodeClass( nodeArgs )
            >
            > for line in data.split("\n" ):
            > if line:
            > n = makeNode(line)
            > print n[/color]

            Comment

            Working...