#NEWLINE# def ordered_items( dic , key_order , other_keys = True , default = NotGiven ) : #NEWLINE# #INDENT# 'Like ``dict.iteritems()`` but with a specified key order.\n\n    Arguments:\n\n    * ``dic`` is any mapping.\n    * ``key_order`` is a list of keys.  Items will be yielded in this order.\n    * ``other_keys`` is a boolean.\n    * ``default`` is a value returned if the key is not in the dict.\n\n    This yields the items listed in ``key_order``.  If a key does not exist\n    in the dict, yield the default value if specified, otherwise skip the\n    missing key.  Afterwards, if ``other_keys`` is true, yield the remaining\n    items in an arbitrary order.\n\n    Usage::\n\n        >>> dic = {"To": "you", "From": "me", "Date": "2008/1/4", "Subject": "X"}\n        >>> dic["received"] = "..."\n        >>> order = ["From", "To", "Subject"]\n        >>> list(ordered_items(dic, order, False))\n        [(\'From\', \'me\'), (\'To\', \'you\'), (\'Subject\', \'X\')]\n    ' #NEWLINE# d = dict ( dic ) #NEWLINE# for key in key_order : #NEWLINE# #INDENT# if ( key in d ) : #NEWLINE# #INDENT# ( yield ( key , d . pop ( key ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( default is not NotGiven ) : #NEWLINE# #INDENT# ( yield ( key , default ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if other_keys : #NEWLINE# #INDENT# for ( key , value ) in iteritems ( d ) : #NEWLINE# #INDENT# ( yield ( key_order , value ) )