From 916b2a2d666e1f07903892968b6debfe85d29d49 Mon Sep 17 00:00:00 2001 From: Ravachol Date: Sun, 31 Mar 2024 02:31:52 +0800 Subject: [PATCH] use deepcopy on dicts to avoid changing default options --- app/models/audio.py | 2 -- app/models/base.py | 6 +++--- app/models/text.py | 2 -- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/app/models/audio.py b/app/models/audio.py index 80b660c..1fc41c5 100644 --- a/app/models/audio.py +++ b/app/models/audio.py @@ -34,8 +34,6 @@ class Audio(Base): "n":{"enabled": False} } - _options = dict(OPTIONS_DEFAULT) - # generate a noise sample @staticmethod def noise_sample(): diff --git a/app/models/base.py b/app/models/base.py index 9d16616..639959a 100644 --- a/app/models/base.py +++ b/app/models/base.py @@ -2,6 +2,7 @@ base.py the Base model """ +import copy from telebot import TeleBot from telebot.types import Message @@ -11,13 +12,12 @@ class Base: OPTIONS_AVAILABLE = [] OPTIONS_DEFAULT = {} - _options = dict(OPTIONS_DEFAULT) - def __init__(self, bot:TeleBot = None, message:Message = None): self.__bot = bot self.__message = message + self._options = copy.deepcopy(self.OPTIONS_DEFAULT) # get content of this object @@ -33,7 +33,7 @@ def set_options(self, options): if options["bool_options"]: opt = options["options"] # index for mapping from boolean array to options - index = 0 + index = 0 for i in self._options: # change options accordingly, opt with be chopped to the last elements self._options[i]["enabled"] = opt[-len(self.OPTIONS_AVAILABLE):][index] diff --git a/app/models/text.py b/app/models/text.py index 672265a..ffa5957 100644 --- a/app/models/text.py +++ b/app/models/text.py @@ -35,8 +35,6 @@ class Text(Base): "content": string.digits} } - _options = dict(OPTIONS_DEFAULT) - # generate random string, returns the object itself def generate(self, options = None): # check options