design-patterns

Recursion error when combining abstract factory pattern with delegator pattern

Recursion error when combining abstract factory pattern with delegator pattern Question: I am learning about design patterns in Python and wanted to combine the abstract factory with the delegation pattern (to gain deeper insights into how the pattern works). However, I am getting a weird recursion error when combining the two patterns, which I do …

Total answers: 2

applying Singleton to Spotipy throws error

applying Singleton to Spotipy throws error Question: Spotipy’s object needs to be passed a credential kw arg my code: import spotipy class Spoti2(spotipy.Spotify): spoty_obj = None @classmethod def __new__(cls, client_credentials_manager): if cls.spoty_obj is None: cls.spoty_obj = super().__new__(cls) print(‘se creo instancia’) return cls.spoty_obj but i get the error: Spoti2.__new__() got multiple values for argument ‘client_credentials_manager’ however, …

Total answers: 1

Multiple calling @property that reduce list

Multiple calling @property that reduce list Question: I’m training my skills of design patterns. With a Factory schema i tried to get and pop example numbers from a imited list. With initialization of seccond account i recieved an IndexError. In debug mote i noticed that between initialisation of acc01 and acc02 I have a 4 …

Total answers: 2

Why use a superclass's __init__ to change it into a subclass?

Why use a superclass's __init__ to change it into a subclass? Question: I’m working on replicating the SHAP package algorithm – an explainability algorithm for machine learning. I’ve been reading through the author’s code, and I’ve come across a pattern I’ve never seen before. The author has created a superclass called Explainer, which is a …

Total answers: 1

Trying to understand the difference in what matches and the resulting output for findall vs finditer

Trying to understand the difference in what matches and the resulting output for findall vs finditer Question: Using findall: import re target_string = "please sir, that’s obviously a clip-on." result = re.findall(r"[a-z]+(‘[a-z])?[a-z]*", target_string) print(result) # result: [”, ”, "’s", ”, ”, ”, ”] Using finditer: import re target_string ="please sir, that’s obviously a clip-on." result …

Total answers: 1

Python Design Question – Concrete classes with different signature

Python Design Question – Concrete classes with different signature Question: I have two classes with similar methods; however, there are some different mandatory parameters in one class. e.g. class NamespaceScoped: def get_object(self, name, namespace): pass def delete_object(self, name, namespace): pass class ClusterScoped: def get_object(self, name): pass def delete_object(self, name): pass I need to create an …

Total answers: 1

python if list_item == re.match

python if list_item == re.match Question: I’m trying to practice regex patterns with conditions in python (googlecollab), but stuck in (if… and…) by getting proper numbers from the list[000 to 999] – i need only numbers, ending with one digit ‘1’ (not 11, 111, 211 – I need only 001, 021, 031, 101), but it …

Total answers: 1

How to have typing support for a static property (using a decorator)

How to have typing support for a static property (using a decorator) Question: Given a static property decorator: class static_property: def __init__(self, getter): self.__getter = getter def __get__(self, obj, objtype): return self.__getter(objtype) @staticmethod def __call__(getter_fn): return static_property(getter_fn) That is applied to a class as follows: class Foo: @static_prop def bar(self) -> int: return 10 Add …

Total answers: 1

Check unique value when define concrete class for abstract variable in python

Check unique value when define concrete class for abstract variable in python Question: Suppose that I have this architecture for my classes: # abstracts.py import abc class AbstractReader(metaclass=abc.ABCMeta): @classmethod def get_reader_name(cl): return cls._READER_NAME @classmethod @property @abc.abstractmethod def _READER_NAME(cls): raise NotImplementedError # concretes.py from .abstracts import AbstractReader class ReaderConcreteNumber1(AbstractReader): _READER_NAME = "NAME1" class ReaderConcreteNumber2(AbstractReader): _READER_NAME = …

Total answers: 2