Parse yaml directories as key=value pairs#273
Parse yaml directories as key=value pairs#273katharinahafner wants to merge 2 commits intobw2:masterfrom
Conversation
|
Minimal example: # config.yml
vars:
a: [1, 2, 3]
b: "foo"
c: "bar"Add an action to parse dict-string to dict import argparse
from configargparse import ArgParser, YAMLConfigFileParser
class StoreDict(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
import json
try:
my_dict = json.loads(values)
except json.JSONDecodeError:
my_dict = json.loads(values.replace("'",'"'))
setattr(namespace, self.dest, my_dict)
p = ArgParser(config_file_parser_class=YAMLConfigFileParser)
p.add('-c', '--config_file', default='config.yml', is_config_file=True)
p.add('--vars', dest="vars", action=StoreDict)
args = p.parse_args()
print(args.vars) |
|
If other users would like to see support for dictionaries, please post your views here or in #258 |
|
I see the original request in #258 for this came from Snakemake, which is also the project I'm personally interested in. I don't think this PR is the right fix as it stands. In the PR we have: So the entire Python dict is quoted into the argument string, which is equivalent to calling Which replaces the quotes then treats the Python-quoted dict literal as JSON. This sometimes works, sometimes not. I think for Snakemake what we are really after is: Snakemake already has internal logic to turn these lists of strings back into dicts, and as long as you assume (or check!) that no dict key contains |
|
I've written some demonstration code showing how this can now be implemented with minimal subclassing of the config parser, if a new See scenario 3 in this code: If there is enough enthusiasm for this change, hopefully it can be accepted into the next release of the module. |
Fix #258