Created
October 9, 2011 13:05
-
-
Save kamawanu/1273661 to your computer and use it in GitHub Desktop.
dict2list
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
#!python | |
def dict2list(dict1): | |
assert isinstance(dict1,dict) | |
indexes = sorted(dict1.keys()) | |
## try: | |
[ int(xx) for xx in indexes ] # validation: all keys is integer | |
assert indexes[0] == 0 and indexes[-1] == len(indexes)-1, "can not convert to list" | |
return [ dict1[xx] for xx in indexes ] | |
def unittester(dict1): | |
import logging | |
try: | |
outp = dict2list(dict1) | |
logging.warn( (dict1,outp) ) | |
except Exception,err: | |
logging.warn( (dict1,err) ) | |
__all__ = [ | |
dict2list.__name__ | |
] | |
if __name__ == '__main__': | |
unittester( { 0:1, 1:2, 2:3, 3:4 } ) | |
unittester( { 0:1, 1:2, 3:4 } ) | |
unittester( { "A":1, 1:2, 2:4 } ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment