Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import pytest
from bidict import OrderedBidict, ValueDuplicationError, bidict
BIDICT_TYPES = (bidict, OrderedBidict)
ELEMENTS = OrderedBidict((
('H', 'hydrogen'), ('He', 'helium'),
('Li', 'lithium'), ('Be', 'beryllium'), ('B', 'boron'), ('C', 'carbon'),
('N', 'nitrogen'), ('O', 'oxygen'), ('F', 'fluorine'), ('Ne', 'neon'),
('Na', 'sodium'), ('Mg', 'magnesium'), ('Al', 'aluminum'), ('Si', 'silicon'),
('P', 'phosphorus'), ('S', 'sulfur'), ('Cl', 'chlorine'), ('Ar', 'argon'),
))
UPDATE_NODUP = OrderedBidict((
('K', 'potassium'), ('Ca', 'calcium'), ('Sc', 'Scandium'), ('Ti', 'titanium'),
('V', 'vanadium'), ('Cr', 'chromium'), ('Mn', 'manganese'), ('Fe', 'iron'), ('Co', 'cobalt'),
('Ni', 'nickel'), ('Cu', 'copper'), ('Zn', 'zinc'), ('Ga', 'gallium'), ('Ge', 'germanium'),
('As', 'arsenic'), ('Se', 'selenium'), ('Br', 'bromine'), ('Kr', 'krypton'), ('Rb', 'rubidium'),
('Sr', 'strontium'), ('Y', 'yttrium'), ('Zr', 'zirconium'), ('Nb', 'niobium'),
('Mo', 'molybdenum'), ('Tc', 'technetium'), ('Ru', 'ruthenium'), ('Rh', 'rhodium'),
('Pd', 'palladium'), ('Ag', 'silver'), ('Cd', 'cadmium'), ('In', 'indium'), ('Sn', 'tin'),
('Sb', 'antimony'), ('Te', 'tellurium'), ('I', 'iodine'), ('Xe', 'xenon'), ('Cs', 'cesium'),
('Ba', 'barium'), ('La', 'lanthanum'), ('Ce', 'cerium'), ('Pr', 'praseodymium'),
('Nd', 'neodymium'), ('Pm', 'promethium'), ('Sm', 'samarium'), ('Eu', 'europium'),
('Gd', 'gadolinium'), ('Tb', 'terbium'), ('Dy', 'dysprosium'), ('Ho', 'holmium'),
('Er', 'erbium'), ('Tm', 'thulium'), ('Yb', 'ytterbium'), ('Lu', 'lutetium'), ('Hf', 'hafnium'),
('Ta', 'tantalum'), ('W', 'tungsten'), ('Re', 'rhenium'), ('Os', 'osmium'), ('Ir', 'iridium'),
('Pt', 'platinum'), ('Au', 'gold'), ('Hg', 'mercury'), ('Tl', 'thallium'), ('Pb', 'lead'),
('Bi', 'bismuth'), ('Po', 'polonium'), ('At', 'astatine'), ('Rn', 'radon'), ('Fr', 'francium'),
('Ra', 'radium'), ('Ac', 'actinium'), ('Th', 'thorium'), ('Pa', 'protactinium'),
# 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/.
"""Benchmarks."""
import pytest
from bidict import OrderedBidict, ValueDuplicationError, bidict
BIDICT_TYPES = (bidict, OrderedBidict)
ELEMENTS = OrderedBidict((
('H', 'hydrogen'), ('He', 'helium'),
('Li', 'lithium'), ('Be', 'beryllium'), ('B', 'boron'), ('C', 'carbon'),
('N', 'nitrogen'), ('O', 'oxygen'), ('F', 'fluorine'), ('Ne', 'neon'),
('Na', 'sodium'), ('Mg', 'magnesium'), ('Al', 'aluminum'), ('Si', 'silicon'),
('P', 'phosphorus'), ('S', 'sulfur'), ('Cl', 'chlorine'), ('Ar', 'argon'),
))
UPDATE_NODUP = OrderedBidict((
('K', 'potassium'), ('Ca', 'calcium'), ('Sc', 'Scandium'), ('Ti', 'titanium'),
('V', 'vanadium'), ('Cr', 'chromium'), ('Mn', 'manganese'), ('Fe', 'iron'), ('Co', 'cobalt'),
('Ni', 'nickel'), ('Cu', 'copper'), ('Zn', 'zinc'), ('Ga', 'gallium'), ('Ge', 'germanium'),
('As', 'arsenic'), ('Se', 'selenium'), ('Br', 'bromine'), ('Kr', 'krypton'), ('Rb', 'rubidium'),
('Sr', 'strontium'), ('Y', 'yttrium'), ('Zr', 'zirconium'), ('Nb', 'niobium'),
('Mo', 'molybdenum'), ('Tc', 'technetium'), ('Ru', 'ruthenium'), ('Rh', 'rhodium'),
('Pd', 'palladium'), ('Ag', 'silver'), ('Cd', 'cadmium'), ('In', 'indium'), ('Sn', 'tin'),
('Sb', 'antimony'), ('Te', 'tellurium'), ('I', 'iodine'), ('Xe', 'xenon'), ('Cs', 'cesium'),
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
"""Equality and hashing tests."""
from collections import Counter, Hashable, OrderedDict, Mapping, defaultdict
from itertools import product
import pytest
from bidict import FrozenOrderedBidict, OrderedBidict, bidict, namedbidict, frozenbidict
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 = (
Equality tests.
"""
from collections import Counter, OrderedDict, Mapping, defaultdict
from itertools import combinations, product
import pytest
from bidict import FrozenBidict, FrozenOrderedBidict, OrderedBidict, bidict, namedbidict
# 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)
def _updatepmap(self):
""" Update parameter names according to root name and number. """
ar = self._allRoots()
if len(ar) == 1:
# Only one subset. Apply no name updating
self.pmap = bidict.OrderedBidict()
for n in list(self.bss[0].pmap):
self.pmap[n] = self.bss[0].pmap[n]
else:
# More than one subset
# Root Count
rc = {r:ar.count(r) for r in set(ar)}
self.pmap = bidict.OrderedBidict()
for b in self.bss:
useNumber = int(rc[b.rootName] > 1)
for n in list(b.pmap):
useName = self._composePN(n, b.rootName, b.number*useNumber)
self.pmap[useName] = b.pmap[n]
def _updatepmap(self):
""" Update parameter names according to root name and number. """
ar = self._allRoots()
if len(ar) == 1:
# Only one subset. Apply no name updating
self.pmap = bidict.OrderedBidict()
for n in list(self.bss[0].pmap):
self.pmap[n] = self.bss[0].pmap[n]
else:
# More than one subset
# Root Count
rc = {r:ar.count(r) for r in set(ar)}
self.pmap = bidict.OrderedBidict()
for b in self.bss:
useNumber = int(rc[b.rootName] > 1)
for n in list(b.pmap):
useName = self._composePN(n, b.rootName, b.number*useNumber)
self.pmap[useName] = b.pmap[n]