pyparsing batch file

P

Paul McGuire

Unfortunately, it does not parse the whole file names with
the underscore and I do not know yet, how I can access the
line with 'define/boundary-conditions'. Every 'argument' of
that command should become a separate python variable!?
Does anyone have an idea, how I can achieve this!?
Regards!
Fabian

You are trying to match "keps1500_500.dat" with the expression
"Word(alphanums)". Since the filename contains characters other than
alphas and numbers, you must add the remaining characters ("." and
"_") to the expression. Try changing:

write= Word(alphanums)

to:

write= Word(alphanums+"._")


To help you to parse "/define/boundary-conditions in velocity-inlet 10
0.1 0.1 no 1", we would need to know just what these arguments are,
and what values they can take. I'll take a wild guess, and propose
this:

real = Combine(integer + "." + integer)
defineBoundaryConditions = "/define/boundary-conditions" + \
oneOf("in out inout")("direction") + \
Word(alphanums+"-")("conditionName") + \
integer("magnitude") + \
real("initialX") + \
real("initialY") + \
oneOf("yes no")("optional") + \
integer("normal")

(Note I am using the new notation for setting results names,
introduced in 1.4.7 - simply follow the expression with ("name"),
instead of having to call .setResultsName.)

And here is a slight modification to your printout routine, using the
dump() method of the ParseResults class:

for tokens in defineBoundaryConditions.searchString(data):
print
print "Boundary Conditions = "+ tokens.conditionName
print tokens.dump()
print
print 50*"-"


prints:

Boundary Conditions = velocity-inlet
['/define/boundary-conditions', 'in', 'velocity-inlet', '10', '0.1',
'0.1', 'no', '1']
- conditionName: velocity-inlet
- direction: in
- initialX: 0.1
- initialY: 0.1
- magnitude: 10
- normal: 1
- optional: no
 
F

Fabian Braennstroem

Hi,

me again :)

I would like to parse a small batch file:

file/read-case kepstop.cas
file/read-data keps1500.dat
solve/monitors/residual/plot no
solve/monitors/residual/print yes
/define/boundary-conditions in velocity-inlet 10 0.1 0.1 no 1
it 500
wd keps1500_500.dat
yes
exit

Right now, I use this little example:

from pyparsing import *

input =
open("/home/fab/HOME/Dissertation/CFD/Fluent/Batch/fluent_batch",
'r')
data = input.read()

#------------------------------------------------------------------------
# Define Grammars
#------------------------------------------------------------------------

integer = Word(nums)
hexnums = Word(alphanums)
end = Literal("\n").suppress()
all = SkipTo(end)
#threadname = dblQuotedString
threadname_read_case = Literal("file/read-case")
threadname_read_data= Literal("file/read-data")
threadname_it = Literal("it")
write_data=Literal("wd")
cas_datei= Word(alphanums)
iteration= Word(nums)
write= Word(alphanums)
file_read_data= "file/read-data " + hexnums.setResultsName("rd")

logEntry = threadname_read_case.setResultsName("threadname")
+ cas_datei.setResultsName("cas_datei")+file_read_data
logEntry = file_read_data
logEntryNew = threadname_it.setResultsName("threadname") +
iteration.setResultsName("iteration")
logEntryWD = write_data.setResultsName("threadname") +
write.setResultsName("write")

#------------------------------------------------------------------------

for tokens in logEntryNew.searchString(data):
    print
    print "Iteration Command=\t "+ tokens.threadname
    print "Number of Iterations=\t "+ tokens.iteration
    for x in tokens.condition:
       print x
    print 50*"-"

for tokens in logEntryWD.searchString(data):
    print
    print "Write Data Command=\t "+ tokens.threadname
    print "Data File Name=\t "+ tokens.write
    for x in tokens.condition:
       print x
    print 50*"-"

for tokens in logEntry.searchString(data):
    print
    print "no idea=\t "+ tokens.threadname
    print "Data File=\t "+ tokens.rd
    print
    for x in tokens.condition:
       print x
    print 50*"-"


Unfortunately, it does not parse the whole file names with
the underscore and I do not know yet, how I can access the
line with 'define/boundary-conditions'. Every 'argument' of
that command should become a separate python variable!?
Does anyone have an idea, how I can achieve this!?
Regards!
Fabian
 
F

Fabian Braennstroem

Hi Paul,

Paul said:
Unfortunately, it does not parse the whole file names with
the underscore and I do not know yet, how I can access the
line with 'define/boundary-conditions'. Every 'argument' of
that command should become a separate python variable!?
Does anyone have an idea, how I can achieve this!?
Regards!
Fabian

You are trying to match "keps1500_500.dat" with the expression
"Word(alphanums)". Since the filename contains characters other than
alphas and numbers, you must add the remaining characters ("." and
"_") to the expression. Try changing:

write= Word(alphanums)

to:

write= Word(alphanums+"._")


To help you to parse "/define/boundary-conditions in velocity-inlet 10
0.1 0.1 no 1", we would need to know just what these arguments are,
and what values they can take. I'll take a wild guess, and propose
this:

real = Combine(integer + "." + integer)
defineBoundaryConditions = "/define/boundary-conditions" + \
oneOf("in out inout")("direction") + \
Word(alphanums+"-")("conditionName") + \
integer("magnitude") + \
real("initialX") + \
real("initialY") + \
oneOf("yes no")("optional") + \
integer("normal")

(Note I am using the new notation for setting results names,
introduced in 1.4.7 - simply follow the expression with ("name"),
instead of having to call .setResultsName.)

And here is a slight modification to your printout routine, using the
dump() method of the ParseResults class:

for tokens in defineBoundaryConditions.searchString(data):
print
print "Boundary Conditions = "+ tokens.conditionName
print tokens.dump()
print
print 50*"-"


prints:

Boundary Conditions = velocity-inlet
['/define/boundary-conditions', 'in', 'velocity-inlet', '10', '0.1',
'0.1', 'no', '1']
- conditionName: velocity-inlet
- direction: in
- initialX: 0.1
- initialY: 0.1
- magnitude: 10
- normal: 1
- optional: no

Great! Thanks for the very good explanation!

Regards!
Fabian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,201
Latest member
KourtneyBe

Latest Threads

Top