class _SyncRate(_RateBase): sync_lock: SyncLock = field(init=False, default_factory=lambda: SyncLock()) sync_semaphore: SyncSemaphore = field( init=False, default_factory=lambda: SyncSemaphore()) def __post_init__(self) -> None: super().__post_init__() self.sync_semaphore._value = self.size @property def running(self) -> int: with self.sync_lock: # noinspection PyProtectedMember,PyUnresolvedReferences return self.size - self.sync_semaphore._value def get_decorator(self) -> Callable: def decorator(*args, **kwargs): with self.sync_semaphore: wait_time = self.get_wait_time() if wait_time > 0: sync_sleep(wait_time) return self.fn(*args, **kwargs) decorator.rate = self return decorator def get_wait_time(self) -> int: with self.sync_lock: return super().get_wait_time()
def copy(self): """ Copies the queue. Returns ------- new : ``SyncQueue`` """ with self._lock: new = object.__init__(type(self)) new._results = self._results.copy() new._waiter = None new._cancelled = self._cancelled new._lock = SyncLock() return new
def __init__(self, iterable=None, max_length=None, cancelled=False): """ Creates a new ``SyncQueue`` instance with the given parameters. Parameters ---------- iterable : `iterable` of `Any` Iterable to set the queue's results initially from. max_length : `int` Maximal length of the queue. If the queue would pass it's maximal length, it's oldest results are popped. cancelled : `bool` Whether the queue should be initially cancelled. """ self._results = deque(maxlen=max_length) if iterable is None else deque(iterable, maxlen=max_length) self._waiter = None self._cancelled = cancelled self._lock = SyncLock()
class _SyncMemo(_MemoBase): sync_lock: SyncLock = field(init=False, default_factory=lambda: SyncLock())
class _SyncMemoize(_MemoizeBase): _sync_lock: SyncLock = field(init=False, default_factory=lambda: SyncLock()) def get_raw_key(self, *args, **kwargs) -> Tuple[Hashable, ...]: if self.keygen is None: raw_key = self.default_keygen(*args, **kwargs) else: raw_key = self.keygen(**self.get_args_as_kwargs(*args, **kwargs)) if not isinstance(raw_key, tuple): raw_key = [raw_key] raw_key = tuple(raw_key) return raw_key def get_behavior(self, *, insert: bool, update: bool) -> Callable: def get_call(*, fn: Callable) -> Callable: @wraps(self.fn) def call(*args, **kwargs) -> Any: raw_key = self.get_raw_key(*args, **kwargs) key = self.get_key(raw_key) with self._sync_lock: memo: _SyncMemo = self.get_memo(key, insert=insert) if memo is None: return fn(*args, **kwargs) self.expire_one_memo() with memo.sync_lock: if ( (insert and not memo.memo_return_state.called) or (update and memo.memo_return_state.value is not _MemoZeroValue) ): memo.memo_return_state.called = True try: memo.memo_return_state.value = fn(*args, **kwargs) except Exception as e: memo.memo_return_state.raised = True memo.memo_return_state.value = e self.bind_key_lifetime(raw_key, key) return self.finalize_memo(memo=memo, key=key) return call return get_call def insert(self, *args, **kwargs) -> Any: return self.get_behavior(insert=True, update=False)(fn=self.fn)(*args, **kwargs) def update(self, *args, **kwargs) -> Callable: def to(value: Any) -> Any: def fn(*_args, **_kwargs) -> Any: return value return self.get_behavior(insert=False, update=True)(fn=fn)(*args, **kwargs) return to def upsert(self, *args, **kwargs) -> Callable: def to(value: Any) -> Any: def fn(*_args, **_kwargs) -> Any: return value return self.get_behavior(insert=True, update=True)(fn=fn)(*args, **kwargs) return to def remove(self, *args, **kwargs) -> None: raw_key = self.get_raw_key(*args, **kwargs) key = self.get_key(raw_key) self.reset_key(key) def get_decorator(self) -> Callable: def decorator(*args, **kwargs) -> Any: return self.insert(*args, **kwargs) decorator.memoize = self return decorator @staticmethod def make_memo(t0: Optional[float]) -> _SyncMemo: return _SyncMemo(t0=t0) def reset(self) -> None: with self._sync_lock: super().reset() def reset_key(self, key: Union[int, str]) -> None: with self._sync_lock: super().reset_key(key)
def __init__(self): self.lock = SyncLock() self.buffer = deque()
parts.append(' ') parts.append(content) index = index + 1 if index == limit: break parts.append('.') CARDS_BY_ID = {} CARDS_BY_NAME = {} CHESUTO_FOLDER = os.path.join(KOISHI_PATH, 'chesuto_data') CARDS_FILE = os.path.join(CHESUTO_FOLDER, 'cards.json') CARDS_FILE_LOCK = SyncLock() PROTECTED_FILE_NAMES = {'cards.json'} class Card(object): __slots__ = ('_length_hint', 'description', 'flags', 'id', 'image_name', 'name', 'rarity') def __init__(self, description, id_, name, rarity, flags=0, image_name=None):