def get_class_instance_key(cls, args, kwargs): """ Returns a unique identifier for a class instantiation. """ l = [id(cls)] for arg in args: l.append(id(arg)) l.extend((k, id(v)) for k, v in kwargs.items()) return tuple(sorted(l)) # used as a metaclass for classes that should be memoized # (technically not a decorator, but it's similar enough) Cached = mementos.memento_factory('Cached', get_class_instance_key) def memoize(fun): """A decorator for memoizing functions. Only works on functions that take simple arguments - arguments that take list-like or dict-like arguments will not be memoized, and this function will raise a TypeError. """ @funcutils.wraps(fun) def wrapper(*args, **kwargs): do_memoization = sportsref.get_option('memoize') if not do_memoization: return fun(*args, **kwargs)
from enum import Enum from selectors import EVENT_READ from mementos import memento_factory from event_handlers import SocketAcceptorHandler, ChannelHandler from loggers import EmptyLogger from reactor import AsyncReactor from readers import DefaultReader ArgsMementoMetaclass = memento_factory( "ArgsMementoMetaclass", lambda cls, args, kwargs: (cls, ) + args) class ChannelState(Enum): IDLE = 0 READY = 1 CLOSED = 2 class Channel(object): def __init__(self, reader=None, crucial=True, state_notifier=None, reactor=None, logger=None): super(Channel, self).__init__() self._state_notifier = state_notifier # handler = handler or ChannelHandler(self) self._reactor = reactor or AsyncReactor() self._logger = logger or EmptyLogger()
def get_class_instance_key(cls, args, kwargs): """ Returns a unique identifier for a class instantiation. """ identifiers = [id(cls)] for arg in args: identifiers.append(id(arg)) identifiers.extend((k, id(v)) for k, v in list(kwargs.items())) return tuple(sorted(identifiers)) # used as a metaclass for classes that should be memoized # (technically not a decorator, but it's similar enough) Cached = mementos.memento_factory("Cached", get_class_instance_key) def memoize(fun): """A decorator for memoizing functions. Only works on functions that take simple arguments - arguments that take list-like or dict-like arguments will not be memoized, and this function will raise a TypeError. """ @functools.wraps(fun) def wrapper(*args, **kwargs): do_memoization = sportsref.get_option("memoize") if not do_memoization: