summaryrefslogtreecommitdiff
path: root/lib/testtools/testtools/helpers.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/testtools/testtools/helpers.py')
-rw-r--r--lib/testtools/testtools/helpers.py34
1 files changed, 33 insertions, 1 deletions
diff --git a/lib/testtools/testtools/helpers.py b/lib/testtools/testtools/helpers.py
index dbf66719ed..2595c1d629 100644
--- a/lib/testtools/testtools/helpers.py
+++ b/lib/testtools/testtools/helpers.py
@@ -1,4 +1,4 @@
-# Copyright (c) 2010-2011 testtools developers. See LICENSE for details.
+# Copyright (c) 2010-2012 testtools developers. See LICENSE for details.
__all__ = [
'safe_hasattr',
@@ -85,3 +85,35 @@ def safe_hasattr(obj, attr, _marker=object()):
properties.
"""
return getattr(obj, attr, _marker) is not _marker
+
+
+def map_values(function, dictionary):
+ """Map ``function`` across the values of ``dictionary``.
+
+ :return: A dict with the same keys as ``dictionary``, where the value
+ of each key ``k`` is ``function(dictionary[k])``.
+ """
+ return dict((k, function(dictionary[k])) for k in dictionary)
+
+
+def filter_values(function, dictionary):
+ """Filter ``dictionary`` by its values using ``function``."""
+ return dict((k, v) for k, v in dictionary.items() if function(v))
+
+
+def dict_subtract(a, b):
+ """Return the part of ``a`` that's not in ``b``."""
+ return dict((k, a[k]) for k in set(a) - set(b))
+
+
+def list_subtract(a, b):
+ """Return a list ``a`` without the elements of ``b``.
+
+ If a particular value is in ``a`` twice and ``b`` once then the returned
+ list then that value will appear once in the returned list.
+ """
+ a_only = list(a)
+ for x in b:
+ if x in a_only:
+ a_only.remove(x)
+ return a_only