#NEWLINE# def correlate_objects( objects , attr ) : #NEWLINE# #INDENT# 'Correlate several objects under one dict.\n\n    If you have several objects each with a \'name\' attribute, this\n    puts them in a dict keyed by name.\n\n    Example::\n\n        >>> class Flintstone(DumbObject):\n        ...    pass\n        ...\n        >>> fred = Flintstone(name="Fred", age=41)\n        >>> barney = Flintstone(name="Barney", age=31)\n        >>> flintstones = correlate_objects([fred, barney], "name")\n        >>> sorted(flintstones.keys())\n        [\'Barney\', \'Fred\']\n        >>> flintstones["Barney"].age\n        31\n\n    If you\'re having trouble spelling this method correctly, remember:\n    "relate" has one \'l\'.  The \'r\' is doubled because it occurs after a prefix.\n    Thus "correlate".\n    ' #NEWLINE# ret = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# i = 0 #NEWLINE# for obj in objects : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# my_key = getattr ( obj , attr ) #NEWLINE# #UNINDENT# except AttributeError : #NEWLINE# #INDENT# msg = "'%s' object at 'objects[%d]' contains no attribute '%s'" #NEWLINE# tup = ( type ( obj ) . __name__ , i , attr ) #NEWLINE# raise AttributeError ( ( msg % tup ) ) #NEWLINE# #UNINDENT# ret [ my_key ] = obj #NEWLINE# i += 1 #NEWLINE# #UNINDENT# return ret