Skip to content

Instantly share code, notes, and snippets.

@akopichin
Created March 23, 2012 09:09
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 akopichin/2168749 to your computer and use it in GitHub Desktop.
Save akopichin/2168749 to your computer and use it in GitHub Desktop.
//set value to multidementional dict with lists/dicts inside
def set_val(self, path='', new_val=None, *args):
if '/' in path:
chunks = path.split('/')
chunks.reverse()
val = self.data
while (len(chunks)>1):
chunk = chunks.pop()
cnt = len(chunks)
if chunk.isdigit():
val = val[int(chunk)]
else:
val = val[chunk]
if chunks[0].isdigit():
val[int(chunks[0])] = new_val
else:
val[chunks[0]] = new_val
return True
try:
self.data[path] = new_val
return True
except (KeyError, IndexError):
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment