Created
March 23, 2012 09:09
-
-
Save akopichin/2168749 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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