summaryrefslogtreecommitdiff
path: root/testtools/helpers.py
blob: f3c87de3e52d27bf8f634d9bbbd3d10cbc6e4b61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Copyright (c) 2010-2012 testtools developers. See LICENSE for details.

import sys


def try_import(name, alternative=None, error_callback=None):
    """Attempt to import a module, with a fallback.

    Attempt to import ``name``.  If it fails, return ``alternative``.  When
    supporting multiple versions of Python or optional dependencies, it is
    useful to be able to try to import a module.

    :param name: The name of the object to import, e.g. ``os.path`` or
        ``os.path.join``.
    :param alternative: The value to return if no module can be imported.
        Defaults to None.
    :param error_callback: If non-None, a callable that is passed the
        ImportError when the module cannot be loaded.
    """
    module_segments = name.split('.')
    last_error = None
    remainder = []

    # module_name will be what successfully imports. We cannot walk from the
    # __import__ result because in import loops (A imports A.B, which imports
    # C, which calls try_import("A.B")) A.B will not yet be set.
    while module_segments:
        module_name = '.'.join(module_segments)
        try:
            __import__(module_name)
        except ImportError:
            last_error = sys.exc_info()[1]
            remainder.append(module_segments.pop())
            continue
        else:
            break
    else:
        if last_error is not None and error_callback is not None:
            error_callback(last_error)
        return alternative

    module = sys.modules[module_name]
    nonexistent = object()
    for segment in reversed(remainder):
        module = getattr(module, segment, nonexistent)
        if module is nonexistent:
            if last_error is not None and error_callback is not None:
                error_callback(last_error)
            return alternative

    return module


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 {k: function(dictionary[k]) for k in dictionary}


def filter_values(function, dictionary):
    """Filter ``dictionary`` by its values using ``function``."""
    return {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 {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