#NEWLINE# def dictlike_iteritems( dictlike ) : #NEWLINE# #INDENT# 'Return a (key, value) iterator for almost any dict-like object.' #NEWLINE# if compat . py3k : #NEWLINE# #INDENT# if hasattr ( dictlike , 'items' ) : #NEWLINE# #INDENT# return list ( dictlike . items ( ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if hasattr ( dictlike , 'iteritems' ) : #NEWLINE# #INDENT# return dictlike . iteritems ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if hasattr ( dictlike , 'items' ) : #NEWLINE# #INDENT# return iter ( dictlike . items ( ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# getter = getattr ( dictlike , '__getitem__' , getattr ( dictlike , 'get' , None ) ) #NEWLINE# if ( getter is None ) : #NEWLINE# #INDENT# raise TypeError ( ( "Object '%r' is not dict-like" % dictlike ) ) #NEWLINE# #UNINDENT# if hasattr ( dictlike , 'iterkeys' ) : #NEWLINE# #INDENT# def iterator( ) : #NEWLINE# #INDENT# for key in dictlike . iterkeys ( ) : #NEWLINE# #INDENT# ( yield ( key , getter ( key ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# return iterator ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if hasattr ( dictlike , 'keys' ) : #NEWLINE# #INDENT# return iter ( ( ( key , getter ( key ) ) for key in dictlike . keys ( ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise TypeError ( ( "Object '%r' is not dict-like" % dictlike ) )