app/soc/logic/dicts.py
author Sverre Rabbelier <srabbelier@gmail.com>
Fri, 13 Feb 2009 22:36:45 +0000
changeset 1307 091a21cf3627
parent 900 0b416bb14970
child 1308 35b75ffcbb37
permissions -rw-r--r--
Update the copyright notice for 2009. Patch by: Sverre Rabbelier
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
#
1307
091a21cf3627 Update the copyright notice for 2009.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 900
diff changeset
     3
# Copyright 2009 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:
499
d22e4fe8e64b Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 486
diff changeset
    56
    the target dict, with any missing values from updates merged in, in-place.
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
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    59
  if not target:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    60
    target = {}
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    61
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    62
  for key, value in updates.iteritems():
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    63
    if key not in target:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    64
      target[key] = value
900
0b416bb14970 Extend dicts.merge to be (optionally) recursive
Sverre Rabbelier <srabbelier@gmail.com>
parents: 744
diff changeset
    65
    elif sub_merge:
0b416bb14970 Extend dicts.merge to be (optionally) recursive
Sverre Rabbelier <srabbelier@gmail.com>
parents: 744
diff changeset
    66
      target_value = target[key]
0b416bb14970 Extend dicts.merge to be (optionally) recursive
Sverre Rabbelier <srabbelier@gmail.com>
parents: 744
diff changeset
    67
0b416bb14970 Extend dicts.merge to be (optionally) recursive
Sverre Rabbelier <srabbelier@gmail.com>
parents: 744
diff changeset
    68
      # try to merge dicts
0b416bb14970 Extend dicts.merge to be (optionally) recursive
Sverre Rabbelier <srabbelier@gmail.com>
parents: 744
diff changeset
    69
      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
    70
        # 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
    71
        # to sub_merge but also to recursive
0b416bb14970 Extend dicts.merge to be (optionally) recursive
Sverre Rabbelier <srabbelier@gmail.com>
parents: 744
diff changeset
    72
        target[key] = merge(target_value, value,
0b416bb14970 Extend dicts.merge to be (optionally) recursive
Sverre Rabbelier <srabbelier@gmail.com>
parents: 744
diff changeset
    73
                            sub_merge=recursive, recursive=recursive)
0b416bb14970 Extend dicts.merge to be (optionally) recursive
Sverre Rabbelier <srabbelier@gmail.com>
parents: 744
diff changeset
    74
0b416bb14970 Extend dicts.merge to be (optionally) recursive
Sverre Rabbelier <srabbelier@gmail.com>
parents: 744
diff changeset
    75
      # try to merge lists
0b416bb14970 Extend dicts.merge to be (optionally) recursive
Sverre Rabbelier <srabbelier@gmail.com>
parents: 744
diff changeset
    76
      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
    77
        target[key] = target_value + value
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    78
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    79
  return target
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    80
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
    81
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    82
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
    83
  """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
    84
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 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
    86
  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
    87
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    88
  Args:
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    89
    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
    90
    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
    91
  """
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    92
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    93
  result = {}
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    94
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    95
  size = len(keys)
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    96
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    97
  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
    98
    if i < len(values):
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    99
      value = values[i]
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
   100
    else:
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
   101
      value = None
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
   102
    key = keys[i]
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
   103
    result[key] = value
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
   104
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
   105
  return result
486
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   106
719
2e635755713a Added a trivial unzip method, counterpart to zip
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   107
2e635755713a Added a trivial unzip method, counterpart to zip
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   108
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
   109
  """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
   110
2e635755713a Added a trivial unzip method, counterpart to zip
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   111
  Args:
2e635755713a Added a trivial unzip method, counterpart to zip
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   112
    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
   113
    order: the order of the keys
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
2e635755713a Added a trivial unzip method, counterpart to zip
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   116
  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
   117
2e635755713a Added a trivial unzip method, counterpart to zip
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   118
486
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   119
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
   120
  """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
   121
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   122
  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
   123
  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
   124
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   125
  Args:
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   126
    target: the dictionary to filter
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   127
    keys: the fields to filter
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
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   130
  result = {}
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   131
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   132
  for key, value in target.iteritems():
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   133
    if key in keys:
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   134
      new_key = keys[key]
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   135
      result[new_key] = value
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   136
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   137
  return result
721
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
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   140
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
   141
  """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
   142
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   143
  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
   144
  into a new dictionary instead.
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   145
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   146
  >>> split({})
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   147
  [{}]
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   148
  >>> split({'foo':'bar'})
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   149
  [{'foo': 'bar'}]
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   150
  >>> split({'foo':'bar', 'bar':'baz'})
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   151
  [{'foo': 'bar', 'bar': 'baz'}]
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   152
  >>> split({'foo':'bar', 'bar':['one', 'two']})
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   153
  [{'foo': 'bar', 'bar': 'one'}, {'foo': 'bar', 'bar': 'two'}]
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   154
  >>> 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
   155
  [{'bar': 'one', 'foo': 'bar', 'baz': 'three'},
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   156
  {'bar': 'two', 'foo': 'bar', 'baz': 'three'},
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   157
  {'bar': 'one', 'foo': 'bar', 'baz': 'four'},
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   158
  {'bar': 'two', 'foo': 'bar', 'baz': 'four'}]
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
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   161
  result = [{}]
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   162
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   163
  for key, values in target.iteritems():
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   164
    # 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
   165
    if not isinstance(values, list):
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   166
      values = [values]
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   167
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   168
    tmpresult = []
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   169
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   170
    # Iterate over all we gathered so far
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   171
    for filter in result:
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   172
      for value in values:
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   173
        # Create a new dict from the current filter
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   174
        newdict = dict(filter)
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   175
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   176
        # 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
   177
        newdict[key] = value
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   178
        tmpresult.append(newdict)
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   179
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   180
    # Update the result for the next iteration
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   181
    result = tmpresult
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   182
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   183
  return result