Questions tagged [python]
Python is a dynamically typed, high-level interpreted programming language. Its design focuses on clear syntax, an intuitive approach to object-oriented programming, and making the right way to do things obvious. Python supports modules and exceptions, and has an extensive standard module library. Python is general-purpose and thus used widely, from the web to embedded systems.
2,033 questions
0
votes
2
answers
185
views
How to reduce the number of class instances passed to the __init__() method of a Python class?
I have a Python class called FunctionsManager; its __init__() method is the following:
class FunctionsManager:
def __init__(self, instance_class1, instance_class2, ..., instance_classN):
...
2
votes
1
answer
167
views
Serving several external APIs in Django + Angular application
I'm working on a web-based app that uses Django and Angular. This app uses several external APIs to fetch environmental data from different monitoring networks. We then use these data to perform ...
2
votes
2
answers
255
views
Is it a violation of the three-tier architecture if I inject one service into another inside the logic layer?
I am a beginner programmer with little experience in building complex applications. Currently I'm making a messenger using Python's FastAPI for the back-end. The main thing that I am trying to achieve ...
3
votes
6
answers
602
views
Is OOP really beneficial for enterprise-scale business software compared to procedural languages like ABAP or COBOL?
I'm currently drafting a Python coding standard for internal enterprise use, primarily targeting business applications that involve heavy data access, reporting, and transactional logic.
In this ...
0
votes
2
answers
376
views
Global State, How To Do IT?
im kind of a newbie so take me easy
i face a problem every time i make a project
specially Client projects, which is Global State i always struggle to do it
for example, here is the structure of one ...
3
votes
2
answers
264
views
How to structuring a read/write submodule in OOP Python
I am developing a python package that needs to be able to read/write from/to multiple formats. E.g. foo format and bar format. I am trying to contain the functions relating to each format in a single ...
1
vote
1
answer
114
views
Python: mapping the content of a structured text file to dictionary tree
I'm looking for a method to map the content of a structured text file to a nested dictionary (dictionary tree). The text file consists of (nested) sections with each section starting with the pattern ...
4
votes
2
answers
502
views
Control flow and communication with two separate frontends (maybe with exceptions)?
I am trying to write a backend for use with a completely text based UI for one shot operations (eg. python scriptname arg, executes that argument and exits) and a GUI using the curses library for some ...
3
votes
1
answer
252
views
How to manage working directory in interactive development environments like Jupyter Notebook?
I'm having trouble with managing the working directory in Jupyter Notebook. For example, I have a .py script that requires me to change the working directory to its directory to run it properly. I've ...
0
votes
2
answers
429
views
How to implement a server application that can reload configuration without restart
I have a game server implemented in Python to which clients can connect and play against each other.
I'd like to be able to reload configuration from a config file without restarting the server (as ...
5
votes
2
answers
820
views
In a python project, when should you use __init__.py, __main__.py, and just normal .py?
Say I am making a pdf editor app with the following structure:
├── main.py
├── 📂 drawing_functions/
├── 📂 util/
├── 📂 GUI/
└── 📂 document_handling/
Each of these folders have a collection of ...
5
votes
5
answers
713
views
How to test for performance regression?
I am working on a refactor on a certain package (I can give details if asked). The package involves a clever lazy evaluation of a sort of nested sequence of arithmetic operations. If the numerical ...
0
votes
1
answer
232
views
Best practice when declaring imports with different execution contexts in python?
Say I have a Python project that looks like this:
project/
├── package/
│ ├── __init__.py
│ ├── module1.py
└── main.py
I would like package/ to be self-contained, though it is called from main.py....
5
votes
2
answers
734
views
Why did Python designers decide not to declare vars? [closed]
In Python when you want a local variable, you just assign to it x = 10.
In most modern languages you declare local vars (regardless of type):
JavaScript: let/const/var
Swift: let/var
Kotlin: val/var
...
1
vote
2
answers
548
views
Should an API client class be reusable?
For an API client I am working on, I was wondering, whether the main class should be reusable. This question basically boils down to: should the HTTP client be instantiated in __init__ or __enter__/...
0
votes
0
answers
71
views
Simple packaging for multi-file/module Python deployments
I'm working on a Python codebase which we are trying to improve the modularity of. Both the top level scripts and the modules that they depend on are all in one main repo. We have another repo which ...
-3
votes
2
answers
5k
views
Is it better practice to set default values for optional argparse arguments?
I mention python's argparse as an example but this would apply to any CLI argument parser library for any language. Just generally speaking, at a high level, if there are a lot of optional arguments ...
1
vote
1
answer
359
views
Separation of concerns between business layer, data layer and presentation layer without losing information
I'm developing an api in Fast API using sqlalchemy to manage my ORM classes and operations with the database.
I'm dealing with some design decisions to have my classes as little coupled as possible ...
0
votes
2
answers
729
views
Explain why it's bad to use a middleware to coat error messages as exceptions
We manage a backend application behind a FastAPI REST controller with multiple endpoints.
A member of our team decided to use a middleware on the application which parses the body of the response for ...
2
votes
1
answer
147
views
A Design Pattern for a component which both writes to and reads from an ordered event log
I am searching for a good design for a set of components I am writing for a system.
I believe it is very likely there is a Design Pattern, or set of Design Patterns, which could be combined to solve ...
15
votes
4
answers
5k
views
What side-effects, if any, are okay when importing a python module?
Generally, modules should not have side effects. However, in a lot of cases, the side-effects are hard to avoid or may be desirable. There are also popular packages with on-import side-effects.
Which ...
7
votes
2
answers
726
views
When the stack frames become computationally expensive
I've been experimenting with different data structures and algorithms in Python, Java and C to see in what circumstances function/method inlining could bring meaningful gains in terms of the execution ...
0
votes
1
answer
262
views
Best practices for setting up a test suite for a python library?
note: here library means a package that is installed from some wheel/egg/tar file. as opposed to a regular package which is just a container of modules basically. package usually is used for both as ...
0
votes
1
answer
210
views
Serving part of a web application as a python package for people to install, ok to do?
Background
To preface, I am building a database for the construction of a timing detector. However, I am very new to software engineering and database development. I have been building a website using ...
1
vote
1
answer
979
views
Deciding between logic on the front-end or back-end
Before I begin, I want to say I am very new to this and I am a junior but also solo developer with no seniors to ask for guidance. Please feel free to explain to me that I am not approaching it ...
3
votes
1
answer
609
views
Object-oriented programming design with relational database tables
I want to understand what is considered best-practice to better align with OOP when handling relational databases. I cannot find any online examples where classes and a more maintainable/re-usable ...
-2
votes
1
answer
521
views
Defining functions inside vs outside a class
Say I have a class with a function do_thing that is comprised of multiple steps, which themselves segregate into functions (first_process and second_process). At what point would this be considered ...
1
vote
3
answers
865
views
Architecture to avoid circular imports in Python, specifically a flask server
I have a flask server which uses a controller-service-resource model.
For simplification it is like this:
Controller(parses requests, dispatches to A or B):
ServiceA
ServiceB
However there are ...
2
votes
1
answer
158
views
Refactoring Processor classes
I am writing some python 3 bioinformatics software and was wondering about the best way to write it in an OOP format. I am pretty sure a lot of my classes are violating the SRP principle, but I'm not ...
-3
votes
1
answer
198
views
Is it a bad idea to use NodeJS + Python for back end development? [closed]
I'm working on a platform that consumes and transforms data to make reports and visualize those reports in a client app.
The team has experience with NodeJS and NestJS, and some of us have experience ...
0
votes
0
answers
47
views
Webapp/Webserver architecture patterns for shared components
Motivation:
I am building a webserver that is essentially a dashboard app. Users make requests and get analytics data and visualizations back. Multiple users can be in the same "workspace," ...
1
vote
2
answers
133
views
Dynamic web application hitting database on front page load to fetch profile image
Folks, I am in middle of writing a web application (Python/Flask) where home page has user profile image in the navbar which is coming from a database (blob), I am wondering if this is a good practice....
2
votes
2
answers
933
views
Should private attributes or public attributes be the default in Python classes?
In python we use a leading _ to make object attributes implicitly "private". If we want to give people set/get access we can use the @property decorator. Or, if setting/getting is allowed ...
5
votes
1
answer
512
views
How does one add observability to python code without affecting code quality?
We're working an agile project and designing as we go on a new python commandline app / systemd service for some fancy in-house project. Right now, we're supposed to be adding an observability / ...
-2
votes
2
answers
356
views
Polymorphism with variable default argument count
I'm in the process of writing a library in Python, and I've run into a design problem concerning the use of polymorphism.
I have an ABC with abstract method 'foo':
class A(ABC):
@abstractmethod
...
1
vote
0
answers
517
views
How to structure your Python code with asynchronous and synchronous parts
I have a Python FastAPI server application which naturally guides you towards the asynchronous paradigm.
For legacy reasons, I have to support two backends, one which is purely synchronous and one ...
1
vote
0
answers
76
views
Best design pattern for integrating Google OAuth with existing JWT token-based authentication system
We currently have a backend authentication mechanism in place that utilizes JWT tokens. Users sign in or sign up using email and password, and upon successful authentication, the backend issues JWT ...
-2
votes
1
answer
244
views
Are there some best practices how to build a client library for an API?
I was tasked to create an API client library for our already existing API. I tried to research some ways how to approach this but couldn't find anything. Are there some best practices how to build ...
2
votes
2
answers
589
views
Is this architecture overkill? What is a good way to architect this software?
I have an algorithm I am trying to implement that has steps 1 to 5. There are several different ways I could implement each step. Each calculation step is essentially just an astronomy calculation, ...
1
vote
2
answers
211
views
Using an instance method or a function with conditional logic based on type
I'm developing a data pipeline in Python. We receive a variety of different types of data set that we need to process (e.g. xml, json, csv and some Excel workbooks) - where processing involves parsing ...
0
votes
3
answers
186
views
Python API - store data in SQL AND NoSql
I am a student and am currently programming an API in Python. Among other things, it is possible to register, log in, create a user profile with data, etc.
I would like to be able to store and ...
1
vote
1
answer
106
views
How to test integration with an infinite generator
I'm struggling to (integration) test a component that makes use of an infinite generator.
Background:
At a really high level, this component is essentially a "middleman" in a kafka workflow. ...
-2
votes
4
answers
2k
views
Compare version numbers
I need to compare versions of a software. The requirement is that I need to know if a given version is older (smaller), same or newer (higher) than another one.
The version format is XX.X.XX where:
...
-1
votes
1
answer
183
views
How to structure a modular MITM proxy with python
To explain what I'm trying to accomplish.
Sometimes I visit website1.com, here I can download Linux ISOs but it's a bit annoying because it has a lot of ads, some of which are launched via javascript ...
-3
votes
1
answer
173
views
Learn a framework on a project, or mix languages between backend services [closed]
I am designing my next project, which will do various domain-specific tasks, but all that will be controlled and used via a generic crud web app.
I have been professionally using Java with Spring for ...
7
votes
1
answer
3k
views
Why are there two ways to retrieve values from a dictionary in Python?
In Python there are basically two ways to get a value from a dictionary:
dictionary["key"]
dictionary.get("key")
Is there any - maybe historical - reason for this behavior?
I ...
2
votes
2
answers
1k
views
Splitting up large SQLAlchemy model
Anybody have advice on splitting up a large SQLAlchemy model into smaller parts? I have a ~2000 line model called Article that is becoming difficult to manage. We often have to scroll and scroll to ...
2
votes
1
answer
2k
views
In Python when is absolutely preferable to use a class instead of a module?
Python is the language I use most in this period.
My background in Java
Before start learning Python I have programmed in Java language. In Java all code is written inside the methods of a class and ...
0
votes
0
answers
75
views
How to structure my DB setup?
I have made a small project to try and learn to implement SQLite-DBs into my projects. For the DB part, I use sqlmodel and followed their tutorial. For the creation of the engine however, they use a ...
3
votes
1
answer
220
views
How to structure repositories for a small number of entities?
I am working on a project implemented in DDD style, and I use Repository architecture pattern to persist domain changes. I have multiple roles in domain layer, and that's what raises my question - how ...