change some lines from a read file

U

utab

Dear all,

I have to change some lines from a template file, which is rather long
to paste here, but I would like to make some parts of some lines
optional with my command line arguments but I could not see this
directly, I can count the line numbers and decide on this basis to
decide the lines to be configured to my will.

More specifically, say, I have a that file includes

this is an example python file to play around
..
..
..
some lines
..
..
..
.. -> an option line for example.
..
..
..
-> another option line so on.

and execute the script
../myScript option1 option2

so that the options at correct locations will be written.

Any other options for this simple job that i can improve my Python a
bit as well.

Best,
Umut
 
M

MRAB

utab said:
Dear all,

I have to change some lines from a template file, which is rather long
to paste here, but I would like to make some parts of some lines
optional with my command line arguments but I could not see this
directly, I can count the line numbers and decide on this basis to
decide the lines to be configured to my will.

More specifically, say, I have a that file includes

this is an example python file to play around
.
.
.
some lines
.
.
.
. -> an option line for example.
.
.
.
-> another option line so on.

and execute the script
./myScript option1 option2

so that the options at correct locations will be written.

Any other options for this simple job that i can improve my Python a
bit as well.
You could put special markers around the optional parts in the template
file. Your script would remove the markers from around the optional
parts it wants to keep and then remove any remaining unwanted optional
parts and their markers.
 
A

Anthra Norell

utab said:
Dear all,

I have to change some lines from a template file, which is rather long
to paste here, but I would like to make some parts of some lines
optional with my command line arguments but I could not see this
directly, I can count the line numbers and decide on this basis to
decide the lines to be configured to my will.

More specifically, say, I have a that file includes

this is an example python file to play around
.
.
.
some lines
.
.
.
. -> an option line for example.
.
.
.
-> another option line so on.

and execute the script
./myScript option1 option2

so that the options at correct locations will be written.

Any other options for this simple job that i can improve my Python a
bit as well.

Best,
Umut
Your description is not explicit enough to convey your intention. If
your template file is too long to post, post a short representative
section, an input data sample and a hand-edited sample of the output
data you want to generate. You will get more and better advice. .

Frederic
 
N

norseman

Anthra said:
Your description is not explicit enough to convey your intention. If
your template file is too long to post, post a short representative
section, an input data sample and a hand-edited sample of the output
data you want to generate. You will get more and better advice. .

Frederic

===================================================

Assuming the total number of changes is not large,

put OPTION1 where you want it to go in the text
put OPTION2 (same)

import sys

if len(sys.argv) >1:
OPTION1 = sys.argv[1]
else:
OPTION1 = "some default stuff"

similar for OPTION2
if len(sys.argv) >2:
like above
and any others

print section1+OPTION1+section2+OPTION2....

or
just using the line count (in place of "print sect..")
line_number= 0
for i in template:
if line_number matches line number to place Opt1
print OPTION1
elif line_number matches v2
print OPTION2
and so forth
else:
print i (line from template NOT being changed)
line_number += Line_number

