app/soc/logic/dicts.py
author Lennard de Rijk <ljvderijk@gmail.com>
Sun, 05 Jul 2009 23:45:57 +0200
changeset 2551 087684992ab0
parent 2177 e2c193e1f631
permissions -rw-r--r--
Renamed survey-take-090627.js to survey-take-090705.js.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     2
#
1308
35b75ffcbb37 Partially reverted "Update the copyright notice for 2009."
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1307
diff changeset
     3
# Copyright 2008 the Melange authors.
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     4
#
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     8
#
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    10
#
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    16
499
d22e4fe8e64b Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 486
diff changeset
    17
"""Logic related to handling dictionaries.
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    18
"""
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    19
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    20
__authors__ = [
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    21
  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
484
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 410
diff changeset
    22
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    23
  ]
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    24
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    25
484
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 410
diff changeset
    26
def filter(target, keys):
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 410
diff changeset
    27
  """Filters a dictonary to only allow items with the given keys.
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 410
diff changeset
    28
  
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 410
diff changeset
    29
  Args:
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 410
diff changeset
    30
    target: The dictionary that is to be filtered
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 410
diff changeset
    31
    keys: The list with keys to filter the dictionary on
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 410
diff changeset
    32
  
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 410
diff changeset
    33
  Returns:
499
d22e4fe8e64b Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 486
diff changeset
    34
    A dictionary that only contains the (key,value) from target that 
d22e4fe8e64b Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 486
diff changeset
    35
    have their key in keys.
484
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 410
diff changeset
    36
  """
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 410
diff changeset
    37
  result = {}
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 410
diff changeset
    38
  
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 410
diff changeset
    39
  for key, value in target.iteritems():
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 410
diff changeset
    40
    if key in keys:
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 410
diff changeset
    41
      result[key] = value
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 410
diff changeset
    42
  
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 410
diff changeset
    43
  return result
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 410
diff changeset
    44
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 410
diff changeset
    45
900
0b416bb14970 Extend dicts.merge to be (optionally) recursive
Sverre Rabbelier <srabbelier@gmail.com>
parents: 744
diff changeset
    46
def merge(target, updates, sub_merge=False, recursive=False):
484
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 410
diff changeset
    47
  """Like the builtin 'update' method but does not overwrite existing values.
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    48
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    49
  Args:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    50
    target: The dictionary that is to be updated, may be None
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    51
    updates: A dictionary containing new values for the original dict
900
0b416bb14970 Extend dicts.merge to be (optionally) recursive
Sverre Rabbelier <srabbelier@gmail.com>
parents: 744
diff changeset
    52
    sub_merge: Merge a dict or list present in both target and update
0b416bb14970 Extend dicts.merge to be (optionally) recursive
Sverre Rabbelier <srabbelier@gmail.com>
parents: 744
diff changeset
    53
    recursive: Determines whether merge_subdicts is recursive
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    54
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 363
diff changeset
    55
  Returns:
2177
e2c193e1f631 Do not rely on dicts.merge to change target
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2073
diff changeset
    56
    a new dict, with any missing values from updates merged into target
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    57
  """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    58
2177
e2c193e1f631 Do not rely on dicts.merge to change target
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2073
diff changeset
    59
  target = target.copy() if target else {}
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    60
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    61
  for key, value in updates.iteritems():
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    62
    if key not in target:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    63
      target[key] = value
900
0b416bb14970 Extend dicts.merge to be (optionally) recursive
Sverre Rabbelier <srabbelier@gmail.com>
parents: 744
diff changeset
    64
    elif sub_merge:
0b416bb14970 Extend dicts.merge to be (optionally) recursive
Sverre Rabbelier <srabbelier@gmail.com>
parents: 744
diff changeset
    65
      target_value = target[key]
0b416bb14970 Extend dicts.merge to be (optionally) recursive
Sverre Rabbelier <srabbelier@gmail.com>
parents: 744
diff changeset
    66
0b416bb14970 Extend dicts.merge to be (optionally) recursive
Sverre Rabbelier <srabbelier@gmail.com>
parents: 744
diff changeset
    67
      # try to merge dicts
0b416bb14970 Extend dicts.merge to be (optionally) recursive
Sverre Rabbelier <srabbelier@gmail.com>
parents: 744
diff changeset
    68
      if isinstance(value, dict) and isinstance(target_value, dict):
0b416bb14970 Extend dicts.merge to be (optionally) recursive
Sverre Rabbelier <srabbelier@gmail.com>
parents: 744
diff changeset
    69
        # the merge becomes recursive by specifying it not only as value
0b416bb14970 Extend dicts.merge to be (optionally) recursive
Sverre Rabbelier <srabbelier@gmail.com>
parents: 744
diff changeset
    70
        # to sub_merge but also to recursive
0b416bb14970 Extend dicts.merge to be (optionally) recursive
Sverre Rabbelier <srabbelier@gmail.com>
parents: 744
diff changeset
    71
        target[key] = merge(target_value, value,
0b416bb14970 Extend dicts.merge to be (optionally) recursive
Sverre Rabbelier <srabbelier@gmail.com>
parents: 744
diff changeset
    72
                            sub_merge=recursive, recursive=recursive)
0b416bb14970 Extend dicts.merge to be (optionally) recursive
Sverre Rabbelier <srabbelier@gmail.com>
parents: 744
diff changeset
    73
0b416bb14970 Extend dicts.merge to be (optionally) recursive
Sverre Rabbelier <srabbelier@gmail.com>
parents: 744
diff changeset
    74
      # try to merge lists
0b416bb14970 Extend dicts.merge to be (optionally) recursive
Sverre Rabbelier <srabbelier@gmail.com>
parents: 744
diff changeset
    75
      if isinstance(value, list) and isinstance(target_value, list):
0b416bb14970 Extend dicts.merge to be (optionally) recursive
Sverre Rabbelier <srabbelier@gmail.com>
parents: 744
diff changeset
    76
        target[key] = target_value + value
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    77
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    78
  return target
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    79
484
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 410
diff changeset
    80
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    81
def zip(keys, values):
484
6364f8b0656b Add an e-mail dispatcher that can be used to send messages via the website. Add base and invitation templates that can be used with email dispatcher to send invitation emails. Please read the module doc string for more information how to use it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 410
diff changeset
    82
  """Returns a dict containing keys with values.
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    83
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    84
  If there are more items in keys than in values, None will be used.
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    85
  If there are more items in values than in keys, they will be ignored.
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    86
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    87
  Args:
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    88
    keys: the keys for the dictionary
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    89
    values: the values for the dictionary
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    90
  """
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    91
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    92
  result = {}
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    93
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    94
  size = len(keys)
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    95
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    96
  for i in range(size):
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    97
    if i < len(values):
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    98
      value = values[i]
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    99
    else:
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
   100
      value = None
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
   101
    key = keys[i]
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
   102
    result[key] = value
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
   103
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
   104
  return result
486
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   105
719
2e635755713a Added a trivial unzip method, counterpart to zip
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   106
2e635755713a Added a trivial unzip method, counterpart to zip
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   107
def unzip(target, order):
744
cd9bf163473c Add missing dots, fix imports sorting and too long lines in different modules. Add TODO in soc.views.models.host module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 721
diff changeset
   108
  """Constructs a list from target in the order specified by order.
719
2e635755713a Added a trivial unzip method, counterpart to zip
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   109
2e635755713a Added a trivial unzip method, counterpart to zip
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   110
  Args:
2e635755713a Added a trivial unzip method, counterpart to zip
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   111
    target: the dictionary to pull the values from
2e635755713a Added a trivial unzip method, counterpart to zip
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   112
    order: the order of the keys
2e635755713a Added a trivial unzip method, counterpart to zip
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   113
  """
2e635755713a Added a trivial unzip method, counterpart to zip
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   114
2e635755713a Added a trivial unzip method, counterpart to zip
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   115
  return (target[key] for key in order)
2e635755713a Added a trivial unzip method, counterpart to zip
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   116
2e635755713a Added a trivial unzip method, counterpart to zip
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   117
486
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   118
def rename(target, keys):
499
d22e4fe8e64b Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 486
diff changeset
   119
  """Returns a dict containing only the key/value pairs from keys.
486
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   120
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   121
  The keys from target will be looked up in keys, and the corresponding
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   122
  value from keys will be used instead. If a key is not found, it is skipped.
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   123
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   124
  Args:
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   125
    target: the dictionary to filter
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   126
    keys: the fields to filter
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   127
  """
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   128
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   129
  result = {}
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   130
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   131
  for key, value in target.iteritems():
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   132
    if key in keys:
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   133
      new_key = keys[key]
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   134
      result[new_key] = value
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   135
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   136
  return result
721
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   137
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   138
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   139
def split(target):
744
cd9bf163473c Add missing dots, fix imports sorting and too long lines in different modules. Add TODO in soc.views.models.host module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 721
diff changeset
   140
  """Takes a dictionary and splits it into single-valued dicts.
721
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   141
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   142
  If there are any values in target that are a list it is split up
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   143
  into a new dictionary instead.
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   144
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   145
  >>> split({})
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   146
  [{}]
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   147
  >>> split({'foo':'bar'})
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   148
  [{'foo': 'bar'}]
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   149
  >>> split({'foo':'bar', 'bar':'baz'})
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   150
  [{'foo': 'bar', 'bar': 'baz'}]
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   151
  >>> split({'foo':'bar', 'bar':['one', 'two']})
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   152
  [{'foo': 'bar', 'bar': 'one'}, {'foo': 'bar', 'bar': 'two'}]
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   153
  >>> split({'foo':'bar', 'bar':['one', 'two'], 'baz': ['three', 'four']})
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   154
  [{'bar': 'one', 'foo': 'bar', 'baz': 'three'},
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   155
  {'bar': 'two', 'foo': 'bar', 'baz': 'three'},
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   156
  {'bar': 'one', 'foo': 'bar', 'baz': 'four'},
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   157
  {'bar': 'two', 'foo': 'bar', 'baz': 'four'}]
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   158
  """
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   159
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   160
  result = [{}]
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   161
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   162
  for key, values in target.iteritems():
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   163
    # Make the value a list if it's not
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   164
    if not isinstance(values, list):
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   165
      values = [values]
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   166
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   167
    tmpresult = []
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   168
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   169
    # Iterate over all we gathered so far
2073
6eb9b4652c80 Style fixes and removal of unused imports in soc.logic modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1899
diff changeset
   170
    for current_filter in result:
721
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   171
      for value in values:
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   172
        # Create a new dict from the current filter
2073
6eb9b4652c80 Style fixes and removal of unused imports in soc.logic modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1899
diff changeset
   173
        newdict = dict(current_filter)
721
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   174
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   175
        # And create a new dict that also has the current key/value pair
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   176
        newdict[key] = value
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   177
        tmpresult.append(newdict)
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   178
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   179
    # Update the result for the next iteration
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   180
    result = tmpresult
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   181
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   182
  return result
1370
8d684e64fbbe Add a groupby method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   183
1899
c841800f3727 Added format and containsAll to the dicts module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1842
diff changeset
   184
1370
8d684e64fbbe Add a groupby method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   185
def groupby(target, group_key):
8d684e64fbbe Add a groupby method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   186
  """Groups a list of dictionaries by group_key.
8d684e64fbbe Add a groupby method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   187
  """
8d684e64fbbe Add a groupby method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   188
8d684e64fbbe Add a groupby method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   189
  result = {}
8d684e64fbbe Add a groupby method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   190
8d684e64fbbe Add a groupby method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   191
  for value in target:
8d684e64fbbe Add a groupby method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   192
    key_value = value[group_key]
8d684e64fbbe Add a groupby method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   193
8d684e64fbbe Add a groupby method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   194
    if not key_value in result:
