Use this block here:
````
if hasattr(d, 'iteritems):
   d = d.iteritems()
elif hasattr(d, 'items'):
  d = d.items()
````