Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
).filter(lambda i: i[0] != i[1])
def _bidict_strat(bi_types, init_items=I_PAIRS_NODUP, _inv=attrgetter('inverse')):
fwd_bidicts = st.tuples(bi_types, init_items).map(lambda i: i[0](i[1]))
inv_bidicts = fwd_bidicts.map(_inv)
return fwd_bidicts | inv_bidicts
BIDICTS = _bidict_strat(BIDICT_TYPES)
FROZEN_BIDICTS = _bidict_strat(FROZEN_BIDICT_TYPES)
MUTABLE_BIDICTS = _bidict_strat(MUTABLE_BIDICT_TYPES)
ORDERED_BIDICTS = _bidict_strat(ORDERED_BIDICT_TYPES)
if PY2:
_NAMEDBI_VALID_NAME_PAT = re.compile('[A-Za-z_][A-Za-z0-9_]*$')
_NAMEDBI_VALID_NAMES = st.from_regex(_NAMEDBI_VALID_NAME_PAT, fullmatch=True)
IS_VALID_NAME = _NAMEDBI_VALID_NAME_PAT.match
else:
_ALPHABET = [chr(i) for i in range(0x10ffff) if chr(i).isidentifier()]
_NAMEDBI_VALID_NAMES = st.text(_ALPHABET, min_size=1)
IS_VALID_NAME = str.isidentifier
NAMEDBIDICT_NAMES_ALL_VALID = st.lists(_NAMEDBI_VALID_NAMES, min_size=3, max_size=3, unique=True)
NAMEDBIDICT_NAMES_SOME_INVALID = st.lists(st.text(min_size=1), min_size=3, max_size=3).filter(
lambda i: not all(IS_VALID_NAME(name) for name in i)
)
NAMEDBIDICT_TYPES = st.tuples(NAMEDBIDICT_NAMES_ALL_VALID, BIDICT_TYPES).map(
lambda i: namedbidict(*i[0], base_type=i[1])
)
NAMEDBIDICTS = _bidict_strat(NAMEDBIDICT_TYPES)
def test_refcycle_obidict_nodes(ob_cls, init_items):
"""When you release your last strong reference to an ordered bidict,
the refcount of each of its internal nodes drops to 0
allowing the memory to be reclaimed immediately.
"""
assume(init_items)
gc.disable()
try:
some_ordered_bidict = ob_cls(init_items)
# pylint: disable=protected-access
node_weakrefs = [ref(node) for node in some_ordered_bidict._fwdm.values()]
if PY2:
# On Python 2, list comprehension references leak to the enclosing scope,
# so this reference must be released for the refcount to drop to 0.
del node # pylint: disable=undefined-variable
assert all(ref() is not None for ref in node_weakrefs)
del some_ordered_bidict
assert all(ref() is None for ref in node_weakrefs)
finally:
gc.enable()
'H'
Please see https://github.com/jab/bidict for the most up-to-date code and
https://bidict.readthedocs.io for the most up-to-date documentation
if you are reading this elsewhere.
.. :copyright: (c) 2019 Joshua Bronson.
.. :license: MPLv2. See LICENSE for details.
"""
from warnings import warn
from .compat import PY2, PYMAJOR, PYMINOR
if PY2:
raise ImportError('Python 3.5+ is required.')
if (PYMAJOR, PYMINOR) < (3, 5): # pragma: no cover
warn('This version of bidict is untested on Python < 3.5 and may not work.')
# The rest of this file only collects functionality implemented in the rest of the
# source and exports it under the `bidict` module namespace (via `__all__`).
# pylint: disable=wrong-import-position
from ._abc import BidirectionalMapping
from ._base import BidictBase
from ._mut import MutableBidict
from ._bidict import bidict
from ._dup import DuplicationPolicy, IGNORE, OVERWRITE, RAISE
from ._exc import (
BidictException, DuplicationError,
cinv._inv = copy._fwd
cinv._inverse = copy
copy._inverse = cinv
return copy
__copy__ = copy
__len__ = _proxied('__len__')
__iter__ = _proxied('__iter__')
__getitem__ = _proxied('__getitem__')
values = _proxied('keys', attrname='inv')
values.__doc__ = \
"B.values() -> a set-like object providing a view on B's values.\n\n" \
'Note that because values of a BidirectionalMapping are also keys\n' \
'of its inverse, this returns a *KeysView* object rather than a\n' \
'*ValuesView* object, conferring set-like benefits.'
if PY2: # pragma: no cover
viewkeys = _proxied('viewkeys')
viewvalues = _proxied('viewkeys', attrname='inv',
doc=values.__doc__.replace('values()', 'viewvalues()'))
values.__doc__ = "Like dict's ``values``."
# Use ItemsView here rather than proxying to _fwd.viewitems() so that
# OrderedBidictBase (whose _fwd's values are nodes, not bare values)
# can use it.
viewitems = ItemsView
class BidictException(Exception):
"""Base class for bidict exceptions."""