8d684e64fbbe Add a groupby method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   195
      result[key_value] = []
8d684e64fbbe Add a groupby method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   196
8d684e64fbbe Add a groupby method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   197
    result[key_value].append(value)
8d684e64fbbe Add a groupby method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   198
8d684e64fbbe Add a groupby method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   199
  return result
1749
a0ae6e6431a9 Add a groupDictBy to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1370
diff changeset
   200
1899
c841800f3727 Added format and containsAll to the dicts module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1842
diff changeset
   201
1749
a0ae6e6431a9 Add a groupDictBy to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1370
diff changeset
   202
def groupDictBy(target, key, new_key=None):
a0ae6e6431a9 Add a groupDictBy to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1370
diff changeset
   203
  """Groups a dictionary by a key.
a0ae6e6431a9 Add a groupDictBy to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1370
diff changeset
   204
  """
a0ae6e6431a9 Add a groupDictBy to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1370
diff changeset
   205
a0ae6e6431a9 Add a groupDictBy to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1370
diff changeset
   206
  if not new_key:
a0ae6e6431a9 Add a groupDictBy to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1370
diff changeset
   207
    new_key = key
a0ae6e6431a9 Add a groupDictBy to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1370
diff changeset
   208
a0ae6e6431a9 Add a groupDictBy to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1370
diff changeset
   209
  result = ((k, v[new_key]) for k, v in target.iteritems() if v[key])
a0ae6e6431a9 Add a groupDictBy to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1370
diff changeset
   210
  return dict(result)
1842
391b89d04bdd Added an identity function to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1749
diff changeset
   211
1899
c841800f3727 Added format and containsAll to the dicts module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1842
diff changeset
   212
1842
391b89d04bdd Added an identity function to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1749
diff changeset
   213
def identity(target):
391b89d04bdd Added an identity function to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1749
diff changeset
   214
  """Returns a dictionary with the values equal to the keys.
391b89d04bdd Added an identity function to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1749
diff changeset
   215
  """
391b89d04bdd Added an identity function to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1749
diff changeset
   216
1899
c841800f3727 Added format and containsAll to the dicts module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1842
diff changeset
   217
  result = ((i, i) for i in target)
1842
391b89d04bdd Added an identity function to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1749
diff changeset
   218
  return dict(result)
1899
c841800f3727 Added format and containsAll to the dicts module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1842
diff changeset
   219
c841800f3727 Added format and containsAll to the dicts module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1842
diff changeset
   220
c841800f3727 Added format and containsAll to the dicts module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1842
diff changeset
   221
def format(target, input):
c841800f3727 Added format and containsAll to the dicts module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1842
diff changeset
   222
  """Returns a dictionary with the values formatted with input.
c841800f3727 Added format and containsAll to the dicts module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1842
diff changeset
   223
  """
c841800f3727 Added format and containsAll to the dicts module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1842
diff changeset
   224
c841800f3727 Added format and containsAll to the dicts module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1842
diff changeset
   225
  result = ((k, v % input) for k, v in target.iteritems())
c841800f3727 Added format and containsAll to the dicts module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1842
diff changeset
   226
  return dict(result)
c841800f3727 Added format and containsAll to the dicts module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1842
diff changeset
   227
c841800f3727 Added format and containsAll to the dicts module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1842
diff changeset
   228
c841800f3727 Added format and containsAll to the dicts module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1842
diff changeset
   229
def containsAll(target, keys):
c841800f3727 Added format and containsAll to the dicts module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1842
diff changeset
   230
  """Returns true iff target contains all keys.
c841800f3727 Added format and containsAll to the dicts module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1842
diff changeset
   231
  """
c841800f3727 Added format and containsAll to the dicts module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1842
diff changeset
   232
c841800f3727 Added format and containsAll to the dicts module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1842
diff changeset
   233
  result = ((i in target) for i in keys)
c841800f3727 Added format and containsAll to the dicts module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1842
diff changeset
   234
  return all(result)