ConfigParser: Can I read(ConfigParser.get()) a configuration file anduse it to call a funciton?

Discussion in 'Python' started by jamitwidme@gmail.com, Jun 26, 2008.

  1. Guest

    Hello. I am a novice programmer and have a question

    I have a configuration file(configuration.cfg)
    I read this from reading.py using ConfigParser
    When I use ConfigParser.get() function, it returns a string.
    I want to call a function that has the same name as the string from
    the configuration file.


    configuration.cfg
    ---------------------------------------
    [1234]
    title: abcd
    function: efgh
    ---------------------------------------


    reading.py
    --------------------------------------------------------
    import ConfigParser

    def efgh():
    print 'blah'

    config = ConfigParser.ConfigParser()
    config.read('configuration.cfg')

    fcn = config.get('1234','function')
    type(fcn)
    print fcn
    --------------------------------------------------------

    <type 'str'>
    efgh


    Is there any way to call efgh() ?
    One way I know is using if statement
    if fcn == 'efgh':
    efgh()

    But I am going to have many functions to call, so I want to avoid
    this.


    Thank you for your help
     
    , Jun 26, 2008
    #1
    1. Advertising

  2. Re: ConfigParser: Can I read(ConfigParser.get()) a configuration fileand use it to call a funciton?

    Le Thursday 26 June 2008 16:41:27 , vous avez écrit :
    > Hello. I am a novice programmer and have a question
    >
    > I have a configuration file(configuration.cfg)
    > I read this from reading.py using ConfigParser
    > When I use ConfigParser.get() function, it returns a string.
    > I want to call a function that has the same name as the string from
    > the configuration file.
    >
    >


    You can find the function in the global dictionary (returned by globals()):

    globs = globals()
    func_name = config.read('1234', 'function')
    func = globs[func_name]

    # and then call it
    func()

    But a safer way would be to use a class with some static methods:

    class Functions (object) :

    @staticmethod
    def efgh () :
    blah blah...

    and then find the function in the class dict:

    func = getattr(Functions, func_name)
    func()

    this way you can restrict the set of functions the user can give, excluding
    those which are not supposed to be called this way.

    --
    Cédric Lucantis
     
    Cédric Lucantis, Jun 26, 2008
    #2
    1. Advertising

  3. Matimus Guest

    Re: ConfigParser: Can I read(ConfigParser.get()) a configuration fileand use it to call a funciton?

    On Jun 26, 7:41 am, wrote:
    > Hello. I am a novice programmer and have a question
    >
    > I have a configuration file(configuration.cfg)
    > I read this from reading.py using ConfigParser
    > When I use ConfigParser.get() function, it returns a string.
    > I want to call a function that has the same name as the string from
    > the configuration file.
    >
    > configuration.cfg
    > ---------------------------------------
    > [1234]
    > title: abcd
    > function: efgh
    > ---------------------------------------
    >
    > reading.py
    > --------------------------------------------------------
    > import ConfigParser
    >
    > def efgh():
    >    print 'blah'
    >
    > config = ConfigParser.ConfigParser()
    > config.read('configuration.cfg')
    >
    > fcn = config.get('1234','function')
    > type(fcn)
    > print fcn
    > --------------------------------------------------------
    >
    > <type 'str'>
    > efgh
    >
    > Is there any way to call efgh() ?
    > One way I know is using if statement
    > if fcn == 'efgh':
    >    efgh()
    >
    > But I am going to have many functions to call, so I want to avoid
    > this.
    >
    > Thank you for your help


    Something like this:
    globals()[fcn]()
     
    Matimus, Jun 26, 2008
    #3
  4. Guest

    Re: ConfigParser: Can I read(ConfigParser.get()) a configuration fileand use it to call a funciton?

    Thank you for the answers.
    Now I understood how to call a function, let me ask you another
    question.

    configuration.cfg
    ---------------------------------------
    [1234]
    title: abcd
    function: efgh
    ---------------------------------------

    reading.py
    --------------------------------------------------------
    import ConfigParser

    class Functions:
    def efgh(self):
    print 'blah'

    fcn = Functions()

    config = ConfigParser.ConfigParser()
    config.read('configuration.cfg')
    title = config.get('1234','title') # number 1
    function_name = config.get('1234','function')

    title = getattr(fcn, function_name)
    title()
    --------------------------------------------------------

    instead of assigning string value('abcd') to title at number 1

    I want to assign this function(fcn.efgh()) to abcd and make abcd a
    FunctionType.
    so later on, I want to call it by abcd(), not title().
    The reason is I will have a loop reading from configuration file, so I
    need to have different names for each function.
    abcd is a string I read got it from config.get('1234','title')

    Thanks again.
     
    , Jun 26, 2008
    #4
    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. Mark Turney
    Replies:
    3
    Views:
    940
    Kevin Saff
    Jun 3, 2004
  2. Affan Syed
    Replies:
    6
    Views:
    8,291
    =?utf-8?Q?Ali_=C3=87ehreli?=
    Nov 30, 2004
  3. oyster
    Replies:
    16
    Views:
    500
    Cameron Laird
    Feb 19, 2009
  4. Lie Ryan
    Replies:
    5
    Views:
    326
  5. Pierre Barbier de Reuille

    Can't get irb to read its configuration file

    Pierre Barbier de Reuille, Sep 9, 2006, in forum: Ruby
    Replies:
    2
    Views:
    100
    Pierre Barbier de Reuille
    Sep 9, 2006
Loading...

Share This Page