checksum_dict package

Submodules

checksum_dict.base module

class checksum_dict.base.ChecksumAddressDict[source]

Bases: Dict[EthAddressKey, T]

A dictionary that maps Ethereum addresses to objects, automatically checksumming the provided address key when setting and getting values.

If a seed dictionary or iterable of key-value pairs is provided, the keys will be added and the values will be set accordingly. The keys are checksummed when they are set using the __setitem__ method. If seed is not provided or is None, the dictionary is initialized without any entries.

Note

This implementation uses a cchecksum’s Cython implementation for checksumming to optimize performance over the standard eth_utils.to_checksum_address().

Examples

Creating a ChecksumAddressDict with a seed dictionary:

>>> from checksum_dict import ChecksumAddressDict
>>> seed = {"0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb": True}
>>> d = ChecksumAddressDict(seed)
>>> print(d)
ChecksumAddressDict({'0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB': True})

Creating an empty ChecksumAddressDict:

>>> d = ChecksumAddressDict()
>>> print(d)
ChecksumAddressDict({})

Accessing and setting items:

>>> lower = "0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb"
>>> d[lower] = False
>>> print(d[lower])
False

See also

  • EthAddressKey for details on how keys are checksummed.

__class_getitem__()

See PEP 585

__contains__(key, /)

True if the dictionary has the specified key, else False.

__delitem__(key, /)

Delete self[key].

__eq__(value, /)

Return self==value.

__ge__(value, /)

Return self>=value.

__getattribute__(name, /)

Return getattr(self, name).

__getitem__(key)[source]

x.__getitem__(y) <==> x[y]

Parameters:

key (AnyAddress)

Return type:

T

__gt__(value, /)

Return self>value.

__init__(seed=None)[source]
Parameters:

seed (Dict[AnyAddress, T] | Iterable[Tuple[AnyAddress, T]] | None)

Return type:

None

classmethod __init_subclass__(*args, **kwargs)

This method is called when a class is subclassed.

The default implementation does nothing. It may be overridden to extend subclasses.

__ior__(value, /)

Return self|=value.

__iter__()

Implement iter(self).

__le__(value, /)

Return self<=value.

__len__()

Return len(self).

__lt__(value, /)

Return self<value.

__ne__(value, /)

Return self!=value.

__new__(**kwargs)
__or__(value, /)

Return self|value.

__repr__()[source]

Return repr(self).

Return type:

str

__reversed__()

Return a reverse iterator over the dict keys.

__ror__(value, /)

Return value|self.

__setitem__(key, value)[source]

Set self[key] to value.

Parameters:
Return type:

None

__sizeof__() size of D in memory, in bytes
clear() None.  Remove all items from D.
copy() a shallow copy of D
fromkeys(value=None, /)

Create a new dictionary with keys from iterable and values set to value.

get(key, default=None, /)

Return the value for key if key is in the dictionary, else default.

items() a set-like object providing a view on D's items
keys() a set-like object providing a view on D's keys
pop(k[, d]) v, remove specified key and return the corresponding value.

If the key is not found, return the default if given; otherwise, raise a KeyError.

popitem()

Remove and return a (key, value) pair as a 2-tuple.

Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.

setdefault(key, default=None, /)

Insert key with a value of default if key is not in the dictionary.

Return the value for key if key is in the dictionary, else default.

update([E, ]**F) None.  Update D from dict/iterable E and F.

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values() an object providing a view on D's values
__dict__ = mappingproxy({'__module__': 'checksum_dict.base', '__doc__': '\n    A dictionary that maps Ethereum addresses to objects, automatically checksumming\n    the provided address key when setting and getting values.\n\n    If a `seed` dictionary or iterable of key-value pairs is provided, the keys will\n    be added and the values will be set accordingly. The keys are checksummed when\n    they are set using the `__setitem__` method. If `seed` is not provided or is `None`,\n    the dictionary is initialized without any entries.\n\n    Note:\n        This implementation uses a :mod:`cchecksum`\'s Cython implementation for checksumming to optimize\n        performance over the standard :func:`eth_utils.to_checksum_address`.\n\n    Examples:\n        Creating a ChecksumAddressDict with a seed dictionary:\n\n        >>> from checksum_dict import ChecksumAddressDict\n        >>> seed = {"0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb": True}\n        >>> d = ChecksumAddressDict(seed)\n        >>> print(d)\n        ChecksumAddressDict({\'0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB\': True})\n\n        Creating an empty ChecksumAddressDict:\n\n        >>> d = ChecksumAddressDict()\n        >>> print(d)\n        ChecksumAddressDict({})\n\n        Accessing and setting items:\n\n        >>> lower = "0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb"\n        >>> d[lower] = False\n        >>> print(d[lower])\n        False\n\n    See Also:\n        - :class:`EthAddressKey` for details on how keys are checksummed.\n    ', '__init__': <function ChecksumAddressDict.__init__>, '__repr__': <function ChecksumAddressDict.__repr__>, '__getitem__': <function ChecksumAddressDict.__getitem__>, '__setitem__': <function ChecksumAddressDict.__setitem__>, '_getitem_nochecksum': <function ChecksumAddressDict._getitem_nochecksum>, '_setitem_nochecksum': <function ChecksumAddressDict._setitem_nochecksum>, '__orig_bases__': (typing.Dict[checksum_dict._key.EthAddressKey, ~T],), '__dict__': <attribute '__dict__' of 'ChecksumAddressDict' objects>, '__weakref__': <attribute '__weakref__' of 'ChecksumAddressDict' objects>, '__parameters__': (~T,), '__annotations__': {}})
__hash__ = None
__module__ = 'checksum_dict.base'
__orig_bases__ = (typing.Dict[checksum_dict._key.EthAddressKey, ~T],)
__parameters__ = (~T,)
__slots__ = ()
__weakref__

