All Questions
Tagged with python design-patterns
114
questions
0
votes
2answers
55 views
How to handle results output without cluttering code?
My Python project performs a complex set of operations, and it's important to make clear which operations it uses and in what order. Therefore, it has a main method that reads like an overview of the ...
0
votes
0answers
65 views
Download method of type void vs response
Given a method that is widely used and has a void return type:
from somepackage import download_model
from somepackage import get_filename
def download(name, download_path):
response = ...
0
votes
2answers
138 views
Is there a common Python pattern for the scenario “run code, save the output and load it on a rerun”?
I've been using different ad-hoc variations on this, especially in numpy / pandas / data science-y applications. For example let's say I've done some intensive processing that outputs a numpy array ...
0
votes
0answers
42 views
Pipeline pattern for MVC based GUI application?
I've got an application that uses MVC to run individual, isolated scripts to support our office.
There is a root application will open new windows (limit 1 per tool) for each tool opened and each ...
0
votes
1answer
68 views
Python: Function pipeline with multiple return/input values, or use OOP? Best Practices?
I have a 'processing' function and a 'serializing' function. Currently the processor returns 4 different types of data structures to be serialized in different ways.
Looking for the best practise on ...
-2
votes
1answer
83 views
Best practice: keep DB models in one file or split into modules?
I've a Python project with ~30 SQLAlchemy models and I'm not sure where they belong. All models belong to the DB but also to a module, so I'm not sure about the right namespace.
Here are some ideas:
...
0
votes
0answers
73 views
What design pattern is Python's PEP 451 implementing?
PEP 451 provides a mechanism to change what is actually loaded when someone uses import in Python.
After reading PEP 451 and using it in one of my projects I thought that this was an implementation of ...
0
votes
1answer
72 views
Versioning of data handling pipeline elements
I have developed a custom-made Python package, which provides 2 classes to play with: Stage and Step. They operate based on a Chain of Responsibility design pattern, and behave in a way where:
Both ...
-1
votes
1answer
98 views
Refactoring: Pythonic way of reducing the subclasses?
background: so, I am working on an NLP problem. where I need to extract different types of features based on different types of context from text documents. and I currently have a setup where there is ...
-1
votes
1answer
54 views
Efficiently keep different data structures synchronised - tree, list of objects
Problem description
I have a pet project to translate TEI XML to standoff JSON and back: https://github.com/standoff-nlp/standoffconverter and I am currently thinking about a rewrite/restructuring.
...
2
votes
1answer
126 views
Converter implementation in Python: class versus module?
I've written a little library that uses the builtin ast library to convert between functions, docstrings, argparse .add_argument, classes, and [soon] more.
Stubs look something like this, with ir ...
0
votes
2answers
90 views
User-friendly parameter parsing from yaml
Problem
I have designed an evaluation tool (in python) and need some help to make it more user friendly. The tool requires ~100 (nested) parameters, which it gets from a yaml file and stores ...
-1
votes
1answer
49 views
Preventing name collision between user-defined modules within a framework
I'm a contributor to a framework that's designed for producing synthetic data. The system allows the end-user to create custom data generators and load them into the framework. Currently we store the ...
4
votes
3answers
190 views
Approach for updating status of a function
When I call a function, I want to receive updates when the function reaches some milestones:
def do_something():
start_with_something()
# update
for x in iterate_something():
# ...
0
votes
1answer
43 views
What's a proper way to call a chain of methods that modify an instance attribute?
I've got an Algorithm class whose responsibility is to find if a given word is in a list of words.
As part of doing that, the algorithm first has to lowercase the words, remove punctuation, and ...
4
votes
2answers
917 views
What's wrong with using a Singleton?
I'm working on a Python application in which there are two Singleton classes: App and Configuration.
The former seems straight forward, only ever instantiate one App instance; the latter seems ...
2
votes
1answer
130 views
Python: Return or update object
Firstly I am new to Software Engineering and my last question was closed. I am doing my best to ask relevant questions and improve. If you are going to down vote my question I'd really appreciate if ...
0
votes
0answers
44 views
Is it idiomatic to use protobufs as containers within a service?
I love gRPC, but I find every step of the protobuf process rather frustrating (particularly in Python). Even though they are structurally similar to data structures composed of lists and dicts, you ...
2
votes
1answer
83 views
How to simplify a complex factory pattern?
I have a function which takes the incoming request, parses the data and performs an action and posts the results to a webhook. This is running as background as a Celery Task. This function is a common ...
1
vote
4answers
261 views
Design pattern for a function class
I have been experimenting with the idea of function classes as explained in this article and Composition applied to function dependencies as described in the following questions:
https://...
2
votes
2answers
650 views
Chat part of application - using UDP or TCP?
I'm trying to build a section of my app where two users can message each other. I've read about TCP and UDP and it seems like TCP is more suited due to ordered packet delivery. However, TCP requires a ...
1
vote
1answer
84 views
Choosing a suitable design structural pattern for a use case
I have the following models.
First, there is a Vector which has a circular DNA sequence.
Second, there is a LinearizedVector which could one of below classes.
LinearizedVectorBase:
...
1
vote
1answer
133 views
Should I use an `else lif` or an `if` for the second of two consecutive assertions?
Here's an example of my question in python. Notice there's only a very subtle difference: changing an if to an elif. There's no difference in behavior; if the first if statement is executed, the ...
0
votes
0answers
66 views
Architecture Design of Command&Control application center for displays
I am facing dilemma on how to best design the following functionality. What design patterns and OOD principles should I use.
For simplicity sake following are basic requirements:
displays type ...
1
vote
2answers
68 views
When using data and domain models, where should validation take place? And how should errors be fed back to the user?
When using data and domain models, where does validation take place? Both or just
For example:
class UsersDB():
def create(self, user_data):
# Create user here
return ...
0
votes
1answer
134 views
Design Patterns for Passing Large Quantity of Parameters in Machine Learning
I am looking to better understand best practices for handling large quantity of parameters. I am particularly interested in the types of parameters involved in machine learning code bases and ...
-2
votes
1answer
88 views
Is this an example of a wrapper function?
I'm working on an application in python. One of the functions we have is called
def createThread(user, status, title, ...):
# some code here
system.db.runUpdateQuery("INSERT INTO table(value,...
-3
votes
2answers
96 views
Seeking appropriate design pattern(s) to describe most function-based mathematical problems
I have been looking for a good, general design pattern to implement simple mathematical structures where functions have the following properties:
know which parameters they contain, parameters are "...
-1
votes
1answer
90 views
Not sure what design pattern works better in my case
Currently I have a large ontology like this:
There are different categories of variants and each can be somatic or germline.
There are more common behaviours between same variants, like somatic CNVs ...
1
vote
1answer
113 views
Appropriate design pattern for developing rules and outcomes [duplicate]
In the exercice i´m developing bellow, i´m applying the chain of responsibility pattern. The pattern works correctly but i don´t think it's the best approach. I think the rule design pattern should be ...
1
vote
1answer
188 views
mongodb queries architecture - resolving lots of nested referenced objects
I have an angular 8 application, with a Python + MongoDB API on the backend.
At present, I have 4 collections, namely: Users, Tasks, Companies and Groups.
All of these resource types are retrievable ...
3
votes
2answers
306 views
Does my class violate the Single Responsibility Principle in SOLID?
I want to ask:
Whether the Role class violates Single Responsibility Principle in SOLID ? I think deleteAccount() is not belong to Role class but Role class is way to extend code in the future
...
2
votes
1answer
136 views
Object attributes as special parameter objects in python
I am writing a library that can be used with some GUI and would like to be able to build an interface where user can see and/or change most of the object's parameters, and also write some of these ...
0
votes
0answers
89 views
Passing Python Class to CLI arguments
So i want to make a crawler in python but not the usual UA/middlewares way.
My idea was a modulable architecture, which pass the callable class as arguments in a cli. This is kind of odd, i don't know ...
1
vote
2answers
146 views
Observer reporting to multiple layers up from the bottom of hierarchy
There is a swarm of objects. When a new unit of certain kind appears on the frame, the swarm integrates this object by calling some add_new_unit method. Above the swarm is a controller abstraction, ...
1
vote
2answers
251 views
Should there not be methods intended to be only called from inside of the package, but from the outside of the class they're defined in?
Note: This is a follow-up to this question on StackOverflow.
I have to write a wrapper in Python to an external API, accessible through HTTP. The code is supposed to be publicly available on GitHub.
...
0
votes
2answers
80 views
How to design classes of a self-driving machine, if I need a simulation?
Background: I'm working on a project with a self-driving machine with a tank-like control, somehting like:
forward()
left()
right()
stop()
The code is running on a raspberry pi. The GPIO outputs are ...
6
votes
2answers
3k views
Design pattern for similar classes that require different implementations
Edited: Update is at the bottom
There could be a common or best practice for this scenario, but I am unfamiliar with it. However, it could very easily be a matter of subjective opinion on how one ...
1
vote
2answers
77 views
Building a plugins-based code in Python
I have a program which perform different actions depending on the plugins that are passed. For example, python main.py -m foo -m bar will perform the actions of foo and bar.
The structure of the ...
3
votes
0answers
147 views
Data processing pipeline design for processing data
I have a use case for which I need to build a data processing pipeline
Customer contact leads data coming from different data sources like csv, data base, api has to be first mapped to a universal ...
0
votes
2answers
479 views
Dependency injection in a loop?
Suppose I have a loop (in Python syntax):
xml = "<x>...</x>"
for i in arr:
j = f(x) # some complex computation
obj = Class(i, j)
xml = obj.run(xml)
Does it make sense to use ...
3
votes
0answers
106 views
Python3: How to change the design of a class hierarchy to improve access to objects there buried?
I asked this question already at stackoverflow together with a serialization related part and at codereview for the design part only. Since the design related part receives no answers or comments on ...
5
votes
1answer
3k views
Delegation pattern in Python: is it unpopular? Is it considered not Pythonic?
As a Ruby/Rails person, I often deal with method delegation. It's often convenient to delegate a method to a composed object or a constant. Ruby even has a def_delegator helper method to easily build ...
0
votes
1answer
100 views
Passing messages through a chain of containers in python
When I write python code for simulations, I often end up with the following situation: I have a class describing the general environment which contains a list of instances of a class that describes ...
2
votes
1answer
127 views
Traversing over two similar object structures using a pattern
Let's say I have an Object Structure like this that I import data into from a source:
Reporting:
Body:
ReportingEntity:
DocSpec
Reports[]:
ConstEntities[]
DocSpec
...
-1
votes
2answers
149 views
How to choose the most suitable solution for a problem given some choices?
To ilustrate my main concern let's start by considering a "trivial" typical problem, data filtering & parsing coming from a process and dumping the information onto something {gui console, file, ...
9
votes
3answers
4k views
Better conditional debugging pattern?
Given the need to log only in debug mode the easiest way would be to use if conditions:
def test(x, debug=False):
if debug:
print(x)
# ...Some more code
if debug:
print("...
2
votes
2answers
550 views
How to implement Strategy pattern for combined behaviours
Strategy pattern solves the necessity of applying a certain algorithm/behaviour depending on the object's type itself. So you can iterate over a bunch of similar objects and call the same function ...
2
votes
0answers
57 views
How to interface with very badly written code in Python? [duplicate]
I have to extend some very badly written Python code (no documentation, very interdependent, barely any encapsulation, very static, everything hard-coded, etc..) and therefore do I obviously have to ...
0
votes
1answer
71 views
Uncertainly importing modules with Python
I'm developing a module that should be able to interact with other modules such as numpy or PIL. To accept parameters and test if, for example, a parameter is an numpy.ndarray, I'd need to import ...