PyYAML replace dash in keys with underscore
Question: I would like to map directly some configuration parameters from YAML into
Python argument names. Just wondering if there is a way without writing extra-
code (to modify keys afterwards) to let YAML parser replace dash '-' in a key
with an underscore '_'.

    
    
    some-parameter: xyz
    some-other-parameter: 123
    

Should become when parsed with PyYAML (or may be other lib) a dictionary with
values:

    
    
    {'some_parameter': 'xyz', 'some_other_parameter': 123}
    

Than I can pass the dictionary to a function as named parameters:

    
    
    foo(**parsed_data)
    

I know I can iterate through the keys afterwards and modify their values, but
I don't want to do that :)


Answer: At least for your stated case, you don't need to transform the keys. Given:

    
    
    import pprint
    
    def foo(**kwargs):
        print 'KWARGS:', pprint.pformat(kwargs)
    

If you set:

    
    
    values = {
        'some-parameter': 'xyz',
        'some-other-parameter': 123,
    }
    

And then call:

    
    
    foo(**values)
    

You get:

    
    
    KWARGS: {'some-other-parameter': 123, 'some-parameter': 'xyz'}
    

If you goal is is actually to call a function like this:

    
    
    def foo(some_parameter=None, some_other_parameter=None):
        pass
    

Then sure, you would need to map the key names. But you could just do this:

    
    
    foo(**dict((k.replace('-','_'),v) for k,v in values.items()))
    

