From 1dd31e3f93584635c05a7c6a16909f98ad87f055 Mon Sep 17 00:00:00 2001 From: DevilXD <4180725+DevilXD@users.noreply.github.com> Date: Sun, 15 Sep 2024 16:27:18 +0200 Subject: [PATCH] Remove OrderedSet, channels have to be re-sorted during selection process anyway --- twitch.py | 8 ++++---- utils.py | 42 +----------------------------------------- 2 files changed, 5 insertions(+), 45 deletions(-) diff --git a/twitch.py b/twitch.py index 9959bba..3fd866f 100644 --- a/twitch.py +++ b/twitch.py @@ -36,7 +36,6 @@ from utils import ( timestamp, create_nonce, task_wrapper, - OrderedSet, AwaitableValue, ExponentialBackoff, ) @@ -724,14 +723,14 @@ class Twitch: elif self._state is State.CHANNELS_FETCH: self.gui.status.update(_("gui", "status", "gathering")) # start with all current channels, clear the memory and GUI - new_channels: OrderedSet[Channel] = OrderedSet(channels.values()) + new_channels: set[Channel] = set(channels.values()) channels.clear() self.gui.channels.clear() # gather and add ACL channels from campaigns # NOTE: we consider only campaigns that can be progressed # NOTE: we use another set so that we can set them online separately no_acl: set[Game] = set() - acl_channels: OrderedSet[Channel] = OrderedSet() + acl_channels: set[Channel] = set() next_hour = datetime.now(timezone.utc) + timedelta(hours=1) for campaign in self.inventory: if ( @@ -757,7 +756,8 @@ class Twitch: # add a list of live channels with drops enabled new_channels.update(await self.get_live_streams(game, drops_enabled=True)) # sort them descending by viewers, by priority and by game priority - # NOTE: We can drop OrderedSet now because there's no more channels being added + # NOTE: Viewers sort also ensures ONLINE channels are sorted to the top + # NOTE: We can drop using the set now, because there's no more channels being added ordered_channels: list[Channel] = sorted( new_channels, key=self._viewers_key, reverse=True ) diff --git a/utils.py b/utils.py index 9367874..f5a0248 100644 --- a/utils.py +++ b/utils.py @@ -19,7 +19,7 @@ from contextlib import suppress from functools import cached_property from datetime import datetime, timezone from collections import abc, OrderedDict -from typing import Any, Literal, MutableSet, Callable, Generic, Mapping, TypeVar, ParamSpec, cast +from typing import Any, Literal, Callable, Generic, Mapping, TypeVar, ParamSpec, cast import yarl from PIL.ImageTk import PhotoImage @@ -327,46 +327,6 @@ class ExponentialBackoff: self.steps = 0 -class OrderedSet(MutableSet[_T]): - """ - Implementation of a set that preserves insertion order, - based on OrderedDict with values set to None. - """ - def __init__(self, iterable: abc.Iterable[_T] = [], /): - self._items: OrderedDict[_T, None] = OrderedDict((item, None) for item in iterable) - - def __repr__(self) -> str: - return f"{self.__class__.__name__}([{', '.join(map(repr, self._items))}])" - - def __contains__(self, item: object, /) -> bool: - return item in self._items - - def __iter__(self) -> abc.Iterator[_T]: - return iter(self._items) - - def __len__(self) -> int: - return len(self._items) - - def add(self, item: _T, /) -> None: - self._items[item] = None - - def discard(self, item: _T, /) -> None: - with suppress(KeyError): - del self._items[item] - - def update(self, *others: abc.Iterable[_T]) -> None: - for it in others: - for item in it: - if item not in self._items: - self._items[item] = None - - def difference_update(self, *others: abc.Iterable[_T]) -> None: - for it in others: - for item in it: - if item in self._items: - del self._items[item] - - class AwaitableValue(Generic[_T]): def __init__(self): self._value: _T