How to use the bidict.OrderedBidictBase function in bidict

To help you get started, we’ve selected a few bidict examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jab / bidict / tests / properties / test_properties.py View on Github external
method = getattr(bi, methodname)
        try:
            result = method(*args)
        except (KeyError, BidictException) as exc:
            # Call should fail clean, i.e. bi should be in the same state it was before the call.
            assertmsg = '%r did not fail clean: %r' % (method, exc)
            assert bi == bi_orig, assertmsg
            assert bi.inv == bi_orig.inv, assertmsg
        else:
            # Should get the same result as calling the same method on the compare-to dict.
            cmp_dict = cmp_dict_orig.copy()
            cmp_dict_meth = getattr(cmp_dict, methodname, None)
            if cmp_dict_meth:
                cmp_result = cmp_dict_meth(*args)
                if isinstance(cmp_result, Iterable):
                    coll = list if isinstance(bi, OrderedBidictBase) else set
                    result = coll(result)
                    cmp_result = coll(cmp_result)
                assert result == cmp_result, 'methodname=%s, args=%r' % (methodname, args)
        # Whether the call failed or succeeded, bi should pass consistency checks.
        assert len(bi) == sum(1 for _ in bi.items())
        assert len(bi.inv) == sum(1 for _ in bi.inv.items())
        assert bi == dict(bi)
        assert bi.inv == dict(bi.inv)
        assert bi == OrderedDict((k, v) for (v, k) in bi.inv.items())
        assert bi.inv == OrderedDict((v, k) for (k, v) in bi.items())
github jab / bidict / tests / properties / _strategies.py View on Github external
_cmpdict = lambda i: (OrderedDict if issubclass(i, OrderedBidictBase) else dict)  # noqa: E731
github jab / bidict / tests / test_hypothesis.py View on Github external
def test_eq_ne_hash(bi_cls, other_cls, init_items, init_unequal, not_a_mapping):
    """Test various equality comparisons and hashes between bidicts and other objects."""
    # pylint: disable=too-many-locals
    some_bidict = bi_cls(init_items)
    other_equal = other_cls(init_items)
    other_equal_inv = getattr(other_equal, 'inv',
                              OrderedDict((v, k) for (k, v) in iteritems(other_equal)))

    bidict_is_ordered = isinstance(some_bidict, OrderedBidictBase)
    other_is_ordered = issubclass(other_cls, (OrderedBidictBase, OrderedDict))
    collection = list if bidict_is_ordered and other_is_ordered else set

    both_hashable = all(isinstance(i, c.Hashable) for i in (some_bidict, other_equal))
    has_eq_order_sens = getattr(bi_cls, 'equals_order_sensitive', None)

    other_unequal = other_cls(init_unequal)
    other_unequal_inv = getattr(other_unequal, 'inv',
                                OrderedDict((v, k) for (k, v) in iteritems(other_unequal)))

    assert some_bidict == other_equal
    assert not some_bidict != other_equal
    assert some_bidict.inv == other_equal_inv
    assert not some_bidict.inv != other_equal_inv

    assert collection(iteritems(some_bidict)) == collection(iteritems(other_equal))
github jab / bidict / tests / test_hypothesis.py View on Github external
def test_eq_ne_hash(bi_cls, other_cls, init_items, init_unequal, not_a_mapping):
    """Test various equality comparisons and hashes between bidicts and other objects."""
    # pylint: disable=too-many-locals
    some_bidict = bi_cls(init_items)
    other_equal = other_cls(init_items)
    other_equal_inv = getattr(other_equal, 'inv',
                              OrderedDict((v, k) for (k, v) in iteritems(other_equal)))

    bidict_is_ordered = isinstance(some_bidict, OrderedBidictBase)
    other_is_ordered = issubclass(other_cls, (OrderedBidictBase, OrderedDict))
    collection = list if bidict_is_ordered and other_is_ordered else set

    both_hashable = all(isinstance(i, c.Hashable) for i in (some_bidict, other_equal))
    has_eq_order_sens = getattr(bi_cls, 'equals_order_sensitive', None)

    other_unequal = other_cls(init_unequal)
    other_unequal_inv = getattr(other_unequal, 'inv',
                                OrderedDict((v, k) for (k, v) in iteritems(other_unequal)))

    assert some_bidict == other_equal
    assert not some_bidict != other_equal
    assert some_bidict.inv == other_equal_inv
    assert not some_bidict.inv != other_equal_inv

    assert collection(iteritems(some_bidict)) == collection(iteritems(other_equal))
    assert collection(iteritems(some_bidict.inv)) == collection(iteritems(other_equal_inv))