list of weak references to the object (if defined)

checksum_dict.default module

class checksum_dict.default.DefaultChecksumDict[source]

Bases: DefaultDict[EthAddressKey, T], ChecksumAddressDict[T]

A defaultdict that maps Ethereum addresses to objects.

This class inherits from both collections.DefaultDict and ChecksumAddressDict. It will automatically checksum your provided address key when setting and getting values through the inherited behavior from ChecksumAddressDict.

Note

This implementation uses a cchecksum’s Cython implementation for checksumming to optimize performance over the standard eth_utils.to_checksum_address().

Example

>>> from checksum_dict import DefaultChecksumDict
>>> default = int
>>> d = DefaultChecksumDict(default)
>>> lower = "0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb"
>>> print(d[lower])
0
>>> d[lower] = 42
>>> print(d[lower])
42

As shown, the lowercase key lower is automatically checksummed when setting and getting values.

See also

  • ChecksumAddressDict

  • collections.DefaultDict

  • EthAddressKey for details on how keys are checksummed.

__class_getitem__()

See PEP 585

__contains__(key, /)

True if the dictionary has the specified key, else False.

__copy__()

D.copy() -> a shallow copy of D.

__delitem__(key, /)

Delete self[key].

__eq__(value, /)

Return self==value.

__ge__(value, /)

Return self>=value.

__getattribute__(name, /)

Return getattr(self, name).

__getitem__(key)

x.__getitem__(y) <==> x[y]

Parameters:

key (AnyAddress)

Return type:

T

__gt__(value, /)

Return self>value.

__init__(default, seed=None)[source]
Parameters:
Return type:

None

classmethod __init_subclass__(*args, **kwargs)

This method is called when a class is subclassed.

The default implementation does nothing. It may be overridden to extend subclasses.

__ior__(value, /)

Return self|=value.

__iter__()

Implement iter(self).

__le__(value, /)

Return self<=value.

__len__()

Return len(self).

__lt__(value, /)

Return self<value.

__missing__()

__missing__(key) # Called by __getitem__ for missing key; pseudo-code: if self.default_factory is None: raise KeyError((key,)) self[key] = value = self.default_factory() return value

__ne__(value, /)

Return self!=value.

__new__(**kwargs)
__or__(value, /)

Return self|value.

__reduce__()

Return state information for pickling.

__repr__()

Return repr(self).

__reversed__()

Return a reverse iterator over the dict keys.

__ror__(value, /)

Return value|self.

__setitem__(key, value)

Set self[key] to value.

Parameters:
Return type:

None

__sizeof__() size of D in memory, in bytes
clear() None.  Remove all items from D.
copy() a shallow copy of D.
fromkeys(value=None, /)

Create a new dictionary with keys from iterable and values set to value.

get(key, default=None, /)

Return the value for key if key is in the dictionary, else default.

items() a set-like object providing a view on D's items
keys() a set-like object providing a view on D's keys
pop(k[, d]) v, remove specified key and return the corresponding value.

If the key is not found, return the default if given; otherwise, raise a KeyError.

popitem()

Remove and return a (key, value) pair as a 2-tuple.

Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.

setdefault(key, default=None, /)

Insert key with a value of default if key is not in the dictionary.

Return the value for key if key is in the dictionary, else default.

