Skip to content

Instantly share code, notes, and snippets.

@jimhorng
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimhorng/e8336178ad6acbe36d76 to your computer and use it in GitHub Desktop.
Save jimhorng/e8336178ad6acbe36d76 to your computer and use it in GitHub Desktop.
dot format to dict
def gen_dict(key_tree, value):
if len(key_tree) == 1:
return "%s = \"%s\";" %(key_tree[0], value)
else:
return (gen_dict(key_tree[1:], value) +
"\n" + "{key1} = {{}};\n{key1}['{key2}'] = {key2};".format(key1=key_tree[0],
key2=key_tree[1]))
print gen_dict(key_tree="a.b.c.d".split("."), value="haha")
# input: a.b.c.d = "haha"
# output:
# d = "haha";
# c = {};
# c['d']=d;
# b = {};
# b['c']=c;
# a = {};
# a['b']=b;
# or a:{ b:{ c:{ d: "haha" } } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment