app/soc/logic/dicts.py
changeset 1899 c841800f3727
parent 1842 391b89d04bdd
child 2073 6eb9b4652c80
equal deleted inserted replaced
1898:7f18e83a129f 1899:c841800f3727
   180     # Update the result for the next iteration
   180     # Update the result for the next iteration
   181     result = tmpresult
   181     result = tmpresult
   182 
   182 
   183   return result
   183   return result
   184 
   184 
       
   185 
   185 def groupby(target, group_key):
   186 def groupby(target, group_key):
   186   """Groups a list of dictionaries by group_key.
   187   """Groups a list of dictionaries by group_key.
   187   """
   188   """
   188 
   189 
   189   result = {}
   190   result = {}
   196 
   197 
   197     result[key_value].append(value)
   198     result[key_value].append(value)
   198 
   199 
   199   return result
   200   return result
   200 
   201 
       
   202 
   201 def groupDictBy(target, key, new_key=None):
   203 def groupDictBy(target, key, new_key=None):
   202   """Groups a dictionary by a key.
   204   """Groups a dictionary by a key.
   203   """
   205   """
   204 
   206 
   205   if not new_key:
   207   if not new_key:
   206     new_key = key
   208     new_key = key
   207 
   209 
   208   result = ((k, v[new_key]) for k, v in target.iteritems() if v[key])
   210   result = ((k, v[new_key]) for k, v in target.iteritems() if v[key])
   209   return dict(result)
   211   return dict(result)
   210 
   212 
       
   213 
   211 def identity(target):
   214 def identity(target):
   212   """Returns a dictionary with the values equal to the keys.
   215   """Returns a dictionary with the values equal to the keys.
   213   """
   216   """
   214 
   217 
   215   result = [(i, i) for i in target]
   218   result = ((i, i) for i in target)
   216   return dict(result)
   219   return dict(result)
       
   220 
       
   221 
       
   222 def format(target, input):
       
   223   """Returns a dictionary with the values formatted with input.
       
   224   """
       
   225 
       
   226   result = ((k, v % input) for k, v in target.iteritems())
       
   227   return dict(result)
       
   228 
       
   229 
       
   230 def containsAll(target, keys):
       
   231   """Returns true iff target contains all keys.
       
   232   """
       
   233 
       
   234   result = ((i in target) for i in keys)
       
   235   return all(result)