0

My apologies for the crappy headline. If I were able to frame my problem properly I would have used google ;)

I found an piece of python code able to parse ini files into a python dict called "store":

#!/usr/bin/env python

from ConfigParser import SafeConfigParser

def read(file, store):

    def parse_maybe(section):
        if not confp.has_section(section):
            return False

        if (section == "Main"):
            for left, right in confp.items(section):
                store[left] = right.format(**store)
        return True

    confp = SafeConfigParser()
    confp.read(file)
    parse_maybe("Main")

store = {}
store["basedir"] = "/path/to/somewhere"
read("foo.ini", store)

The ini files may include declarations with placeholders, for instance:

[Main]
output = {basedir}/somename.txt

When running the code, {basedir} gets replaced by "/path/to/somewhere" already defined in store. I guess this magic comes from this line of code:

store[left] = right.format(**store)

I understand what the code does. But I do not understand how this works. What is this ** operator doing with the dictionary? A pointer to a tutorial, etc. would be highly appreciated.

drscheme
  • 19
  • 1
  • 7
  • If that doesn't solve your query then a google search for "python double star" returned lots of other resources – Sayse Dec 22 '15 at 09:21
  • 3
    **store unpacks a dictionary. If a function takes keyword arguments, and the keyword-value pairs are in a dictionary, you can pass these pairs into the functions arguments using ** – texasflood Dec 22 '15 at 09:23
  • @texasflood: that was the answer. Thank you. – drscheme Dec 22 '15 at 09:56

1 Answers1

0

The answer to my question is twofold:

1) I did not know that this is possible with format:

print "{a} is {b}".format(a="Python", b="great")

Python is great

2) Essentially the ** Operator unpacks a dictionary:

dict = {"a": "Python", "b": "great"}
print "{a} is {b}".format(**dict)

Python is great

drscheme
  • 19
  • 1
  • 7