__author__ = 'ziyan.yin'

from threading import local


class Store(local):
    """
    Thread local storage.

        >>> d = Store()
        >>> d.x = 1
        >>> d.x
        1
        >>> import threading
        >>> def f(): d.x = 2
        ...
        >>> t = threading.Thread(target=f)
        >>> t.start()
        >>> t.join()
        >>> d.x
        1
    """

    _instances = set()

    def __init__(self):
        Store._instances.add(self)

    def __del__(self):
        Store._instances.remove(self)

    def __hash__(self):
        return id(self)

    @staticmethod
    def clear_all():
        """Clears all ThreadedDict instances.
        """
        for t in list(Store._instances):
            t.clear()

    clear_all = staticmethod(clear_all)

    # Define all these methods to more or less fully emulate dict -- attribute access
    # is built into threading.local.

    def __getitem__(self, key):
        return self.__dict__[key]

    def __setitem__(self, key, value):
        self.__dict__[key] = value

    def __delitem__(self, key):
        del self.__dict__[key]

    def __contains__(self, key):
        return key in self.__dict__

    has_key = __contains__

    def clear(self):
        self.__dict__.clear()

    def copy(self):
        return self.__dict__.copy()

    def get(self, key, default=None):
        return self.__dict__.get(key, default)

    def items(self):
        return self.__dict__.items()

    def keys(self):
        return self.__dict__.keys()

    def values(self):
        return self.__dict__.values()

    def pop(self, key, *args):
        return self.__dict__.pop(key, *args)

    def popitem(self):
        return self.__dict__.popitem()

    def setdefault(self, key, default=None):
        return self.__dict__.setdefault(key, default)

    def update(self, *args, **kwargs):
        self.__dict__.update(*args, **kwargs)

    def __repr__(self):
        return "<Store %r>" % self.__dict__

    __str__ = __repr__

__author__ = 'ziyan.yin'

from threading import local


class Store(local):
    """
    Thread local storage.

        >>> d = Store()
        >>> d.x = 1
        >>> d.x
        1
        >>> import threading
        >>> def f(): d.x = 2
        ...
        >>> t = threading.Thread(target=f)
        >>> t.start()
        >>> t.join()
        >>> d.x
        1
    """

    _instances = set()

    def __init__(self):
        Store._instances.add(self)

    def __del__(self):
        Store._instances.remove(self)

    def __hash__(self):
        return id(self)

    @staticmethod
    def clear_all():
        """Clears all ThreadedDict instances.
        """
        for t in list(Store._instances):
            t.clear()

    clear_all = staticmethod(clear_all)

    # Define all these methods to more or less fully emulate dict -- attribute access
    # is built into threading.local.

    def __getitem__(self, key):
        return self.__dict__[key]

    def __setitem__(self, key, value):
        self.__dict__[key] = value

    def __delitem__(self, key):
        del self.__dict__[key]

    def __contains__(self, key):
        return key in self.__dict__

    has_key = __contains__

    def clear(self):
        self.__dict__.clear()

    def copy(self):
        return self.__dict__.copy()

    def get(self, key, default=None):
        return self.__dict__.get(key, default)

    def items(self):
        return self.__dict__.items()

    def keys(self):
        return self.__dict__.keys()

    def values(self):
        return self.__dict__.values()

    def pop(self, key, *args):
        return self.__dict__.pop(key, *args)

    def popitem(self):
        return self.__dict__.popitem()

    def setdefault(self, key, default=None):
        return self.__dict__.setdefault(key, default)

    def update(self, *args, **kwargs):
        self.__dict__.update(*args, **kwargs)

    def __repr__(self):
        return "<Store %r>" % self.__dict__

    __str__ = __repr__

Thread local storage.          >>> d = Store()         >>> d.x = 1         >>> d.x         1         >>> import threading         >>> def f(): d.x = 2         ...         >>> t = threading.Thread(target=f)         >>> t.start()         >>> t.join()         >>> d.x         1 Clears all ThreadedDict instances. # Define all these methods to more or less fully emulate dict -- attribute access # is built into threading.local.