From 6e0ba67f80dbdc203acde24135fbd5c50f384223 Mon Sep 17 00:00:00 2001 From: Alessandro Maggio Date: Sat, 13 Feb 2021 11:48:22 +0100 Subject: [PATCH] The use of __slots__ can save more RAM. There are multiple articles online, I've read this: https://tech.oyster.com/save-ram-with-python-slots/ --- .../TwitchChannelPointsMiner.py | 15 +++++++++++ TwitchChannelPointsMiner/classes/Settings.py | 2 +- TwitchChannelPointsMiner/classes/Twitch.py | 2 ++ .../classes/TwitchLogin.py | 12 +++++++++ .../classes/WebSocketsPool.py | 2 ++ .../classes/entities/Bet.py | 17 ++++++++++++ .../classes/entities/EventPrediction.py | 14 ++++++++++ .../classes/entities/Message.py | 11 ++++++++ .../classes/entities/PubsubTopic.py | 2 ++ .../classes/entities/Raid.py | 2 ++ .../classes/entities/Stream.py | 15 +++++++++++ .../classes/entities/Streamer.py | 26 +++++++++++++++++++ 12 files changed, 119 insertions(+), 1 deletion(-) diff --git a/TwitchChannelPointsMiner/TwitchChannelPointsMiner.py b/TwitchChannelPointsMiner/TwitchChannelPointsMiner.py index e6c8560..8f436f3 100644 --- a/TwitchChannelPointsMiner/TwitchChannelPointsMiner.py +++ b/TwitchChannelPointsMiner/TwitchChannelPointsMiner.py @@ -38,6 +38,21 @@ logger = logging.getLogger(__name__) class TwitchChannelPointsMiner: + __slots__ = [ + "username", + "twitch", + "claim_drops_startup", + "streamers", + "events_predictions", + "minute_watcher_thread", + "ws_pool", + "session_id", + "running", + "start_datetime", + "original_streamers", + "logs_file", + ] + def __init__( self, username: str, diff --git a/TwitchChannelPointsMiner/classes/Settings.py b/TwitchChannelPointsMiner/classes/Settings.py index 8794e78..7ffbcfe 100644 --- a/TwitchChannelPointsMiner/classes/Settings.py +++ b/TwitchChannelPointsMiner/classes/Settings.py @@ -1,3 +1,3 @@ # Empty object shared between class class Settings(object): - pass + __slots__ = ["logger", "streamer_settings"] diff --git a/TwitchChannelPointsMiner/classes/Twitch.py b/TwitchChannelPointsMiner/classes/Twitch.py index 9723d33..d5b1cf7 100644 --- a/TwitchChannelPointsMiner/classes/Twitch.py +++ b/TwitchChannelPointsMiner/classes/Twitch.py @@ -29,6 +29,8 @@ logger = logging.getLogger(__name__) class Twitch(object): + __slots__ = ["cookies_file", "user_agent", "twitch_login", "running"] + def __init__(self, username, user_agent): cookies_path = os.path.join(Path().absolute(), "cookies") Path(cookies_path).mkdir(parents=True, exist_ok=True) diff --git a/TwitchChannelPointsMiner/classes/TwitchLogin.py b/TwitchChannelPointsMiner/classes/TwitchLogin.py index 37929d1..2299efe 100644 --- a/TwitchChannelPointsMiner/classes/TwitchLogin.py +++ b/TwitchChannelPointsMiner/classes/TwitchLogin.py @@ -16,6 +16,18 @@ logger = logging.getLogger(__name__) class TwitchLogin(object): + __self__ = [ + "client_id", + "token", + "login_check_result", + "session", + "session", + "username", + "user_id", + "email", + "cookies", + ] + def __init__(self, client_id, username, user_agent): self.client_id = client_id self.token = None diff --git a/TwitchChannelPointsMiner/classes/WebSocketsPool.py b/TwitchChannelPointsMiner/classes/WebSocketsPool.py index f1f6a8f..67349b0 100644 --- a/TwitchChannelPointsMiner/classes/WebSocketsPool.py +++ b/TwitchChannelPointsMiner/classes/WebSocketsPool.py @@ -22,6 +22,8 @@ logger = logging.getLogger(__name__) class WebSocketsPool: + __slots__ = ["ws", "twitch", "streamers", "events_predictions"] + def __init__(self, twitch, streamers, events_predictions): self.ws = [] self.twitch = twitch diff --git a/TwitchChannelPointsMiner/classes/entities/Bet.py b/TwitchChannelPointsMiner/classes/entities/Bet.py index eaab53c..46a336f 100644 --- a/TwitchChannelPointsMiner/classes/entities/Bet.py +++ b/TwitchChannelPointsMiner/classes/entities/Bet.py @@ -42,6 +42,12 @@ class OutcomeKeys(object): class FilterCondition(object): + __slots__ = [ + "by", + "where", + "value", + ] + def __init__(self, by=None, where=None, value=None, decision=None): self.by = by self.where = where @@ -52,6 +58,15 @@ class FilterCondition(object): class BetSettings(object): + __slots__ = [ + "strategy", + "percentage", + "percentage_gap", + "max_points", + "stealth_mode", + "filter_condition", + ] + def __init__( self, strategy: Strategy = None, @@ -80,6 +95,8 @@ class BetSettings(object): class Bet(object): + __slots__ = ["outcomes", "decision", "total_users", "total_points", "settings"] + def __init__(self, outcomes: list, settings: BetSettings): self.outcomes = outcomes self.__clear_outcomes() diff --git a/TwitchChannelPointsMiner/classes/entities/EventPrediction.py b/TwitchChannelPointsMiner/classes/entities/EventPrediction.py index a55f520..57ea6bc 100644 --- a/TwitchChannelPointsMiner/classes/entities/EventPrediction.py +++ b/TwitchChannelPointsMiner/classes/entities/EventPrediction.py @@ -5,6 +5,20 @@ from TwitchChannelPointsMiner.utils import float_round class EventPrediction(object): + __slots__ = [ + "streamer", + "event_id", + "title", + "created_at", + "prediction_window_seconds", + "status", + "final_result", + "box_fillable", + "bet_confirmed", + "bet_placed", + "bet", + ] + def __init__( self, streamer: Streamer, diff --git a/TwitchChannelPointsMiner/classes/entities/Message.py b/TwitchChannelPointsMiner/classes/entities/Message.py index 2e0888c..450764c 100644 --- a/TwitchChannelPointsMiner/classes/entities/Message.py +++ b/TwitchChannelPointsMiner/classes/entities/Message.py @@ -4,6 +4,17 @@ from TwitchChannelPointsMiner.utils import server_time class Message(object): + __slots__ = [ + "topic", + "topic_user", + "message", + "type", + "data", + "timestamp", + "channel_id", + "identifier", + ] + def __init__(self, data): self.topic, self.topic_user = data["topic"].split(".") diff --git a/TwitchChannelPointsMiner/classes/entities/PubsubTopic.py b/TwitchChannelPointsMiner/classes/entities/PubsubTopic.py index b7ccbb1..8972325 100644 --- a/TwitchChannelPointsMiner/classes/entities/PubsubTopic.py +++ b/TwitchChannelPointsMiner/classes/entities/PubsubTopic.py @@ -1,4 +1,6 @@ class PubsubTopic(object): + __slots__ = ["topic", "user_id", "streamer"] + def __init__(self, topic, user_id=None, streamer=None): self.topic = topic self.user_id = user_id diff --git a/TwitchChannelPointsMiner/classes/entities/Raid.py b/TwitchChannelPointsMiner/classes/entities/Raid.py index df8d680..cd3a525 100644 --- a/TwitchChannelPointsMiner/classes/entities/Raid.py +++ b/TwitchChannelPointsMiner/classes/entities/Raid.py @@ -1,4 +1,6 @@ class Raid(object): + __slots__ = ["raid_id", "target_login"] + def __init__(self, raid_id, target_login): self.raid_id = raid_id self.target_login = target_login diff --git a/TwitchChannelPointsMiner/classes/entities/Stream.py b/TwitchChannelPointsMiner/classes/entities/Stream.py index 59ddb1f..92b9ff8 100644 --- a/TwitchChannelPointsMiner/classes/entities/Stream.py +++ b/TwitchChannelPointsMiner/classes/entities/Stream.py @@ -10,6 +10,21 @@ logger = logging.getLogger(__name__) class Stream(object): + __slots__ = [ + "broadcast_id", + "title", + "game", + "tags", + "drops_enabled", + "viewers_count", + "__last_update", + "spade_url", + "payload", + "watch_streak_missing", + "minute_watched", + "__minute_watched_timestamp", + ] + def __init__(self): self.broadcast_id = None diff --git a/TwitchChannelPointsMiner/classes/entities/Streamer.py b/TwitchChannelPointsMiner/classes/entities/Streamer.py index 3eee2b6..fdf22bb 100644 --- a/TwitchChannelPointsMiner/classes/entities/Streamer.py +++ b/TwitchChannelPointsMiner/classes/entities/Streamer.py @@ -11,6 +11,14 @@ logger = logging.getLogger(__name__) class StreamerSettings(object): + __slots__ = [ + "make_predictions", + "follow_raid", + "claim_drops", + "watch_streak", + "bet", + ] + def __init__( self, make_predictions: bool = None, @@ -37,6 +45,24 @@ class StreamerSettings(object): class Streamer(object): + __slots__ = [ + "username", + "channel_id", + "settings", + "is_online", + "stream_up", + "online_at", + "offline_at", + "channel_points", + "minute_watched_requests", + "viewer_is_mod", + "stream", + "raid", + "history", + "streamer_url", + "chat_url", + ] + def __init__(self, username, settings=None): self.username = username.lower().strip() self.channel_id = 0