update([E, ]**F) None.  Update D from dict/iterable E and F.

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values() an object providing a view on D's values
__annotations__ = {}
__dict__ = mappingproxy({'__module__': 'checksum_dict.default', '__doc__': '\n    A defaultdict that maps Ethereum addresses to objects.\n\n    This class inherits from both :class:`collections.DefaultDict` and\n    :class:`~checksum_dict.base.ChecksumAddressDict`. It will automatically\n    checksum your provided address key when setting and getting values through\n    the inherited behavior from :class:`~checksum_dict.base.ChecksumAddressDict`.\n\n    Note:\n        This implementation uses a :mod:`cchecksum`\'s Cython implementation for checksumming to optimize\n        performance over the standard :func:`eth_utils.to_checksum_address`.\n\n    Example:\n        >>> from checksum_dict import DefaultChecksumDict\n        >>> default = int\n        >>> d = DefaultChecksumDict(default)\n        >>> lower = "0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb"\n        >>> print(d[lower])\n        0\n        >>> d[lower] = 42\n        >>> print(d[lower])\n        42\n\n    As shown, the lowercase key `lower` is automatically checksummed when\n    setting and getting values.\n\n    See Also:\n        - :class:`~checksum_dict.base.ChecksumAddressDict`\n        - :class:`collections.DefaultDict`\n        - :class:`EthAddressKey` for details on how keys are checksummed.\n    ', '__init__': <function DefaultChecksumDict.__init__>, '_getitem_nochecksum': <function DefaultChecksumDict._getitem_nochecksum>, '__orig_bases__': (typing.DefaultDict[checksum_dict._key.EthAddressKey, ~T], checksum_dict.base.ChecksumAddressDict[~T]), '__dict__': <attribute '__dict__' of 'DefaultChecksumDict' objects>, '__weakref__': <attribute '__weakref__' of 'DefaultChecksumDict' objects>, '__parameters__': (~T,), '__annotations__': {}})
__hash__ = None
__module__ = 'checksum_dict.default'
__orig_bases__ = (typing.DefaultDict[checksum_dict._key.EthAddressKey, ~T], checksum_dict.base.ChecksumAddressDict[~T])
__parameters__ = (~T,)
__slots__ = ()
__weakref__

list of weak references to the object (if defined)

default_factory

Factory for default value called by __missing__().

checksum_dict.exceptions module

final exception checksum_dict.exceptions.KeyError[source]

Bases: KeyError

__delattr__(name, /)

Implement delattr(self, name).

__getattribute__(name, /)

Return getattr(self, name).

__init__(*args, **kwargs)
__new__(**kwargs)
__reduce__()

Helper for pickle.

__repr__()[source]

Return repr(self).

Return type:

str

__setattr__(name, value, /)

Implement setattr(self, name, value).

__setstate__()
__str__()

Return str(self).

with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

__cause__

exception cause

__context__

exception context

__dict__ = mappingproxy({'__module__': 'checksum_dict.exceptions', '__repr__': <function KeyError.__repr__>, '__weakref__': <attribute '__weakref__' of 'KeyError' objects>, '__doc__': None, '__annotations__': {}})
__module__ = 'checksum_dict.exceptions'
__suppress_context__
__traceback__
__weakref__

list of weak references to the object (if defined)

args

checksum_dict.singleton module

class checksum_dict.singleton.ChecksumAddressSingletonMeta[source]

Bases: type, Generic[T]

A metaclass for creating singleton instances of addresses.

This metaclass ensures that each address has a single instance across the application. It uses a ChecksumAddressDict to store instances and manages locks to ensure thread safety during instance creation.

Note

This implementation uses a cchecksum’s Cython implementation for checksumming to optimize performance over the standard eth_utils.to_checksum_address().

Examples

>>> class MySingleton(metaclass=ChecksumAddressSingletonMeta):
...     def __init__(self, address):
...         self.address = address
...
>>> instance1 = MySingleton('0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb')
>>> instance2 = MySingleton('0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb')
>>> assert instance1 is instance2

See also

  • ChecksumAddressDict for the underlying dictionary implementation.

  • EthAddressKey for details on how keys are checksummed.

__base__

alias of type

__call__(address, *args, **kwargs)[source]

Create or retrieve a singleton instance for the given address.

Parameters:
  • address (AnyAddress) – The address for which to create or retrieve the singleton instance.

  • *args (Any) – Additional positional arguments for instance creation.

  • **kwargs (Any) – Additional keyword arguments for instance creation.

Returns:

The singleton instance associated with the given address.

Raises:

KeyError – If the address is not found in the cache.

Return type:

T

Examples

>>> class MySingleton(metaclass=ChecksumAddressSingletonMeta):
...     def __init__(self, address):
...         self.address = address
...
>>> instance = MySingleton('0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb')
classmethod __class_getitem__(params)
__delattr__(name, /)

Implement delattr(self, name).

__delitem__(address)[source]

Delete the singleton instance for a given address from the cache.

Parameters:

address (AnyAddress) – The address for which to delete the instance.

Raises:

KeyError – If the address is not found in the cache.

Return type:

None

Examples

>>> meta = ChecksumAddressSingletonMeta('MySingleton', (), {})
>>> del meta['0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb']
__dir__()

Specialized __dir__ implementation for types.

__getattribute__(name, /)

Return getattr(self, name).

__getitem__(address)[source]

Get the singleton instance for address from the cache.

Parameters:

address (AnyAddress) – The address for which to retrieve the singleton instance.

Returns:

The singleton instance associated with the given address.

Raises:

KeyError – If the address is not found in the cache.

Return type:

T

Examples

>>> meta = ChecksumAddressSingletonMeta('MySingleton', (), {})
>>> instance = meta['0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb']
__init__(name, bases, namespace)[source]

