def _turn_iterable_to_mapping_from_attribute(attribute, obj, optional=False):
 if optional:
  if (obj is None):
   return None
 if isinstance(obj, Mapping):
  return obj
 return {getattr(a, attribute): a for a in obj}

def _turn_iterable_to_mapping_from_attribute ( attribute , obj , optional = False ) : if optional : if ( obj is None ) : return None if isinstance ( obj , Mapping ) : return obj return { getattr ( a , attribute ) : a for a in obj }
given obj which is either something that is an instance of mapping or an iterable .