Parsing a setup file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tommy Grav

    #1

    Parsing a setup file

    Hi,

    I have a setup file for some numerical simulation code written in
    C that looks like this:

    dt 0.1
    time 0.0
    nupdate 10
    noutput 100
    ntotal 10000
    G 39.476926421373 015
    Sun 1.00000597682
    mplanet 9.5478610404304 18e-4
    3.409530427945 3.635870038323 .03424028779975
    -2.0471057839802 485 2.0178211484578 044 -9.7301939162196 67e-4
    mplanet 2.8558373315055 975e-4
    6.612079829705 6.386934883415 -.1361443021015
    -1.5268292602445 992 1.4623064051166 743 6.1083548297049 97e-3
    mplanet 4.3727316454589 18e-5
    11.16769742623 16.04343604329 .3617849409933
    -1.1927379555398 092 .75633170499553 87 -7.9503135448118 02e-3
    particle
    5.0 0.2 10.5
    0.0 0.0 10.0
    particle
    6.0 0.1 5.5
    0.0 180.0 -10.0

    there can be any number of mplanet and particle enteries in the the
    file.
    As I am working on porting this code to python I am trying to figure out
    an efficient way of parsing such data into the program. I have the
    freedom
    to play around with the setup file(s) which is nice. I have played
    around with
    cfgparse, which I like, and made a file like this

    [DEFAULT]
    dt = 0.1
    time = 0.0

    and so on, but that does not give me a good way of parsing in the
    list of
    mplanets and particles. I can write my own little parser to handle a
    file
    that contains only the mplanets or particles, but I am wondering if
    there are
    any parsers out there that will handle this type of data? Or is there a
    way to organize my setup file that would simplify the input that I am
    missing?

    Cheers
    Tommy
  • James Stroud

    #2
    Re: Parsing a setup file

    Tommy Grav wrote:
    Hi,
    >
    I have a setup file for some numerical simulation code written in C
    that looks like this:
    >
    dt 0.1
    time 0.0
    nupdate 10
    noutput 100
    ntotal 10000
    G 39.476926421373 015
    Sun 1.00000597682
    mplanet 9.5478610404304 18e-4
    3.409530427945 3.635870038323 .03424028779975
    -2.0471057839802 485 2.0178211484578 044 -9.7301939162196 67e-4
    mplanet 2.8558373315055 975e-4
    6.612079829705 6.386934883415 -.1361443021015
    -1.5268292602445 992 1.4623064051166 743 6.1083548297049 97e-3
    mplanet 4.3727316454589 18e-5
    11.16769742623 16.04343604329 .3617849409933
    -1.1927379555398 092 .75633170499553 87 -7.9503135448118 02e-3
    particle
    5.0 0.2 10.5
    0.0 0.0 10.0
    particle
    6.0 0.1 5.5
    0.0 180.0 -10.0
    >
    there can be any number of mplanet and particle enteries in the the file.
    As I am working on porting this code to python I am trying to figure out
    an efficient way of parsing such data into the program. I have the freedom
    to play around with the setup file(s) which is nice. I have played
    around with
    cfgparse, which I like, and made a file like this
    >
    [DEFAULT]
    dt = 0.1
    time = 0.0
    >
    and so on, but that does not give me a good way of parsing in the list of
    mplanets and particles. I can write my own little parser to handle a file
    that contains only the mplanets or particles, but I am wondering if
    there are
    any parsers out there that will handle this type of data? Or is there a
    way to organize my setup file that would simplify the input that I am
    missing?
    >
    Cheers
    Tommy
    pyaml is good for this kind of thing. No parser needed.


    Also, you may be mistaken about ConfigParser, you just need
    to indent line continuations (and who says the syntactical meaning of
    whitespace is not a good thing?). E.g.:


    euler 6% cat test.conf
    [stuff]
    mplanet = 4.3727316454589 18e-5
    11.16769742623 16.04343604329 .3617849409933
    -1.1927379555398 092 .75633170499553 87 -7.9503135448118 02e-3



    pyfrom ConfigParser import ConfigParser
    pyc = ConfigParser()
    pyc.read('test. conf')
    ['test.conf']
    pyc.items('stuf f')

    [('mplanet',
    '4.372731645458 918e-5\n11.167697426 23 16.04343604329
    ..3617849409933 \n-1.1927379555398 092 .75633170499553 87
    -7.9503135448118 02e-3')]

    James

    Comment

    Working...