Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/pytz/lazy.py
7790 views
from threading import RLock1try:2from collections.abc import Mapping as DictMixin3except ImportError: # Python < 3.34try:5from UserDict import DictMixin # Python 26except ImportError: # Python 3.0-3.37from collections import Mapping as DictMixin8910# With lazy loading, we might end up with multiple threads triggering11# it at the same time. We need a lock.12_fill_lock = RLock()131415class LazyDict(DictMixin):16"""Dictionary populated on first use."""17data = None1819def __getitem__(self, key):20if self.data is None:21_fill_lock.acquire()22try:23if self.data is None:24self._fill()25finally:26_fill_lock.release()27return self.data[key.upper()]2829def __contains__(self, key):30if self.data is None:31_fill_lock.acquire()32try:33if self.data is None:34self._fill()35finally:36_fill_lock.release()37return key in self.data3839def __iter__(self):40if self.data is None:41_fill_lock.acquire()42try:43if self.data is None:44self._fill()45finally:46_fill_lock.release()47return iter(self.data)4849def __len__(self):50if self.data is None:51_fill_lock.acquire()52try:53if self.data is None:54self._fill()55finally:56_fill_lock.release()57return len(self.data)5859def keys(self):60if self.data is None:61_fill_lock.acquire()62try:63if self.data is None:64self._fill()65finally:66_fill_lock.release()67return self.data.keys()686970class LazyList(list):71"""List populated on first use."""7273_props = [74'__str__', '__repr__', '__unicode__',75'__hash__', '__sizeof__', '__cmp__',76'__lt__', '__le__', '__eq__', '__ne__', '__gt__', '__ge__',77'append', 'count', 'index', 'extend', 'insert', 'pop', 'remove',78'reverse', 'sort', '__add__', '__radd__', '__iadd__', '__mul__',79'__rmul__', '__imul__', '__contains__', '__len__', '__nonzero__',80'__getitem__', '__setitem__', '__delitem__', '__iter__',81'__reversed__', '__getslice__', '__setslice__', '__delslice__']8283def __new__(cls, fill_iter=None):8485if fill_iter is None:86return list()8788# We need a new class as we will be dynamically messing with its89# methods.90class LazyList(list):91pass9293fill_iter = [fill_iter]9495def lazy(name):96def _lazy(self, *args, **kw):97_fill_lock.acquire()98try:99if len(fill_iter) > 0:100list.extend(self, fill_iter.pop())101for method_name in cls._props:102delattr(LazyList, method_name)103finally:104_fill_lock.release()105return getattr(list, name)(self, *args, **kw)106return _lazy107108for name in cls._props:109setattr(LazyList, name, lazy(name))110111new_list = LazyList()112return new_list113114# Not all versions of Python declare the same magic methods.115# Filter out properties that don't exist in this version of Python116# from the list.117LazyList._props = [prop for prop in LazyList._props if hasattr(list, prop)]118119120class LazySet(set):121"""Set populated on first use."""122123_props = (124'__str__', '__repr__', '__unicode__',125'__hash__', '__sizeof__', '__cmp__',126'__lt__', '__le__', '__eq__', '__ne__', '__gt__', '__ge__',127'__contains__', '__len__', '__nonzero__',128'__getitem__', '__setitem__', '__delitem__', '__iter__',129'__sub__', '__and__', '__xor__', '__or__',130'__rsub__', '__rand__', '__rxor__', '__ror__',131'__isub__', '__iand__', '__ixor__', '__ior__',132'add', 'clear', 'copy', 'difference', 'difference_update',133'discard', 'intersection', 'intersection_update', 'isdisjoint',134'issubset', 'issuperset', 'pop', 'remove',135'symmetric_difference', 'symmetric_difference_update',136'union', 'update')137138def __new__(cls, fill_iter=None):139140if fill_iter is None:141return set()142143class LazySet(set):144pass145146fill_iter = [fill_iter]147148def lazy(name):149def _lazy(self, *args, **kw):150_fill_lock.acquire()151try:152if len(fill_iter) > 0:153for i in fill_iter.pop():154set.add(self, i)155for method_name in cls._props:156delattr(LazySet, method_name)157finally:158_fill_lock.release()159return getattr(set, name)(self, *args, **kw)160return _lazy161162for name in cls._props:163setattr(LazySet, name, lazy(name))164165new_set = LazySet()166return new_set167168# Not all versions of Python declare the same magic methods.169# Filter out properties that don't exist in this version of Python170# from the list.171LazySet._props = [prop for prop in LazySet._props if hasattr(set, prop)]172173174