Initialize the metaclass with a name, bases, and namespace.

Parameters:
  • name (str) – The name of the class being created.

  • bases (Tuple[type, ...]) – A tuple of base classes.

  • namespace (Dict[str, Any]) – A dictionary representing the class namespace.

Return type:

None

classmethod __init_subclass__(*args, **kwargs)

This method is called when a class is subclassed.

The default implementation does nothing. It may be overridden to extend subclasses.

__instancecheck__(instance, /)

Check if an object is an instance.

__new__(**kwargs)
__or__(value, /)

Return self|value.

__prepare__() dict

used to create the namespace for the class statement

__repr__()

Return repr(self).

__ror__(value, /)

Return value|self.

__setattr__(name, value, /)

Implement setattr(self, name, value).

__setitem__(address, item)[source]

Set the singleton instance for address in the cache.

You can use this if you need to implement non-standard init sequences.

Parameters:
  • address (AnyAddress) – The address for which to set the singleton instance.

  • item (T) – The instance to associate with the given address.

Return type:

None

Examples

>>> meta = ChecksumAddressSingletonMeta('MySingleton', (), {})
>>> meta['0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb'] = MySingleton('0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb')
__sizeof__()

Return memory consumption of the type object.

__subclasscheck__(subclass, /)

Check if a class is a subclass.

__subclasses__()

Return a list of immediate subclasses.

delete_instance(address)[source]

Delete the singleton instance for a given address, if it exists.

Parameters:

address (AnyAddress) – The address for which to delete the instance.

Return type:

None

Examples

>>> meta = ChecksumAddressSingletonMeta('MySingleton', (), {})
>>> meta.delete_instance('0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb')
get_instance(address)[source]

Retrieve the singleton instance for a given address, if it exists.

Parameters:

address (AnyAddress) – The address for which to retrieve the instance.

Returns:

The instance associated with the address, or None if not found.

Return type:

T | None

Examples

>>> meta = ChecksumAddressSingletonMeta('MySingleton', (), {})
>>> instance = meta.get_instance('0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb')
mro()

Return a type’s method resolution order.

__annotations__ = {}
__bases__ = (<class 'type'>, <class 'typing.Generic'>)
__basicsize__ = 888
__dict__ = mappingproxy({'__module__': 'checksum_dict.singleton', '__doc__': "A metaclass for creating singleton instances of addresses.\n\n    This metaclass ensures that each address has a single instance across the application.\n    It uses a :class:`~checksum_dict.base.ChecksumAddressDict` to store instances and manages locks to ensure\n    thread safety during instance creation.\n\n    Note:\n        This implementation uses a :mod:`cchecksum`'s Cython implementation for checksumming to optimize\n        performance over the standard :func:`eth_utils.to_checksum_address`.\n\n    Examples:\n        >>> class MySingleton(metaclass=ChecksumAddressSingletonMeta):\n        ...     def __init__(self, address):\n        ...         self.address = address\n        ...\n        >>> instance1 = MySingleton('0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb')\n        >>> instance2 = MySingleton('0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb')\n        >>> assert instance1 is instance2\n\n    See Also:\n        - :class:`ChecksumAddressDict` for the underlying dictionary implementation.\n        - :class:`EthAddressKey` for details on how keys are checksummed.\n    ", '__init__': <function ChecksumAddressSingletonMeta.__init__>, '__call__': <function ChecksumAddressSingletonMeta.__call__>, '__getitem__': <function ChecksumAddressSingletonMeta.__getitem__>, '__setitem__': <function ChecksumAddressSingletonMeta.__setitem__>, '__delitem__': <function ChecksumAddressSingletonMeta.__delitem__>, 'get_instance': <function ChecksumAddressSingletonMeta.get_instance>, 'delete_instance': <function ChecksumAddressSingletonMeta.delete_instance>, '_ChecksumAddressSingletonMeta__get_address_lock': <function ChecksumAddressSingletonMeta.__get_address_lock>, '_ChecksumAddressSingletonMeta__delete_address_lock': <function ChecksumAddressSingletonMeta.__delete_address_lock>, '__orig_bases__': (<class 'type'>, typing.Generic[~T]), '__parameters__': (~T,), '__annotations__': {'__instances': 'ChecksumAddressDict[T]', '__locks': '_LocksDict', '__locks_lock': 'threading.Lock'}})
__dictoffset__ = 264
__flags__ = 2148029952
__itemsize__ = 40
__module__ = 'checksum_dict.singleton'
__mro__ = (<class 'checksum_dict.singleton.ChecksumAddressSingletonMeta'>, <class 'type'>, <class 'typing.Generic'>, <class 'object'>)
__name__ = 'ChecksumAddressSingletonMeta'
__orig_bases__ = (<class 'type'>, typing.Generic[~T])
__parameters__ = (~T,)
__qualname__ = 'ChecksumAddressSingletonMeta'
__slots__ = ()
__text_signature__ = None
__weakrefoffset__ = 368

