y.classes package

Submodules

y.classes.common module

y.classes.singleton module

class y.classes.singleton.ChecksumASyncSingletonMeta[source]

Bases: ASyncMeta, Generic[T]

A metaclass for creating singleton instances with checksummed addresses and asynchronous capabilities.

This metaclass extends ASyncMeta to ensure that only one instance of a class is created for each synchronous or asynchronous context, with the added functionality of checksumming Ethereum addresses using ChecksumAddressDict.

The differentiation between synchronous and asynchronous contexts is achieved by using the __a_sync_instance_will_be_sync__ method to determine the context during instance creation. Instances are stored in cls.__instances, which is a dict of ChecksumAddressDict keyed by the context (synchronous or asynchronous). This ensures that separate instances are created for each context.

Examples

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

See also

__base__

alias of ASyncMeta

classmethod __prepare__() dict

used to create the namespace for the class statement

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

Create or retrieve a singleton instance for the given address.

This method ensures that only one instance of a class is created for each address in a synchronous or asynchronous context. The address is checksummed to ensure consistency.

Note

This will only work if you initialize your objects using a keyword argument, not a positional argument. TODO: Make it work with positional args.

Parameters:
  • address (Address | HexAddress | ChecksumAddress | Contract | ERC20) – The address for which to create or retrieve the singleton instance.

  • *args – Additional positional arguments for instance creation.

  • **kwargs – Additional keyword arguments for instance creation.

Raises:

RuntimeError – If the instance’s asynchronous attribute does not match the expected context.

Return type:

T

Examples

Correct usage:
>>> class MySingleton(metaclass=ChecksumASyncSingletonMeta):
...     def __init__(self, address, asynchronous=False):
...         self.address = address
...         self.asynchronous = asynchronous
...
>>> instance1 = MySingleton('0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb', asynchronous=True)
>>> instance2 = MySingleton('0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb', asynchronous=True)
>>> assert instance1 is instance2
Incorrect initialization:
>>> class MySingleton(metaclass=ChecksumASyncSingletonMeta):
...     def __init__(self, address, asynchronous=False):
...         self.address = address
...         self.asynchronous = asynchronous
...
>>> try:
...     # Incorrectly initialized using a positional argument for asynchronous flag.
...     MySingleton('0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb', True)
... except RuntimeError as e:
...     print(e)
<RuntimeError: You must initialize your objects with 'asynchronous' specified as a kwarg, not a positional arg. ...>
__dir__()

Specialized __dir__ implementation for types.

__getattribute__(name, /)

Return getattr(self, name).

__init__(name, bases, namespace)[source]

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

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

  • bases – A tuple of base classes.

  • namespace – A dictionary representing the class namespace.

__instancecheck__(instance)

Override for isinstance(instance, cls).

__or__(value, /)

Return self|value.

__ror__(value, /)

Return value|self.

__sizeof__()

Return memory consumption of the type object.

__subclasscheck__(subclass)

Override for issubclass(subclass, cls).

__subclasses__()

Return a list of immediate subclasses.

mro()

Return a type’s method resolution order.

register(subclass)

Register a virtual subclass of an ABC.

Returns the subclass, to allow usage as a class decorator.

__bases__ = (<class 'a_sync.a_sync._meta.ASyncMeta'>, <class 'typing.Generic'>)
__basicsize__ = 920
__dictoffset__ = 264
__flags__ = 2156418560
__itemsize__ = 40
__mro__ = (<class 'y.classes.singleton.ChecksumASyncSingletonMeta'>, <class 'a_sync.a_sync._meta.ASyncMeta'>, <class 'abc.ABCMeta'>, <class 'type'>, <class 'typing.Generic'>, <class 'object'>)
__name__ = 'ChecksumASyncSingletonMeta'
__qualname__ = 'ChecksumASyncSingletonMeta'
__text_signature__ = None
__type_params__ = ()
__weakrefoffset__ = 368

Module contents