Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _wrapped(items): # noqa: E306 (expected 1 blank line before a nested definition)
fwd = dict(items)
if key:
assume(len(fwd) < len(items))
if val:
inv = dict((v, k) for (k, v) in items)
assume(len(inv) < len(items))
if key and val:
invinv = dict((v, k) for (k, v) in iteritems(inv))
# If an item has a duplicate key and val, they must duplicate two other distinct items.
assume(len(invinv) < len(fwd))
return items
return _wrapped
MyNamedBidict = namedbidict('MyNamedBidict', 'key', 'val')
MyNamedFrozenBidict = namedbidict('MyNamedBidict', 'key', 'val', base_type=frozenbidict)
NAMEDBIDICT_VALID_NAME = re.compile('[A-z][A-z0-9_]*$')
MUTABLE_BIDICT_TYPES = (
bidict, OrderedBidict, MyNamedBidict)
IMMUTABLE_BIDICT_TYPES = (frozenbidict, FrozenOrderedBidict, MyNamedFrozenBidict)
ORDERED_BIDICT_TYPES = (OrderedBidict, FrozenOrderedBidict)
BIDICT_TYPES = MUTABLE_BIDICT_TYPES + IMMUTABLE_BIDICT_TYPES
MAPPING_TYPES = BIDICT_TYPES + (dict, OrderedDict)
H_BIDICT_TYPES = strat.sampled_from(BIDICT_TYPES)
H_MUTABLE_BIDICT_TYPES = strat.sampled_from(MUTABLE_BIDICT_TYPES)
H_IMMUTABLE_BIDICT_TYPES = strat.sampled_from(IMMUTABLE_BIDICT_TYPES)
H_ORDERED_BIDICT_TYPES = strat.sampled_from(ORDERED_BIDICT_TYPES)
H_MAPPING_TYPES = strat.sampled_from(MAPPING_TYPES)
H_NAMES = strat.sampled_from(('valid1', 'valid2', 'valid3', 'in-valid'))
H_DUP_POLICIES = strat.sampled_from((IGNORE, OVERWRITE, RAISE))
def test_namedbidict_raises_on_invalid_name(names):
""":func:`bidict.namedbidict` should raise if given invalid names."""
typename, keyname, valname = names
with pytest.raises(ValueError):
namedbidict(typename, keyname, valname)
# pylint: disable=C0111
class DictSubcls(dict):
pass
class OrderedBidictSubcls(OrderedBidict):
pass
# pylint: disable=C0103
items = [('a', 1), ('b', 2)] # use int values so makes sense with Counter
itemsreversed = list(reversed(items))
bidict_items = bidict(items)
frozenbidict_items = FrozenBidict(items)
namedbidict_items = namedbidict('named', 'keys', 'vals')(items)
orderedbidict_items = OrderedBidict(items)
orderedbidict_itemsreversed = OrderedBidict(itemsreversed)
orderedbidictsubcls_items = OrderedBidictSubcls(items)
orderedbidictsubcls_itemsreversed = OrderedBidictSubcls(itemsreversed)
frozenorderedbidict_items = FrozenOrderedBidict(items)
frozenorderedbidict_itemsreversed = FrozenOrderedBidict(itemsreversed)
bidicts = (
bidict_items,
frozenbidict_items,
namedbidict_items,
orderedbidict_items,
orderedbidict_itemsreversed,
orderedbidictsubcls_items,
orderedbidictsubcls_itemsreversed,
frozenorderedbidict_items,
frozenorderedbidict_itemsreversed,
from itertools import product
class dictsubclass(dict):
pass
d = dict(H='hydrogen', He='helium')
c = Counter(d)
o = OrderedDict(d)
dd = defaultdict(int, d)
s = dictsubclass(d)
b = bidict(d)
f = frozenbidict(d)
n = namedbidict('named', 'keys', 'vals')(d)
dicts = (d, c, o, dd, s)
bidicts = (b, f, n)
@pytest.mark.parametrize('d, b', product(dicts, bidicts))
def test_eq(d, b):
assert d == b
lambda i: namedbidict(*i[0], base_type=i[1])
)
# -*- coding: utf-8 -*-
# Copyright 2009-2019 Joshua Bronson. All Rights Reserved.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
"""Types for Hypothoses tests."""
from collections import OrderedDict
from collections.abc import ItemsView, KeysView, Mapping
from bidict import bidict, OrderedBidict, frozenbidict, FrozenOrderedBidict, namedbidict
MyNamedBidict = namedbidict('MyNamedBidict', 'key', 'val')
MyNamedFrozenBidict = namedbidict('MyNamedFrozenBidict', 'key', 'val', base_type=frozenbidict)
MyNamedOrderedBidict = namedbidict('MyNamedOrderedBidict', 'key', 'val', base_type=OrderedBidict)
MUTABLE_BIDICT_TYPES = (bidict, OrderedBidict, MyNamedBidict)
FROZEN_BIDICT_TYPES = (frozenbidict, FrozenOrderedBidict, MyNamedFrozenBidict)
ORDERED_BIDICT_TYPES = (OrderedBidict, FrozenOrderedBidict, MyNamedOrderedBidict)
BIDICT_TYPES = tuple(set(MUTABLE_BIDICT_TYPES + FROZEN_BIDICT_TYPES + ORDERED_BIDICT_TYPES))
class _FrozenDict(KeysView, Mapping):
def __init__(self, *args, **kw): # pylint: disable=super-init-not-called
self._mapping = dict(*args, **kw)
def __getitem__(self, key):
return self._mapping[key]
class _DictSubcls(dict):
pass
class _OrderedBidictSubcls(OrderedBidict):
pass
ITEMS = [('a', 1), ('b', 2)] # use int values so we can use with Counter below
ITEMS_REV = list(reversed(ITEMS))
BIDICT = bidict(ITEMS)
FROZEN = frozenbidict(ITEMS)
NAMED = namedbidict('named', 'keys', 'vals')(ITEMS)
ORDERED = OrderedBidict(ITEMS)
ORDERED_REV = OrderedBidict(ITEMS_REV)
ORDERED_SUB = _OrderedBidictSubcls(ITEMS)
ORDERED_SUB_REV = _OrderedBidictSubcls(ITEMS_REV)
FROZEN_ORDERED = FrozenOrderedBidict(ITEMS)
FROZEN_ORDERED_REV = FrozenOrderedBidict(ITEMS_REV)
BIDICTS = (
BIDICT,
FROZEN,
NAMED,
ORDERED,
ORDERED_REV,
ORDERED_SUB,
ORDERED_SUB_REV,
FROZEN_ORDERED,
FROZEN_ORDERED_REV,
import collections
import logging
from bidict import namedbidict
import braintree
from .compat import getcallargs
logger = logging.getLogger(__name__)
IDMap = namedbidict('IDMap', 'fake_id', 'real_id')
def ensure_state_is_init(f):
def wrapper(*args, **kwargs):
# There's not currently a place to provide global init for the state dict,
# each action needs to ensure subitems are initialized.
named_args = getcallargs(f, *args, **kwargs)
state = named_args['state']
if 'id_maps' not in state:
state['id_maps'] = collections.defaultdict(IDMap)
if 'last_fake_ids' not in state:
state['last_fake_ids'] = {}
return f(*args, **kwargs)
class IndexItem(object):
def __init__(self, item_id, item_name, item_value, index):
self.id, self.name, self.value = item_id, item_name, item_value
self.index = index
def __repr__(self):
return pformat({
'id': self.id,
'name': self.name,
'value': self.value})
class Indexer(object):
RESP_STATUS_CODE = '200'
Index = namedbidict('Index', 'id', 'name')
index_item_type = IndexItem
def __init__(self, rqst,
name_from=None,
id_from=None,
response_code=None, # will use default if set to None
index_item_type=None # will use default if None
):
self.rqst = rqst
self.name_from = name_from
self.id_from = id_from
deref = rqst.client.deref
self.schema = deref(rqst.spec['responses'][response_code or Indexer.RESP_STATUS_CODE]['schema'])