MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / config_dict

Function config_dict

Python/Turtle.py:69–96  ·  view source on GitHub ↗

Convert content of config-file into dictionary.

(filename)

Source from the content-addressed store, hash-verified

67 }
68
69def config_dict(filename):
70 """Convert content of config-file into dictionary."""
71 with open(filename, "r") as f:
72 cfglines = f.readlines()
73 cfgdict = {}
74 for line in cfglines:
75 line = line.strip()
76 if not line or line.startswith("#"):
77 continue
78 try:
79 key, value = line.split("=")
80 except ValueError:
81 print("Bad line in config-file %s:\n%s" % (filename,line))
82 continue
83 key = key.strip()
84 value = value.strip()
85 if value in ["True", "False", "None", "''", '""']:
86 value = eval(value)
87 else:
88 try:
89 if "." in value:
90 value = float(value)
91 else:
92 value = int(value)
93 except ValueError:
94 pass # value need not be converted
95 cfgdict[key] = value
96 return cfgdict
97
98def readconfig(cfgdict):
99 """Read config-files, change configuration-dict accordingly.

Callers 1

readconfigFunction · 0.85

Calls 1

printFunction · 0.50

Tested by

no test coverage detected