app/soc/logic/dicts.py
author Sverre Rabbelier <srabbelier@gmail.com>
Sun, 02 Nov 2008 17:26:06 +0000
changeset 429 e50e18936f06
parent 410 2af7f84f4fc7
child 484 6364f8b0656b
permissions -rw-r--r--
Fixed typo in e-mail address Instead of 'sverre@rabbelier.nl' everything read 'sverer@rabbelier.nl'
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
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    17
"""Logic related to handling dictionaries
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>',
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    22
  ]
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
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 363
diff changeset
    25
def merge(target, updates):
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    26
  """Like the builtin 'update' method but does not overwrite existing values
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    27
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    28
  Args:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    29
    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
    30
    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
    31
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 363
diff changeset
    32
  Returns:
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 363
diff changeset
    33
    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
    34
  """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    35
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    36
  if not target:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    37
    target = {}
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    38
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    39
  for key, value in updates.iteritems():
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    40
    if key not in target:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    41
      target[key] = value
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    42
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    43
  return target
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    44
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    45
def zip(keys, values):
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    46
  """Returns a dict containing keys with values
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    47
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    48
  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
    49
  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
    50
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    51
  Args:
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    52
    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
    53
    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
    54
  """
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    55
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    56
  result = {}
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    57
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    58
  size = len(keys)
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    59
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    60
  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
    61
    if i < len(values):
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    62
      value = values[i]
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    63
    else:
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    64
      value = None
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    65
    key = keys[i]
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    66
    result[key] = value
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    67
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    68
  return result