app/soc/logic/dicts.py
changeset 484 6364f8b0656b
parent 410 2af7f84f4fc7
child 486 ec6b50f48d3b
equal deleted inserted replaced
483:70f21691b0e1 484:6364f8b0656b
    17 """Logic related to handling dictionaries
    17 """Logic related to handling dictionaries
    18 """
    18 """
    19 
    19 
    20 __authors__ = [
    20 __authors__ = [
    21   '"Sverre Rabbelier" <sverre@rabbelier.nl>',
    21   '"Sverre Rabbelier" <sverre@rabbelier.nl>',
       
    22   '"Lennard de Rijk" <ljvderijk@gmail.com>',
    22   ]
    23   ]
    23 
    24 
    24 
    25 
       
    26 def filter(target, keys):
       
    27   """Filters a dictonary to only allow items with the given keys.
       
    28   
       
    29   Args:
       
    30     target: The dictionary that is to be filtered
       
    31     keys: The list with keys to filter the dictionary on
       
    32   
       
    33   Returns:
       
    34     A dictionary that only contains the (key,value) from target that have their key in keys
       
    35   """
       
    36   result = {}
       
    37   
       
    38   for key, value in target.iteritems():
       
    39     if key in keys:
       
    40       result[key] = value
       
    41   
       
    42   return result
       
    43 
       
    44 
    25 def merge(target, updates):
    45 def merge(target, updates):
    26   """Like the builtin 'update' method but does not overwrite existing values
    46   """Like the builtin 'update' method but does not overwrite existing values.
    27 
    47 
    28   Args:
    48   Args:
    29     target: The dictionary that is to be updated, may be None
    49     target: The dictionary that is to be updated, may be None
    30     updates: A dictionary containing new values for the original dict
    50     updates: A dictionary containing new values for the original dict
    31 
    51 
    40     if key not in target:
    60     if key not in target:
    41       target[key] = value
    61       target[key] = value
    42 
    62 
    43   return target
    63   return target
    44 
    64 
       
    65 
    45 def zip(keys, values):
    66 def zip(keys, values):
    46   """Returns a dict containing keys with values
    67   """Returns a dict containing keys with values.
    47 
    68 
    48   If there are more items in keys than in values, None will be used.
    69   If there are more items in keys than in values, None will be used.
    49   If there are more items in values than in keys, they will be ignored.
    70   If there are more items in values than in keys, they will be ignored.
    50 
    71 
    51   Args:
    72   Args: