summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/test/3rdparty/testtools-0.9.34/testtools/helpers.py
blob: 401d2cc10ed9a0f27b8e560dc5b0e7ef9936ffb1 (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
# 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