Module contents

final exception checksum_dict.KeyError[source]

Bases: KeyError

__delattr__(name, /)

Implement delattr(self, name).

__getattribute__(name, /)

Return getattr(self, name).

__init__(*args, **kwargs)
__new__(**kwargs)
__reduce__()

Helper for pickle.

__repr__()[source]

Return repr(self).

Return type:

str

__setattr__(name, value, /)

Implement setattr(self, name, value).

__setstate__()
__str__()

Return str(self).

with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

__annotations__ = {}
__cause__

exception cause

__context__

exception context

__dict__ = mappingproxy({'__module__': 'checksum_dict.exceptions', '__repr__': <function KeyError.__repr__>, '__weakref__': <attribute '__weakref__' of 'KeyError' objects>, '__doc__': None, '__annotations__': {}})
__module__ = 'checksum_dict.exceptions'
__suppress_context__
__traceback__
__weakref__

list of weak references to the object (if defined)

args
class checksum_dict.ChecksumAddressDict[source]

Bases: Dict[EthAddressKey, T]

A dictionary that maps Ethereum addresses to objects, automatically checksumming the provided address key when setting and getting values.

If a seed dictionary or iterable of key-value pairs is provided, the keys will be added and the values will be set accordingly. The keys are checksummed when they are set using the __setitem__ method. If seed is not provided or is None, the dictionary is initialized without any entries.

Note

This implementation uses a cchecksum’s Cython implementation for checksumming to optimize performance over the standard eth_utils.to_checksum_address().

Examples

Creating a ChecksumAddressDict with a seed dictionary:

>>> from checksum_dict import ChecksumAddressDict
>>> seed = {"0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb": True}
>>> d = ChecksumAddressDict(seed)
>>> print(d)
ChecksumAddressDict({'0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB': True})

Creating an empty ChecksumAddressDict:

>>> d = ChecksumAddressDict()
>>> print(d)
ChecksumAddressDict({})

Accessing and setting items:

>>> lower = "0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb"
>>> d[lower] = False
>>> print(d[lower])
False

See also

  • EthAddressKey for details on how keys are checksummed.

__class_getitem__()

See PEP 585

__contains__(key, /)

True if the dictionary has the specified key, else False.

__delitem__(key, /)

Delete self[key].

__eq__(value, /)

Return self==value.

__ge__(value, /)

Return self>=value.

__getattribute__(name, /)

Return getattr(self, name).

__getitem__(key)[source]

x.__getitem__(y) <==> x[y]

Parameters:

key (AnyAddress)

Return type:

T

__gt__(value, /)

Return self>value.

__init__(seed=None)[source]
Parameters:

seed (Dict[AnyAddress, T] | Iterable[Tuple[AnyAddress, T]] | None)

Return type:

None

classmethod __init_subclass__(*args, **kwargs)

This method is called when a class is subclassed.

The default implementation does nothing. It may be overridden to extend subclasses.

__ior__(value, /)

Return self|=value.

__iter__()

Implement iter(self).

__le__(value, /)

Return self<=value.

__len__()

Return len(self).

__lt__(value, /)

Return self<value.

__ne__(value, /)

Return self!=value.

__new__(**kwargs)
__or__(value, /)

Return self|value.

__repr__()[source]

Return repr(self).

Return type:

str

__reversed__()

Return a reverse iterator over the dict keys.

__ror__(value, /)

Return value|self.

__setitem__(key, value)[source]

Set self[key] to value.

Parameters:
Return type:

None

__sizeof__() size of D in memory, in bytes
clear() None.  Remove all items from D.
copy() a shallow copy of D
fromkeys(value=None, /)

Create a new dictionary with keys from iterable and values set to value.

get(key, default=None, /)

Return the value for key if key is in the dictionary, else default.

items() a set-like object providing a view on D's items
keys() a set-like object providing a view on D's keys
pop(k[, d]) v, remove specified key and return the corresponding value.

If the key is not found, return the default if given; otherwise, raise a KeyError.

popitem()

Remove and return a (key, value) pair as a 2-tuple.

Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.

setdefault(key, default=None, /)

Insert key with a value of default if key is not in the dictionary.

Return the value for key if key is in the dictionary, else default.

update([E, ]**F) None.  Update D from dict/iterable E and F.

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values() an object providing a view on D's values
__annotations__ = {}
__dict__ = mappingproxy({'__module__': 'checksum_dict.base', '__doc__': '\n    A dictionary that maps Ethereum addresses to objects, automatically checksumming\n    the provided address key when setting and getting values.\n\n    If a `seed` dictionary or iterable of key-value pairs is provided, the keys will\n    be added and the values will be set accordingly. The keys are checksummed when\n    they are set using the `__setitem__` method. If `seed` is not provided or is `None`,\n    the dictionary is initialized without any entries.\n\n    Note:\n        This implementation uses a :mod:`cchecksum`\'s Cython implementation for checksumming to optimize\n        performance over the standard :func:`eth_utils.to_checksum_address`.\n\n    Examples:\n        Creating a ChecksumAddressDict with a seed dictionary:\n\n        >>> from checksum_dict import ChecksumAddressDict\n        >>> seed = {"0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb": True}\n        >>> d = ChecksumAddressDict(seed)\n        >>> print(d)\n        ChecksumAddressDict({\'0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB\': True})\n\n        Creating an empty ChecksumAddressDict:\n\n        >>> d = ChecksumAddressDict()\n        >>> print(d)\n        ChecksumAddressDict({})\n\n        Accessing and setting items:\n\n        >>> lower = "0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb"\n        >>> d[lower] = False\n        >>> print(d[lower])\n        False\n\n    See Also:\n        - :class:`EthAddressKey` for details on how keys are checksummed.\n    ', '__init__': <function ChecksumAddressDict.__init__>, '__repr__': <function ChecksumAddressDict.__repr__>, '__getitem__': <function ChecksumAddressDict.__getitem__>, '__setitem__': <function ChecksumAddressDict.__setitem__>, '_getitem_nochecksum': <function ChecksumAddressDict._getitem_nochecksum>, '_setitem_nochecksum': <function ChecksumAddressDict._setitem_nochecksum>, '__orig_bases__': (typing.Dict[checksum_dict._key.EthAddressKey, ~T],), '__dict__': <attribute '__dict__' of 'ChecksumAddressDict' objects>, '__weakref__': <attribute '__weakref__' of 'ChecksumAddressDict' objects>, '__parameters__': (~T,), '__annotations__': {}})
__hash__ = None
__module__ = 'checksum_dict.base'
__orig_bases__ = (typing.Dict[checksum_dict._key.EthAddressKey, ~T],)
__parameters__ = (~T,)
__slots__ = ()
__weakref__

list of weak references to the object (if defined)

class checksum_dict.ChecksumAddressSingletonMeta[source]

Bases: type, Generic[T]

A metaclass for creating singleton instances of addresses.

This metaclass ensures that each address has a single instance across the application. It uses a ChecksumAddressDict to store instances and manages locks to ensure thread safety during instance creation.

Note

This implementation uses a cchecksum’s Cython implementation for checksumming to optimize performance over the standard eth_utils.to_checksum_address().

Examples

>>> class MySingleton(metaclass=ChecksumAddressSingletonMeta):
...     def __init__(self, address):
...         self.address = address
...
>>> instance1 = MySingleton('0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb')
>>> instance2 = MySingleton('0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb')
>>> assert instance1 is instance2

See also

  • ChecksumAddressDict for the underlying dictionary implementation.

  • EthAddressKey for details on how keys are checksummed.

__base__

alias of type

__call__(address, *args, **kwargs)[source]

Create or retrieve a singleton instance for the given address.

Parameters:
  • address (AnyAddress) – The address for which to create or retrieve the singleton instance.

  • *args (Any) – Additional positional arguments for instance creation.

  • **kwargs (Any) – Additional keyword arguments for instance creation.

Returns:

The singleton instance associated with the given address.

Raises:

KeyError – If the address is not found in the cache.

Return type:

T

Examples

>>> class MySingleton(metaclass=ChecksumAddressSingletonMeta):
...     def __init__(self, address):
...         self.address = address
...
>>> instance = MySingleton('0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb')
classmethod __class_getitem__(params)
__delattr__(name, /)

Implement delattr(self, name).

__delitem__(address)[source]

Delete the singleton instance for a given address from the cache.

Parameters:

address (AnyAddress) – The address for which to delete the instance.

Raises:

KeyError – If the address is not found in the cache.

Return type:

None

Examples

>>> meta = ChecksumAddressSingletonMeta('MySingleton', (), {})
>>> del meta['0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb']
__dir__()

Specialized __dir__ implementation for types.

__getattribute__(name, /)

Return getattr(self, name).

__getitem__(address)[source]

Get the singleton instance for address from the cache.

Parameters:

address (AnyAddress) – The address for which to retrieve the singleton instance.

Returns:

The singleton instance associated with the given address.

Raises:

KeyError – If the address is not found in the cache.

Return type:

T

Examples

>>> meta = ChecksumAddressSingletonMeta('MySingleton', (), {})
>>> instance = meta['0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb']
__init__(name, bases, namespace)[source]

Initialize the metaclass with a name, bases, and namespace.

Parameters:
  • name (str) – The name of the class being created.

  • bases (Tuple[type, ...]) – A tuple of base classes.

  • namespace (Dict[str, Any]) – A dictionary representing the class namespace.

Return type:

None

classmethod __init_subclass__(*args, **kwargs)

This method is called when a class is subclassed.

The default implementation does nothing. It may be overridden to extend subclasses.

__instancecheck__(instance, /)

Check if an object is an instance.

__new__(**kwargs)
__or__(value, /)

Return self|value.

__prepare__() dict

used to create the namespace for the class statement

__repr__()

Return repr(self).

__ror__(value, /)

