app/soc/logic/dicts.py
author Todd Larsen <tlarsen@google.com>
Wed, 21 Jan 2009 02:14:05 +0000
changeset 860 cfb57fe35d3c
parent 744 cd9bf163473c
child 900 0b416bb14970
permissions -rw-r--r--
Add a clean_agrees_to_tos() validator that requires "Yes" if ToS exists. Also, more user_logic.logic => user_logic fixes. If you are going to import "as", you might as well make the renaming worthwhile. Maybe we need to add a section on "import ... from ... as ..." in the style guide? Patch by: Todd Larsen Review by: to-be-reviewed
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
#
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
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
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 363
diff changeset
    46
def merge(target, updates):
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
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    52
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 363
diff changeset
    53
  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
    54
    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
    55
  """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    56
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    57
  if not target:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    58
    target = {}
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    59
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    60
  for key, value in updates.iteritems():
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    61
    if key not in target:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    62
      target[key] = value
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    63
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    64
  return target
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    65
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
    66
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    67
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
    68
  """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
    69
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    70
  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
    71
  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
    72
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    73
  Args:
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    74
    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
    75
    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
    76
  """
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    77
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    78
  result = {}
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    79
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    80
  size = len(keys)
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    81
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    82
  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
    83
    if i < len(values):
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    84
      value = values[i]
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    85
    else:
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    86
      value = None
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    87
    key = keys[i]
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    88
    result[key] = value
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    89
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    90
  return result
486
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
    91
719
2e635755713a Added a trivial unzip method, counterpart to zip
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    92
2e635755713a Added a trivial unzip method, counterpart to zip
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    93
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
    94
  """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
    95
2e635755713a Added a trivial unzip method, counterpart to zip
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    96
  Args:
2e635755713a Added a trivial unzip method, counterpart to zip
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    97
    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
    98
    order: the order of the keys
2e635755713a Added a trivial unzip method, counterpart to zip
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    99
  """
2e635755713a Added a trivial unzip method, counterpart to zip
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   100
2e635755713a Added a trivial unzip method, counterpart to zip
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   101
  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
   102
2e635755713a Added a trivial unzip method, counterpart to zip
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   103
486
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   104
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
   105
  """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
   106
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   107
  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
   108
  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
   109
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   110
  Args:
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   111
    target: the dictionary to filter
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   112
    keys: the fields to filter
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   113
  """
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   114
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   115
  result = {}
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   116
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   117
  for key, value in target.iteritems():
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   118
    if key in keys:
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   119
      new_key = keys[key]
ec6b50f48d3b Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 484
diff changeset
   120
      result[new_key] = value
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
  return result
721
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   123
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   124
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   125
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
   126
  """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
   127
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   128
  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
   129
  into a new dictionary instead.
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   130
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   131
  >>> split({})
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   132
  [{}]
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   133
  >>> split({'foo':'bar'})
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   134
  [{'foo': 'bar'}]
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   135
  >>> split({'foo':'bar', 'bar':'baz'})
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   136
  [{'foo': 'bar', 'bar': 'baz'}]
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   137
  >>> split({'foo':'bar', 'bar':['one', 'two']})
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   138
  [{'foo': 'bar', 'bar': 'one'}, {'foo': 'bar', 'bar': 'two'}]
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   139
  >>> 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
   140
  [{'bar': 'one', 'foo': 'bar', 'baz': 'three'},
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   141
  {'bar': 'two', 'foo': 'bar', 'baz': 'three'},
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   142
  {'bar': 'one', 'foo': 'bar', 'baz': 'four'},
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   143
  {'bar': 'two', 'foo': 'bar', 'baz': 'four'}]
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
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   146
  result = [{}]
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
  for key, values in target.iteritems():
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   149
    # 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
   150
    if not isinstance(values, list):
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   151
      values = [values]
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   152
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   153
    tmpresult = []
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   154
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   155
    # Iterate over all we gathered so far
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   156
    for filter in result:
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   157
      for value in values:
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   158
        # Create a new dict from the current filter
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   159
        newdict = dict(filter)
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
        # 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
   162
        newdict[key] = value
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   163
        tmpresult.append(newdict)
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   164
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   165
    # Update the result for the next iteration
6f1d29857072 Added a filter method to dicts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 719
diff changeset
   166
    result = tmpresult
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
  return result