config in python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rakeshr3
    New Member
    • Mar 2007
    • 1

    #1

    config in python

    i am confused with config module in python. can anyone help me to solve the problem. why config module is used, what are the features of config module.

    the following is the syntax which i did nt understood in my project.
    Code:
    options = dict(self.config.options('perforce'))
    regards,
    Rakesh
    Last edited by bartonc; Mar 28 '07, 10:40 AM. Reason: added [code][/code] tags
  • ghostdog74
    Recognized Expert Contributor
    • Apr 2006
    • 511

    #2
    Originally posted by rakeshr3
    i am confused with config module in python. can anyone help me to solve the problem. why config module is used, what are the features of config module.

    the following is the syntax which i did nt understood in my project.

    options = dict(self.confi g.options('perf orce'))
    regards,
    Rakesh
    did you download this module from somewhere? there is a module called cfgparse that comes with Python distribution. you can use that. anyways, options are things like

    Code:
    ...
    [SECTION]
    options = value
    ....

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by rakeshr3
      i am confused with config module in python. can anyone help me to solve the problem. why config module is used, what are the features of config module.

      the following is the syntax which i did nt understood in my project.

      options = dict(self.confi g.options('perf orce'))
      regards,
      Rakesh
      My friend, ghostdog74 is correct. The module is called configparser.

      What you have is a RawConfigParser object created by:
      Code:
      config = ConfigParser.ConfigParser()
      These objects are used to retrieve startup values (like window position, font selection, etc) from a file with a certain format. The idea (and the format, I think) come from Unix. On windows, the operating system keeps a database for us called The Windows Registry for this purpose.

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by rakeshr3
        i am confused with config module in python. can anyone help me to solve the problem. why config module is used, what are the features of config module.

        the following is the syntax which i did nt understood in my project.
        Code:
        options = dict(self.config.options('perforce'))
        regards,
        Rakesh
        The Python Manual says

        "An application which requires initial values to be loaded from a file should load the required file or files using readfp() before calling read() for any optional files: "
        Code:
        import ConfigParser, os
        
        config = ConfigParser.ConfigParser()
        config.readfp(open('defaults.cfg'))
        config.read(['site.cfg', os.path.expanduser('~/.myapp.cfg')])
        So, in a file (say, '~/.myapp.cfg') there is a SECTION called "perforce" with option/value pairs that get read in to a dictionary by the syntax that you have given. Hope that helps.

        Comment

        Working...