Using ConfigParse

Discussion in 'Python' started by Zunbeltz Izaola, Jun 8, 2004.

  1. Hi to all !

    I've subclassed ConfigParse in the folowing way.

    class DifracConfigParser(ConfigParser):
    def __init__(self, defaults=None):
    ConfigParser.__init__(self, defaults)
    self.readfp(open('defaultdifrac.cfg'))

    def save(self):
    print "Enter save"
    self.write(open('defaultdifrac.cfg','w'))
    self.read(open('defaultdifrac.cfg'))
    print "OUt save"


    I use the values from the defaultdifrac.cfg file in my dialogs (using
    wxpython) and change them via validators. After closing a dialog that
    changes the values (after validating) i call de save method, but the
    file is not changed.

    Any Idea?

    Thanks in advance

    Zunbeltz


    --
    Zunbeltz Izaola Azkona | wmbizazz at lg dot ehu
    dotes
    Materia Kondentsatuaren Fisika Saila |
    Zientzia eta Teknologia Fakultatea | Phone: 34946015326
    Euskal Herriko Unibertsitatea |
    PK 644 | Fax: 34 944648500
    48080 Bilbo (SPAIN) |
     
    Zunbeltz Izaola, Jun 8, 2004
    #1
    1. Advertising

  2. Zunbeltz Izaola

    Peter Otten Guest

    Zunbeltz Izaola wrote:

    > def save(self):
    > print "Enter save"
    > self.write(open('defaultdifrac.cfg','w'))
    > self.read(open('defaultdifrac.cfg'))
    > print "OUt save"


    I would explicitly close the file before trying to read it.

    >>> def save(fn="tmp.txt", s="so what", close=True):

    .... f = file(fn, "w")
    .... f.write(s)
    .... if close: f.close()
    .... return file(fn).read()
    ....
    >>> save()

    'so what'
    >>> save(s="another")

    'another'
    >>> save(s="yet another", close=False)

    ''

    Peter
     
    Peter Otten, Jun 8, 2004
    #2
    1. Advertising

  3. Peter Otten <> writes:

    > Zunbeltz Izaola wrote:
    >
    > > def save(self):
    > > print "Enter save"
    > > self.write(open('defaultdifrac.cfg','w'))
    > > self.read(open('defaultdifrac.cfg'))
    > > print "OUt save"

    >
    > I would explicitly close the file before trying to read it.
    >
    > >>> def save(fn="tmp.txt", s="so what", close=True):

    > ... f = file(fn, "w")
    > ... f.write(s)
    > ... if close: f.close()
    > ... return file(fn).read()
    > ...
    > >>> save()

    > 'so what'
    > >>> save(s="another")

    > 'another'
    > >>> save(s="yet another", close=False)

    > ''


    Thanks for the sugestion but it doen't work, i don't know wy
    CofigParser doesn't write the file

    Zunbeltz

    >



    > Peter
    >


    --
    Zunbeltz Izaola Azkona | wmbizazz at lg dot ehu
    dotes
    Materia Kondentsatuaren Fisika Saila |
    Zientzia eta Teknologia Fakultatea | Phone: 34946015326
    Euskal Herriko Unibertsitatea |
    PK 644 | Fax: 34 944648500
    48080 Bilbo (SPAIN) |
     
    Zunbeltz Izaola, Jun 8, 2004
    #3
  4. Zunbeltz Izaola

    Peter Otten Guest

    Zunbeltz Izaola wrote:

    > Peter Otten <> writes:
    >
    >> Zunbeltz Izaola wrote:
    >>
    >> > def save(self):
    >> > print "Enter save"
    >> > self.write(open('defaultdifrac.cfg','w'))
    >> > self.read(open('defaultdifrac.cfg'))
    >> > print "OUt save"

    >>
    >> I would explicitly close the file before trying to read it.
    >>
    >> >>> def save(fn="tmp.txt", s="so what", close=True):

    >> ... f = file(fn, "w")
    >> ... f.write(s)
    >> ... if close: f.close()
    >> ... return file(fn).read()
    >> ...
    >> >>> save()

    >> 'so what'
    >> >>> save(s="another")

    >> 'another'
    >> >>> save(s="yet another", close=False)

    >> ''

    >
    > Thanks for the sugestion but it doen't work, i don't know wy
    > CofigParser doesn't write the file


    I looked it up in the source: the read() method expects a filename or a
    sequence of filenames. Incidentally that means, that every line in your
    config-file is interpreted as a filename. It doesn't matter that none of
    these files exist, as all IOErrors are silenced by the method.

    self.readfp(open('defaultdifrac.cfg'))

    or

    self.read("defaultdifrac.cfg")

    in your save() method instead of self.read(open('defaultdifrac.cfg')) should
    fix the problem. That would of course mean that ConfigParser *does* write
    the file, only doesn't read it back. Can you confirm that?

    Peter
     
    Peter Otten, Jun 8, 2004
    #4
  5. Peter Otten <> wrote in message news:<ca4dct$1t2$06$-online.com>...

    > I looked it up in the source: the read() method expects a filename or a
    > sequence of filenames. Incidentally that means, that every line in your
    > config-file is interpreted as a filename. It doesn't matter that none of
    > these files exist, as all IOErrors are silenced by the method.
    >


    I too looked at the source. I understand why it ignores IOErrors
    (though I'm not convinced it's a good idea), but I think I see a
    problem with the ConfigParse module. I would think it should raise
    some error if it can't find any file at all in the list you provide.
    It seems to me that would be more useful than having an empty object.

    Of course, it's probably not something we could change now...
     
    A. Lloyd Flanagan, Jun 8, 2004
    #5
  6. Thanks Peter and Lloyd. But the problem was on my code no on
    ConfigParse. I was working in a module I think it was finished but the
    problem was that after updating the values of the variables I did not
    call the set funtion of the ConfigParse object.

    Apologizes for bothering with silly questions :-(

    Thanks again

    Zunbeltz



    --
    Zunbeltz Izaola Azkona | wmbizazz at lg dot ehu
    dotes
    Materia Kondentsatuaren Fisika Saila |
    Zientzia eta Teknologia Fakultatea | Phone: 34946015326
    Euskal Herriko Unibertsitatea |
    PK 644 | Fax: 34 944648500
    48080 Bilbo (SPAIN) |
     
    Zunbeltz Izaola, Jun 9, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William F. Robertson, Jr.
    Replies:
    1
    Views:
    999
    Kathy Burke
    Jun 25, 2003
  2. pete
    Replies:
    1
    Views:
    3,012
    Jon Yates
    Aug 29, 2003
  3. timmso
    Replies:
    1
    Views:
    575
    Alex Papadimoulis
    Dec 12, 2003
  4. Replies:
    2
    Views:
    582
  5. Neal Becker

    which configparse?

    Neal Becker, Dec 6, 2007, in forum: Python
    Replies:
    1
    Views:
    295
    Sion Arrowsmith
    Dec 7, 2007
Loading...

Share This Page