Last active
December 22, 2015 11:59
-
-
Save jstitch/6469151 to your computer and use it in GitHub Desktop.
Method to serialize the contents of data structures in Python
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
def serializeFields(data): | |
try: | |
it = iter(data) | |
except TypeError as terr: | |
return unicode(data) | |
if type(it) == type(iter([])): | |
l = [] | |
for e in it: | |
l.append(serializeFields(e)) | |
return l | |
elif type(it) == type(iter({})): | |
d = {} | |
for k in it: | |
d[k] = serializeFields(data[k]) | |
return d | |
# elif ... tuples? sets? | |
return unicode(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment