def wrapped(*args, **kwargs): args = tuple([ frozendict(arg) if isinstance(arg, dict) else arg for arg in args ]) kwargs = { k: frozendict(v) if isinstance(v, dict) else v for k, v in kwargs.items() } return func(*args, **kwargs)
def test_sub(fd, fd_dict, subtrahend): fd_copy = fd.copy() newd = {k: v for k, v in fd.items() if (k, v) not in subtrahend} newfrozen: frozendict = frozendict(newd) assert fd - subtrahend == newfrozen fd -= subtrahend assert fd == newfrozen
def test_add(fd, addend): newd = dict(fd) newd.update(addend) newfrozen: frozendict = frozendict(newd) assert fd + addend == newfrozen fd += addend assert fd == newfrozen
def fd_nested_dict(): return { "Sulla": ("Marco", "Adele", "Mario", "Giulia"), "Hicks": ("Bill", ), "others": (frozendict({ "comedians": [ "Woody Allen", "George Carlin", "Emo Philips", "Groucho Marx", "Corrado Guzzanti", ], "comedies": [ "Bananas", "Dogma", "E=mo²", "A Night at the Opera", "Fascisti su Marte", ] })) }
def fd_unhashable(): return frozendict({1: []})
def fd(fd_dict): return frozendict(fd_dict)
{"code": "x == x", **skip_setup, **x100000, "name": "self == self", **size_affected}, {"code": "repr(x)", **skip_setup, **x3000, "name": "repr(d)", **size_affected}, {"code": "str(x)", **skip_setup, **x3000, "name": "str(d)", **size_affected}, ) for n in dictionary_sizes: print('#' * 80) d = dict() for i in range(n - 1): d[getUuid()] = getUuid() d["12323f29-c31f-478c-9b15-e7acc5354df9"] = getUuid() h = immutables.Map(d) fd = frozendict(d) for statement in statements: print('/' * 80) for x in (d, h, fd): if statement["name"] == "hash(d)" and isinstance(x, dict): continue if statement["size_affected"]: iterations = int(statement["iterations"] * max_size / n) else: iterations = statement["iterations"] print(statement) t = timeit.timeit( stmt=statement["code"],
def fd_sub(fd_sub_dict): return frozendict(fd_sub_dict)
def test_constructor_iterator(fd, fd_items): assert frozendict(fd_items) == fd
def test_constructor_kwargs(fd2, fd_dict_2): assert frozendict(**fd_dict_2) == fd2
def fd_empty(): return frozendict()
def fd_giulia(): return frozendict({"Marco": "Sulla", "Giulia": "Sulla"})
def math_fd_raw(): return frozendict(math_dict_raw())
class Species(Formula): """ Formula with phase information (e.g. solid, liquid, gas, or aqueous). Species extends :class:`~chemistry_tools.formulae.formula.Formula` with the new attribute :attr:`phase` :param composition: A :class:`~chemistry_tools.formulae.formula.Formula` object with the elemental composition of a substance, or a :class:`python:dict` representing the same. If :py:obj:`None` an empty object is created :param charge: :param phase: Either ``'s'``, ``'l'``, ``'g'``, or ``'aq'``. :py:obj:`None` represents an unknown phase. """ _phases: frozendict[str, str] = frozendict(s="Solid", l="Liquid", g="Gas", aq="Aqueous") def __init__(self, composition: Optional[Dict[str, int]] = None, charge: int = 0, phase=None): super().__init__(composition, charge) self.phase = phase @classmethod def from_kwargs(cls: Type['S'], *, charge: int = 0, phase: Optional[str] = None, **kwargs) -> S: """ Create a new :class:`~chemistry_tools.formulae.species.Species` object from keyword arguments representing the elements in the compound. :param charge: The charge of the compound :param phase: The phase of the compound (e.g. ``'s'`` for solid) """ # noqa: D400 return cls(kwargs, charge=charge, phase=phase) @classmethod def from_string(cls: Type['S'], formula: str, charge: int = 0, phase: Optional[str] = None) -> S: """ Create a new :class:`~chemistry_tools.formulae.species.Species` object by parsing a string. .. note:: Isotopes cannot (currently) be parsed using this method :param formula: A string with a chemical formula :param phase: Either ``'s'``, ``'l'``, ``'g'``, or ``'aq'``. :py:obj:`None` represents an unknown phase. :param charge: **Examples:** .. code-block:: python >>> water = Species.from_string('H2O') >>> water.phase None >>> NaCl = Species.from_string('NaCl(s)') >>> NaCl.phase s >>> Hg_l = Species.from_string('Hg(l)') >>> Hg_l.phase l >>> CO2g = Species.from_string('CO2(g)') >>> CO2g.phase g >>> CO2aq = Species.from_string('CO2(aq)') >>> CO2aq.phase aq """ if phase is None: for p in cls._phases: if formula.endswith(f"({p})"): phase = p break f = super().from_string(formula, charge) f.phase = phase return f def copy(self: S) -> S: """ Returns a copy of the :class:`~.Species`. """ return self.__class__(self, charge=self.charge, phase=self.phase) def __eq__(self, other) -> bool: """ Returns ``self == other``. """ if isinstance(other, Species): if super().__eq__(other): return self.phase == other.phase return super().__eq__(other) def _repr_elements(self) -> List[str]: elements = super()._repr_elements() if self.phase: elements.append(f"phase={self.phase}") return elements @property def hill_formula(self) -> str: """ Returns the formula in Hill notation. **Examples:** .. code-block:: python >>> Species.from_string('BrC2H5').hill_formula 'C2H5Br' >>> Species.from_string('HBr').hill_formula 'BrH' >>> Species.from_string('[(CH3)3Si2]2NNa').hill_formula 'C6H18NNaSi4' """ hill = super().hill_formula if self.phase: return f"{hill}({self.phase})" else: return hill @property def empirical_formula(self) -> str: """ Returns the empirical formula in Hill notation. The empirical formula has the simplest whole number ratio of atoms of each element present in the formula. **Examples:** .. code-block:: python >>> Formula.from_string('H2O').empirical_formula 'H2O' >>> Formula.from_string('S4').empirical_formula 'S' >>> Formula.from_string('C6H12O6').empirical_formula 'CH2O' """ hill = super().empirical_formula if self.phase: return f"{hill}({self.phase})" else: return hill
def fd_eq(fd_dict_eq): return frozendict(fd_dict_eq)
def fd2_raw(): return frozendict(fd_dict_2_raw())
def fd_dict_eq(): return {"Hicks": "Bill", "Sulla": "Marco", frozendict({1: 2}): "frozen"}
def fd_nested(fd_nested_dict): return frozendict(fd_nested_dict)
def fd_dict_2_raw(): return {"Sulla": "Marco", "Hicks": "Bill", "frozen": frozendict({1: 2})}