Module scrapfly.frozen_dict

Expand source code
class FrozenDict(dict):
    def __init__(self, *args, **kwargs):
        self._hash = None
        super(FrozenDict, self).__init__(*args, **kwargs)

    def __eq__(self, other):
        return self._hash == other.__hash

    def __hash__(self):
        if self._hash is None:
            self._hash = hash(tuple(sorted(self.items())))
        return self._hash

    def _immutable(self, *args, **kws):
        raise TypeError('cannot change object - object is immutable')

    __setitem__ = _immutable
    __delitem__ = _immutable
    pop = _immutable
    popitem = _immutable
    clear = _immutable
    update = _immutable
    setdefault = _immutable

Classes

class FrozenDict (*args, **kwargs)

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)

Expand source code
class FrozenDict(dict):
    def __init__(self, *args, **kwargs):
        self._hash = None
        super(FrozenDict, self).__init__(*args, **kwargs)

    def __eq__(self, other):
        return self._hash == other.__hash

    def __hash__(self):
        if self._hash is None:
            self._hash = hash(tuple(sorted(self.items())))
        return self._hash

    def _immutable(self, *args, **kws):
        raise TypeError('cannot change object - object is immutable')

    __setitem__ = _immutable
    __delitem__ = _immutable
    pop = _immutable
    popitem = _immutable
    clear = _immutable
    update = _immutable
    setdefault = _immutable

Ancestors

  • builtins.dict

Methods

def clear(self, *args, **kws)
Expand source code
def _immutable(self, *args, **kws):
    raise TypeError('cannot change object - object is immutable')
def pop(self, *args, **kws)
Expand source code
def _immutable(self, *args, **kws):
    raise TypeError('cannot change object - object is immutable')
def popitem(self, *args, **kws)
Expand source code
def _immutable(self, *args, **kws):
    raise TypeError('cannot change object - object is immutable')
def setdefault(self, *args, **kws)
Expand source code
def _immutable(self, *args, **kws):
    raise TypeError('cannot change object - object is immutable')
def update(self, *args, **kws)
Expand source code
def _immutable(self, *args, **kws):
    raise TypeError('cannot change object - object is immutable')