app/soc/logic/dicts.py
author Pawel Solyga <Pawel.Solyga@gmail.com>
Sun, 16 Nov 2008 12:48:23 +0000
changeset 484 6364f8b0656b
parent 410 2af7f84f4fc7
child 486 ec6b50f48d3b
permissions -rw-r--r--
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. Patch by: Lennard de Rijk, Pawel Solyga
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>',
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:
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
    34
    A dictionary that only contains the (key,value) from target that have their 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
    35
  """
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
  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
    37
  
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
  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
    39
    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
    40
      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
    41
  
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
  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
    43
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
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 363
diff changeset
    45
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
    46
  """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
    47
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    48
  Args:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    49
    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
    50
    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
    51
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 363
diff changeset
    52
  Returns:
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 363
diff changeset
    53
    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
    54
  """
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
  if not target:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    57
    target = {}
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
  for key, value in updates.iteritems():
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    60
    if key not in target:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    61
      target[key] = value
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    62
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    63
  return target
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    64
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
    65
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    66
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
    67
  """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
    68
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    69
  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
    70
  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
    71
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    72
  Args:
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    73
    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
    74
    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
    75
  """
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
  result = {}
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    78
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    79
  size = len(keys)
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    80
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    81
  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
    82
    if i < len(values):
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    83
      value = values[i]
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    84
    else:
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    85
      value = None
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    86
    key = keys[i]
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    87
    result[key] = value
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    88
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 387
diff changeset
    89
  return result