473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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,
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

Sep 23 '05 #1
5 2604
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.

qq******@gmail.com wrote:
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


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

Sep 23 '05 #2
ConfigParser is for parsing configuration files of the format:

[section1]
option1=<information>
option2=<information>
..
..
..
optionN=<information>

[section2]
option1=<information>
option2=<information>
..
..
..
optionN=<informattion>

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=recordToParse.split(' ')
self.type=elements[0]
self.pointId=int(elements[1])
self.coordinateSysNo=float(elements[2])
self.x=float(elements[3])
self.y=float(elements[4])
self.z=int(elements[5])
Then read the lines, determine the line type and create a class
instance for each one.

-larry
qq******@gmail.com wrote:
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

Sep 23 '05 #3
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

Sep 24 '05 #4
<qq******@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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


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__.update( zip(self.getInitVarNames(), 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
typeNodeClassMap = {
"GRID" : GridNode,
"CQUAD4" : Cquad4Node,
}

def makeNode(s):
nodeType,nodeArgs = s.split(" ",1)
nodeClass = typeNodeClassMap[nodeType]
return nodeClass( nodeArgs )

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

Sep 24 '05 #5
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:
<qq******@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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


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__.update( zip(self.getInitVarNames(), 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
typeNodeClassMap = {
"GRID" : GridNode,
"CQUAD4" : Cquad4Node,
}

def makeNode(s):
nodeType,nodeArgs = s.split(" ",1)
nodeClass = typeNodeClassMap[nodeType]
return nodeClass( nodeArgs )

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


Sep 27 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Peter Maas | last post by:
Hi, currently I'm trying to create a pgsql backend for the roundup issue tracker using the mysql backend as a template (is somebody aware of such a thing? I couldn't find one). The author has...
0
by: Simon John | last post by:
I have a program that consists of one main module and lots of small sub-modules. In the main module I open a text file and grep for a language setting, this language setting will then be used as...
20
by: tomerfiliba | last post by:
hey i've been seeing lots of config-file-readers for python. be it ConfigObj (http://www.voidspace.org.uk/python/configobj.html) or the like. seems like a trend to me. i came to this conclusion...
3
by: Jose Fernandez | last post by:
HI first of all, excuse me for my english. And Thank in advance for even read my post. I have a problem that is driving me insane. I have an application (JUCAR) which use HttpModule and i have...
1
by: Torsten Bronger | last post by:
Hallöchen! I have a module parser.py in the same directory as the main module. In the main module, I import "parser". On Linux, this works as expected, however on Windows, it imports the stdlib...
0
by: UncleRic | last post by:
Environment: Mac OS X (10.4.10) on MacBook Pro I'm a Perl Neophyte. I've downloaded the XML::Parser module and am attempting to install it in my working directory (referenced via PERL5LIB env): ...
3
by: Lowell Alleman | last post by:
Here is the situation: I wrote my own log handler class (derived from logging.Handler) and I want to be able to use it from a logging config file, that is, a config file loaded with the...
3
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I cut and paste the following code from msdn help page which it just introduces view and multiview server controls. Here is what I do: in vs studio 2005, File --New Web Site, it...
1
by: Malthe Borch | last post by:
(Note: repost from python-dev) The ``compiler.ast`` module makes parsing Python source-code and AST manipulation relatively painless and it's straight-forward to implement a transformer class. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.