design-patterns

Strategy Pattern in Python Functional Redesign

Strategy Pattern in Python Functional Redesign Question: I’m trying to create a program that returns "emergency messages" using the Strategy Pattern. I’m building it in a more functional way in that instead of using abstract classes to define my functions, I just defined the functions and called those. However, I get the error TypeError: text_alarm() …

Total answers: 1

MangoDB using JavaScript Pattern with $in operator

MangoDB using JavaScript Pattern with $in operator Question: Trying to create a MongoDB query with the Pymongo driver that search a database for some dog breeds, but I need to do pattern matching due to data being of differing quality. I know I cannot use Regex in a $in style query so I am using …

Total answers: 2

Picking up files with specific names in Python

Picking up files with specific names in Python Question: I’m designing a tool that should only pick up EXR image files from the input folder that follow the naming convention: u#_v#.exr or u#v##.exr (where # denotes whole numbers or positive non-zero integers). All other files should be ignored. My working code is given below. However, …

Total answers: 1

Can some one explain me the this regex pattern in python? re.findall("[a-zA-Z*,*-!*.]"

Can some one explain me the this regex pattern in python? re.findall("[a-zA-Z*,*-!*.]" Question: re.findall("[a-zA-Z*,*-!*.]" This regular expression checks for valid letters, ".", "-", "-", "!" in a word. I understood the first part [a-zA-Z] Can someone please explain this? [*,*-!*.] Asked By: anonymous a || Source Answers: You can use the online tool regex101 to …

Total answers: 2

Best way to create classes that only differ by one method?

Best way to create classes that only differ by one method? Question: I am implementing multiple RL agents which share a lot of common attributes and methods but differ in only one. Namely the one that calculates the td_error. Out of the top of my head I can think of 3 options to implement this: …

Total answers: 1

how to print out the letters of the word with a certain pattern? ex. abcdef print a-6times, b-5 time, c-4 times, and the next letter again 6 times?

how to print out the letters of the word with a certain pattern? ex. abcdef print a-6times, b-5 time, c-4 times, and the next letter again 6 times? Question: text=’abcdef’ leng=len(text) mylist=list(text) def string(): for i in range(leng-3): for j in range(leng-i): print(text[i],end=”) print() string() #itrieddoingreversetoo #theoutputshouldbe: ”’ aaaaaa bbbbb cccc dddddd eeeee ffff each …

Total answers: 1

Using a classmethod to wrap class functions, but not as an alternate constructor

Using a classmethod to wrap class functions, but not as an alternate constructor Question: I’ve gotten in the habit of using classmethods to wrap class functions rather than as an alternate constructor. It seems great to keep all relevant functions within the class namespace rather than defining a wrapper function in the general namespace. I’ve …

Total answers: 2

Is it bad practice to include non-validating methods in a pydantic model?

Is it bad practice to include non-validating methods in a pydantic model? Question: I’m using pydantic 1.3 to validate models for an API I am writing. Is it common/good practice to include arbitrary methods in a class that inherits from pydantic.BaseModel? I need some helper methods associated with the objects and I am trying to …

Total answers: 2

What is a good pattern for validating multiple complex conditions?

What is a good pattern for validating multiple complex conditions? Question: I’m working on a piece of code that needs to validate whether two users are “matched” under several different criteria. If it helps, think of this as a dating app, where we are trying to match people based on age, sexual preference, ethnicity preference, …

Total answers: 4