Files
deb-python-testtools/testtools/helpers.py
Robert Collins b3416e44da * Testtools now depends on extras, a small library split out from it to contain
generally useful non-testing facilities. Since extras has been around for a
  couple of testtools releases now, we're making this into a hard dependency of
  testtools. (Robert Collins)

* Testtools now uses setuptools rather than distutils so that we can document
  the extras dependency. (Robert Collins)
2013-01-18 22:17:19 +13:00

49 lines
1.2 KiB
Python

# Copyright (c) 2010-2012 testtools developers. See LICENSE for details.
__all__ = [
'safe_hasattr',
'try_import',
'try_imports',
]
import sys
# Compat - removal announced in 0.9.25.
from extras import (
safe_hasattr,
try_import,
try_imports,
)
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