Return value|self.

__setattr__(name, value, /)

Implement setattr(self, name, value).

__setitem__(address, item)[source]

Set the singleton instance for address in the cache.

You can use this if you need to implement non-standard init sequences.

Parameters:
  • address (AnyAddress) – The address for which to set the singleton instance.

  • item (T) – The instance to associate with the given address.

Return type:

None

Examples

>>> meta = ChecksumAddressSingletonMeta('MySingleton', (), {})
>>> meta['0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb'] = MySingleton('0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb')
__sizeof__()

Return memory consumption of the type object.

__subclasscheck__(subclass, /)

Check if a class is a subclass.

__subclasses__()

Return a list of immediate subclasses.

delete_instance(address)[source]

Delete the singleton instance for a given address, if it exists.

Parameters:

address (AnyAddress) – The address for which to delete the instance.

Return type:

None

Examples

>>> meta = ChecksumAddressSingletonMeta('MySingleton', (), {})
>>> meta.delete_instance('0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb')
get_instance(address)[source]

Retrieve the singleton instance for a given address, if it exists.

Parameters:

address (AnyAddress) – The address for which to retrieve the instance.

Returns:

The instance associated with the address, or None if not found.

Return type:

T | None

Examples

>>> meta = ChecksumAddressSingletonMeta('MySingleton', (), {})
>>> instance = meta.get_instance('0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb')
mro()

Return a type’s method resolution order.

__annotations__ = {'__instances': 'ChecksumAddressDict[T]', '__locks': '_LocksDict', '__locks_lock': 'threading.Lock'}
__bases__ = (<class 'type'>, <class 'typing.Generic'>)
__basicsize__ = 888
__dict__ = mappingproxy({'__module__': 'checksum_dict.singleton', '__doc__': "A metaclass for creating singleton instances of addresses.\n\n    This metaclass ensures that each address has a single instance across the application.\n    It uses a :class:`~checksum_dict.base.ChecksumAddressDict` to store instances and manages locks to ensure\n    thread safety during instance creation.\n\n    Note:\n        This implementation uses a :mod:`cchecksum`'s Cython implementation for checksumming to optimize\n        performance over the standard :func:`eth_utils.to_checksum_address`.\n\n    Examples:\n        >>> class MySingleton(metaclass=ChecksumAddressSingletonMeta):\n        ...     def __init__(self, address):\n        ...         self.address = address\n        ...\n        >>> instance1 = MySingleton('0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb')\n        >>> instance2 = MySingleton('0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb')\n        >>> assert instance1 is instance2\n\n    See Also:\n        - :class:`ChecksumAddressDict` for the underlying dictionary implementation.\n        - :class:`EthAddressKey` for details on how keys are checksummed.\n    ", '__init__': <function ChecksumAddressSingletonMeta.__init__>, '__call__': <function ChecksumAddressSingletonMeta.__call__>, '__getitem__': <function ChecksumAddressSingletonMeta.__getitem__>, '__setitem__': <function ChecksumAddressSingletonMeta.__setitem__>, '__delitem__': <function ChecksumAddressSingletonMeta.__delitem__>, 'get_instance': <function ChecksumAddressSingletonMeta.get_instance>, 'delete_instance': <function ChecksumAddressSingletonMeta.delete_instance>, '_ChecksumAddressSingletonMeta__get_address_lock': <function ChecksumAddressSingletonMeta.__get_address_lock>, '_ChecksumAddressSingletonMeta__delete_address_lock': <function ChecksumAddressSingletonMeta.__delete_address_lock>, '__orig_bases__': (<class 'type'>, typing.Generic[~T]), '__parameters__': (~T,), '__annotations__': {'__instances': 'ChecksumAddressDict[T]', '__locks': '_LocksDict', '__locks_lock': 'threading.Lock'}})
__dictoffset__ = 264
__flags__ = 2148029952
__itemsize__ = 40
__module__ = 'checksum_dict.singleton'
__mro__ = (<class 'checksum_dict.singleton.ChecksumAddressSingletonMeta'>, <class 'type'>, <class 'typing.Generic'>, <class 'object'>)
__name__ = 'ChecksumAddressSingletonMeta'
__orig_bases__ = (<class 'type'>, typing.Generic[~T])
__parameters__ = (~T,)
__qualname__ = 'ChecksumAddressSingletonMeta'
__slots__ = ()
__text_signature__ = None
__weakrefoffset__ = 368
class checksum_dict.DefaultChecksumDict[source]

Bases: DefaultDict[EthAddressKey, T], ChecksumAddressDict[T]

A defaultdict that maps Ethereum addresses to objects.

This class inherits from both collections.DefaultDict and ChecksumAddressDict. It will automatically checksum your provided address key when setting and getting values through the inherited behavior from ChecksumAddressDict.

Note

This implementation uses a cchecksum’s Cython implementation for checksumming to optimize performance over the standard eth_utils.to_checksum_address().

Example

