def _allocations_dict(allocations, key_fetcher, resource_provider=None):
 allocation_data = collections.defaultdict(dict)
 for allocation in allocations:
  key = key_fetcher(allocation)
  if ('resources' not in allocation_data[key]):
   allocation_data[key]['resources'] = {}
  resource_class = allocation.resource_class
  allocation_data[key]['resources'][resource_class] = allocation.used
  if (not resource_provider):
   generation = allocation.resource_provider.generation
   allocation_data[key]['generation'] = generation
 result = {'allocations': allocation_data}
 if resource_provider:
  result['resource_provider_generation'] = resource_provider.generation
 return result

def _allocations_dict ( allocations , key_fetcher , resource_provider = None ) : allocation_data = collections . defaultdict ( dict ) for allocation in allocations : key = key_fetcher ( allocation ) if ( 'resources' not in allocation_data [ key ] ) : allocation_data [ key ] [ 'resources' ] = { } resource_class = allocation . resource_class allocation_data [ key ] [ 'resources' ] [ resource_class ] = allocation . used if ( not resource_provider ) : generation = allocation . resource_provider . generation allocation_data [ key ] [ 'generation' ] = generation result = { 'allocations' : allocation_data } if resource_provider : result [ 'resource_provider_generation' ] = resource_provider . generation return result
turn allocations into a dict of resources keyed by key_fetcher .