Reviewers: faassen, Description: See if this does what you want.
Please review this at http://codereview.appspot.com/63141 Affected files: datadict.py datadict_test.py Index: datadict_test.py =================================================================== --- datadict_test.py (revision 0) +++ datadict_test.py (revision 0) @@ -0,0 +1,47 @@ +#!/usr/bin/python -S +""" +datadict_test.py: Tests for datadict.py +""" + +__author__ = 'Andy Chu' + + +import os +import sys + +if __name__ == '__main__': + # for jsontemplate and pan, respectively + sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) + sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../..')) + +from jsontemplate import datadict # module under test +from pan.test import testy + + +class DataDictTest(testy.Test): + + def testAddIndex(self): + d = [ {'foo': 1}, + {'spam': 'eggs'}, + ] + datadict.AddIndex(d) + print d + + d = { 'bar': [ + {'foo': 1}, + {'spam': 'eggs'}, + ] } + datadict.AddIndex(d) + print d + + d = { 'bar': [ + {'foo': 1}, + {'spam': [1, 2, 3]}, + {'spam': [{'zero': None}, {'one': None}]}, + ] } + datadict.AddIndex(d) + print d + + +if __name__ == '__main__': + testy.RunThisModule() Property changes on: datadict_test.py ___________________________________________________________________ Added: svn:executable + * Added: svn:eol-style + LF Index: datadict.py =================================================================== --- datadict.py (revision 0) +++ datadict.py (revision 0) @@ -0,0 +1,19 @@ +#!/usr/bin/python -S +""" +datadict.py +""" + +__author__ = 'Andy Chu' + + +def AddIndex(node, index=None): + """Recursively add the current index in all data dictionaries.""" + + if isinstance(node, list): + for i, item in enumerate(node): + AddIndex(item, index=i) + elif isinstance(node, dict): + if index is not None: + node['index'] = index + for key in node: + AddIndex(node[key]) Property changes on: datadict.py ___________________________________________________________________ Added: svn:executable + * Added: svn:eol-style + LF --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "JSON Template" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/json-template?hl=en -~----------~----~----~----~------~----~------~--~---
