0

I have written this function that returns a dictionary, I think either by itertools or by just using yeild i can return dictionary but I have never done that in case of a dictionary

def configDb():
    """ Reads git global config file

        Returns:
            config(dict): git config settings
    """
    # Read git config file
    configFile, _ = execGitCommand('git config --list')
    config = {}
    for line in (each for each in configFile.split("\n") if each):
        config[line.split("=")[0]] = line.split("=")[-1]

    return config

how can I make this function act in a way I do not have to call like configDb() but instead just configDb[key] should give me value ?

Ciasto piekarz
  • 7,853
  • 18
  • 101
  • 197
  • 1
    What is the point of a "dictionary generator"? The dictionary is not useful until all items are loaded; just return a dict. – Hugh Bothwell Feb 16 '14 at 07:07

2 Answers2

0

Dictionaries can't be generatored, but you can yield key-value tuples

def configDb():
    """ Reads git global config file

        Returns:
            config(dict): git config settings
    """
    # Read git config file
    configFile, _ = execGitCommand('git config --list')
    config = {}
    for line in (each for each in configFile.split("\n") if each):
        yield line.split("=")[0], line.split("=")[-1]
mhlester
  • 22,781
  • 10
  • 52
  • 75
0

A partial config file is hardly useful, so there doesn't seem to be much point in a "generated dictionary".

def configDb():
    """ Reads git global config file

        Returns:
            config(dict): git config settings
    """
    # Read git config file
    configFile, _ = execGitCommand('git config --list')
    rows = (line.split("=") for line in configFile.splitlines())
    return dict(row for row in rows if len(row)==2)
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99