>>> from checksum_dict import DefaultChecksumDict
>>> default = int
>>> d = DefaultChecksumDict(default)
>>> lower = "0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb"
>>> print(d[lower])
0
>>> d[lower] = 42
>>> print(d[lower])
42

As shown, the lowercase key lower is automatically checksummed when setting and getting values.

See also

  • ChecksumAddressDict

  • collections.DefaultDict

  • EthAddressKey for details on how keys are checksummed.

__class_getitem__()

See PEP 585

__contains__(key, /)

True if the dictionary has the specified key, else False.

__copy__()

D.copy() -> a shallow copy of D.

__delitem__(key, /)

Delete self[key].

__eq__(value, /)

Return self==value.

__ge__(value, /)

Return self>=value.

__getattribute__(name, /)

Return getattr(self, name).

__getitem__(key)

x.__getitem__(y) <==> x[y]

Parameters:

key (AnyAddress)

Return type:

T

__gt__(value, /)

Return self>value.

__init__(default, seed=None)[source]
Parameters:
Return type:

None

classmethod __init_subclass__(*args, **kwargs)

This method is called when a class is subclassed.

The default implementation does nothing. It may be overridden to extend subclasses.

__ior__(value, /)

Return self|=value.

__iter__()

Implement iter(self).

__le__(value, /)

Return self<=value.

__len__()

Return len(self).

__lt__(value, /)

Return self<value.

__missing__()

__missing__(key) # Called by __getitem__ for missing key; pseudo-code: if self.default_factory is None: raise KeyError((key,)) self[key] = value = self.default_factory() return value

__ne__(value, /)

Return self!=value.

__new__(**kwargs)
__or__(value, /)

Return self|value.

__reduce__()

Return state information for pickling.

__repr__()

Return repr(self).

__reversed__()

Return a reverse iterator over the dict keys.

__ror__(value, /)

Return value|self.

__setitem__(key, value)

Set self[key] to value.

Parameters:
Return type:

None

__sizeof__() size of D in memory, in bytes
clear() None.  Remove all items from D.
copy() a shallow copy of D.
fromkeys(value=None, /)

Create a new dictionary with keys from iterable and values set to value.

get(key, default=None, /)

Return the value for key if key is in the dictionary, else default.

items() a set-like object providing a view on D's items
keys() a set-like object providing a view on D's keys
pop(k[, d]) v, remove specified key and return the corresponding value.

If the key is not found, return the default if given; otherwise, raise a KeyError.

popitem()

Remove and return a (key, value) pair as a 2-tuple.

Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.

setdefault(key, default=None, /)

Insert key with a value of default if key is not in the dictionary.

Return the value for key if key is in the dictionary, else default.

update([E, ]**F) None.  Update D from dict/iterable E and F.

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values() an object providing a view on D's values
__annotations__ = {}
__dict__ = mappingproxy({'__module__': 'checksum_dict.default', '__doc__': '\n    A defaultdict that maps Ethereum addresses to objects.\n\n    This class inherits from both :class:`collections.DefaultDict` and\n    :class:`~checksum_dict.base.ChecksumAddressDict`. It will automatically\n    checksum your provided address key when setting and getting values through\n    the inherited behavior from :class:`~checksum_dict.base.ChecksumAddressDict`.\n\n    Note:\n        This implementation uses a :mod:`cchecksum`\'s Cython implementation for checksumming to optimize\n        performance over the standard :func:`eth_utils.to_checksum_address`.\n\n    Example:\n        >>> from checksum_dict import DefaultChecksumDict\n        >>> default = int\n        >>> d = DefaultChecksumDict(default)\n        >>> lower = "0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb"\n        >>> print(d[lower])\n        0\n        >>> d[lower] = 42\n        >>> print(d[lower])\n        42\n\n    As shown, the lowercase key `lower` is automatically checksummed when\n    setting and getting values.\n\n    See Also:\n        - :class:`~checksum_dict.base.ChecksumAddressDict`\n        - :class:`collections.DefaultDict`\n        - :class:`EthAddressKey` for details on how keys are checksummed.\n    ', '__init__': <function DefaultChecksumDict.__init__>, '_getitem_nochecksum': <function DefaultChecksumDict._getitem_nochecksum>, '__orig_bases__': (typing.DefaultDict[checksum_dict._key.EthAddressKey, ~T], checksum_dict.base.ChecksumAddressDict[~T]), '__dict__': <attribute '__dict__' of 'DefaultChecksumDict' objects>, '__weakref__': <attribute '__weakref__' of 'DefaultChecksumDict' objects>, '__parameters__': (~T,), '__annotations__': {}})
__hash__ = None
__module__ = 'checksum_dict.default'
__orig_bases__ = (typing.DefaultDict[checksum_dict._key.EthAddressKey, ~T], checksum_dict.base.ChecksumAddressDict[~T])
__parameters__ = (~T,)
__slots__ = ()
__weakref__

list of weak references to the object (if defined)

default_factory

Factory for default value called by __missing__().