you can group line number and replacement on command line
runprogram (43,"use this instead") (56,....
and parse inside program before opening template

one thing to watch out for:
for i in file... often returns just one character at a time,
meaning you will have to track EOL's yourself OR
import the readline and use it to stand a better chance of getting what
you expect from a read file function.


Today: 20090504
Logic outline, No particular version

HTH
 
N

norseman

Anthra said:
Your description is not explicit enough to convey your intention. If
your template file is too long to post, post a short representative
section, an input data sample and a hand-edited sample of the output
data you want to generate. You will get more and better advice. .

Frederic

===================================================

Assuming the total number of changes is not large,

put OPTION1 where you want it to go in the text
put OPTION2 (same)

import sys

if len(sys.argv) >1:
OPTION1 = sys.argv[1]
else:
OPTION1 = "some default stuff"

similar for OPTION2
if len(sys.argv) >2:
like above
and any others

print section1+OPTION1+section2+OPTION2....

or
just using the line count (in place of "print sect..")
line_number= 0
for i in template:
if line_number matches line number to place Opt1
print OPTION1
elif line_number matches v2
print OPTION2
and so forth
else:
print i (line from template NOT being changed)
line_number += Line_number

you can group line number and replacement on command line
runprogram (43,"use this instead") (56,....
and parse inside program before opening template

one thing to watch out for:
for i in file... often returns just one character at a time,
meaning you will have to track EOL's yourself OR
import the readline and use it to stand a better chance of getting what
you expect from a read file function.


Today: 20090504
Logic outline, No particular version

HTH

Steve
 
U

utab

Your description is not explicit enough to convey your intention. If
your template file is too long to post, post a short representative
section, an input data sample and a hand-edited sample of the output
data you want to generate. You will get more and better advice. .

Frederic

Thanks here is a sample Session file from a commercial finite element
pre-post processor PATRAN, the file longer but the ideas are the same
so that let me give an example, say I would like to make write line 10
composed of some command line arguments: path+database_name, Also
there are some lines similar to this in the below sections of the
file. I am doing it with a if-elif-else clause checking the line
numbers with which I thought of there should be a better way and ended
up asking...

1 $# Session file patran.ses.02 started recording at 05-May-09
11:12:25
2 $# Build: 15.0.038 Mon Aug 20 21:31:45 PDT 2007
3 $# Recorded by: Patran 2007 r1b
4 $# FLEXlm initialization complete. Acquiring license(s)...
5 $# License File: /opt/patran-2007r1b/license.dat
6 $# Patran 2007 r1b has obtained 1 concurrent license(s) from
FLEXlm per a
7 $# request to execute on Linux node hpc06(001e4f20c494) (Linux
8 $# 2.6.23.15-80.fc7) at 05-May-09 11:12:25.
9 uil_file_new.go( "/opt/patran-2007r1b/template.db", @
10 "/home/utabak/PATRAN/cavityModels/fromGmsh.db" )
11 $# Question from application FILE
12 $# Database /home/utabak/PATRAN/cavityModels/fromGmsh.db
already exists.
13 $# Do you wish to delete the existing database and create a new
one ?
14 $? YES 36000002
15 $# Copying /opt/patran-2007r1b/template.db to
16 $# /home/utabak/PATRAN/cavityModels/fromGmsh.db
17 $# Template copy complete.
18 $# Database version 3.8 created by Patran 2007 r1b successfully
opened.
19 ga_viewport_size_set( "default_viewport", 14.571214, 7.509466,
1 )
20 ga_viewport_location_set( "default_viewport", 0.000000,
1.088771, 1 )
21 $# Creating journal file /home/utabak/PATRAN/cavityModels/
fromGmsh.db.jou at
22 $# 05-May-09 11:12:43
23 nastran_input_import( "/home/utabak/PATRAN/cavityModels/
toPatran.bdf", @
24 "default_group", 10, [TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, FALSE, @
25 TRUE], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0,
0, 0], [ @
26 -2000000000, -2000000000, -2000000000, -2000000000,
-2000000000, -2000000000, @
27 -2000000000, -2000000000, 0, 0] )
28 $# Reading of MSC.Nastran input file completed.
29 ui_exec_function( "mesh_seed_display_mgr", "init" )

Best,
 
A

Anthra Norell

utab said:
Thanks here is a sample Session file from a commercial finite element
pre-post processor PATRAN, the file longer but the ideas are the same
so that let me give an example, say I would like to make write line 10
composed of some command line arguments: path+database_name, Also
there are some lines similar to this in the below sections of the
file. I am doing it with a if-elif-else clause checking the line
numbers with which I thought of there should be a better way and ended
up asking...

1 $# Session file patran.ses.02 started recording at 05-May-09
11:12:25
2 $# Build: 15.0.038 Mon Aug 20 21:31:45 PDT 2007
3 $# Recorded by: Patran 2007 r1b
4 $# FLEXlm initialization complete. Acquiring license(s)...
5 $# License File: /opt/patran-2007r1b/license.dat
6 $# Patran 2007 r1b has obtained 1 concurrent license(s) from
FLEXlm per a
7 $# request to execute on Linux node hpc06(001e4f20c494) (Linux
8 $# 2.6.23.15-80.fc7) at 05-May-09 11:12:25.
9 uil_file_new.go( "/opt/patran-2007r1b/template.db", @
10 "/home/utabak/PATRAN/cavityModels/fromGmsh.db" )
Okay! Please clarify:
Question 1: Is it always line 10 or is it always a path-file-name, which
could be on another line?
Question 2: Are there other lines you want to translate? If yes, how do
you identify them? By line number or by content?
Question 3: If you identify your target by content, what ist the
distinctive feature which it doesn't share with non-target lines? (e.g.
Path-file name format and nothing else)
Question 4: Do you want to substitute the path name with another path
name and the file name with another file name, both of which you supply
on the command line?
Question 5: If your input has a number of different names, do you want
to translate them all to the same name, in which case you'd only have to
supply a single pair of names on the command line?
Question 6: If your input has a number of different names and your
translation also consists of a number of path-file-name pairs, how do
the targets and substitutes correlate? Sequentially? By pattern?
Question 7: Can you estimate how many different path-file-names your
complete session file contains? And how many different substitutes you
may have in extreme cases?
11 $# Question from application FILE
12 $# Database /home/utabak/PATRAN/cavityModels/fromGmsh.db
already exists.
13 $# Do you wish to delete the existing database and create a new
one ?
14 $? YES 36000002
15 $# Copying /opt/patran-2007r1b/template.db to
Question 8: Here you have a file name. What do you do with it?
16 $# /home/utabak/PATRAN/cavityModels/fromGmsh.db
Question 9: Here line 10 repeats. What do you do with it?
17 $# Template copy complete.
18 $# Database version 3.8 created by Patran 2007 r1b successfully
opened.
19 ga_viewport_size_set( "default_viewport", 14.571214, 7.509466,
1 )
20 ga_viewport_location_set( "default_viewport", 0.000000,
1.088771, 1 )
21 $# Creating journal file /home/utabak/PATRAN/cavityModels/
fromGmsh.db.jou at
Question 10: Here the file name of line 10 repeats. What do you do?
22 $# 05-May-09 11:12:43
23 nastran_input_import( "/home/utabak/PATRAN/cavityModels/
toPatran.bdf", @
Question 11: Here the file name of line 10 repeats. What do you do?
24 "default_group", 10, [TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, FALSE, @
25 TRUE], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0,
0, 0], [ @
26 -2000000000, -2000000000, -2000000000, -2000000000,
-2000000000, -2000000000, @
27 -2000000000, -2000000000, 0, 0] )
28 $# Reading of MSC.Nastran input file completed.
29 ui_exec_function( "mesh_seed_display_mgr", "init" )

Best,

Frederic
 

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

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top