0

I have a config file like below

....
....
MIN_TOKENS='30 50'  # can be a sequence of integers
STRIDE='2 0'  # can be a sequence of integers
SIMILARITY='1.0 0.95'  # can be a sequence of values <= 1
....
....

I need to edit the parameters to each of the above configurations(if needed) and test if the edited parameter suits my need by running my application. I need to automate this process using python script.

For example: I need to change the config file to something like

....
....
MIN_TOKENS='30 50'  
STRIDE='2'  
SIMILARITY='0.75'  
....
....

and then run my application. However, the parameters should be automatically generated within range and I shouldn't manually feed them. I'm not familiar with python. Could someone tell me how to approach this.

3
  • How are the parameters used? Can you send the values directly to the program which uses them rather than edit the config file? Commented Jun 2, 2017 at 6:08
  • That can't be done Commented Jun 2, 2017 at 6:12
  • can you please explain why? Commented Jun 2, 2017 at 6:33

1 Answer 1

1

yes you can do. your config.ini will be something like below

[Device]
MIN_TOKENS = "34 45"
STRIDE = 45
SIMILARITY = '0.75'

conf.py

from ConfigParser import SafeConfigParser
config_file = "config.ini"
parser = SafeConfigParser()
parser.optionxform = str
parser.read(config_file)

def set_config(section, option, value):
    if parser.has_section(section):
        parser.set(section, option , value)
        with open(config_file, "w") as conf_file:
            parser.write(conf_file)
            return True
set_config('Device','MIN_TOKENS','"34 20"')

output

[Device]
MIN_TOKENS = "34 20"
STRIDE = 45
SIMILARITY = '0.75'
Sign up to request clarification or add additional context in comments.

5 Comments

The option and value shouldn't be set manually. It is like generating automatic test case and test it
yes sure, you can do it, if option exists then it will update else will create new. but the section name should be there
@Jab this example uses hard coded values, but you can easily modify it to use a variable instead.
@Arun - what if my config file doesn't have a named section?
It is like club attributes under a tag, you can have any name as section

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.