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.

Filter by
Sorted by
Tagged with
-2 votes
2 answers
2k views

@staticmethod vs @classmethod vs functions outside of class in Python

I have some static functions in a Python class. The advantage to using the @staticmethod decorator is that it informs the reader that the method doesn't require any information from the class or ...
user avatar
24 votes
8 answers
7k views

Is using lambdas to express intent not pythonic?

PEP 8 states the following about using anonymous functions (lambdas) Always use a def statement instead of an assignment statement that binds a lambda expression directly to an identifier: # Correct: ...
user avatar
0 votes
2 answers
68 views

Restructuring a “processing slip” like object used in many steps

I have worked on a library which processed data through multiple steps. It was written in R, a dynamic programming language where one could just add fields to existing “objects”, which were just ...
user avatar
0 votes
1 answer
138 views

Is microservice approach always best fit for ETL processes?

In our project we are using Django and Django Rest Framework as main application to get/query the data from database and send it to the frontend. Those endpoints are very fast as they should be. ...
user avatar
  • 151
1 vote
1 answer
101 views

Module with various classes in addition to helper functions defined outside of a class

I have a data science codebase that contains several classes. Several of the methods in the classes rely upon various helper functions. These functions will just precede the class definitions in my ...
user avatar
2 votes
1 answer
329 views

How to balance 'efficient' vs 'clean' code? [closed]

I have been coding in python for a little over a year, and I have learned a lot and developed quite a few applications, in the process. I do not program for my profession, I simply program ...
user avatar
1 vote
1 answer
62 views

Creating an attribute of an object versus a method in the class

This question pertains to properly structuring a class. The object of a class I have is instantiated with multiple parameters. Many of these parameters are then manipulated with each other to derive ...
user avatar
0 votes
2 answers
71 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 ...
user avatar
  • 111
0 votes
1 answer
134 views

Python code template for scripts to be run in batch

I will be writing a number of Python scripts which will be run in batch daily on a Windows server. I am trying to develop a template for writing the scripts. My intention is to use a configuration ...
user avatar
1 vote
1 answer
104 views

Programs running under Slurm for > 24 hours are being killed by process management. What are good approaches in Python to work around this issue? [closed]

I often run data processing/machine learning/filesystem-scanning scripts that can take well over 24 hours to complete. For processes with arbitrarily low memory requirements, I can run without using ...
user avatar
0 votes
1 answer
74 views

I'm designing a python api to play a singleplayer game. should I use statemachine with different states?

I'm designing a python api to play a singleplayer card collection game and my prototype includes a state machine which uses pyautogui's locate function to recognize and update current game state ...
user avatar
  • 11
0 votes
0 answers
75 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 = ...
user avatar
-1 votes
1 answer
88 views

How would I detect a physical attack in video form? [closed]

I hope this question isn't too vague because it's more a discussion than a question. I want to write a code which can detect physical violence in a scene (with the end result being the prediction of ...
user avatar
1 vote
1 answer
104 views

legacy refactor and churn

I am trying to work on finding the following metrics New Work - totally new code which does not replace other code. Churn - code that is rewritten or deleted after being written Help Others - where ...
user avatar
  • 141
3 votes
2 answers
955 views

Communication between two apps

I am thinking of creating two applications, one of which (App 1) will be in Django (DRF) and other (App 2)might be Django but might be another more lightweight framework (maybe Flask or plain Django ...
user avatar
  • 151
-3 votes
1 answer
219 views

How to know how much RAM & CPU my script reqire? [closed]

as the title, how to know? is task manager the best way as my friend told me? I code in python.. I'm new here sorry if my question was in the wrong place.
user avatar
  • 11
0 votes
1 answer
148 views

How to write tests in TDD for downloading and unpacking a file?

So I want to write this function that downloads a file and unpacks it in python using TDD. The function will look like this approximately import urllib.request import tarfile def download_and_unpack(...
user avatar
0 votes
1 answer
114 views

How encrypting website data works

If i wanted to encrypt a password on my website before its sent to the server, would i have to encrypt the password in javascript on the frontend for it to be hidden over the interent or could it be ...
user avatar
-1 votes
1 answer
74 views

Generator variable can’t be used more than once [closed]

Python loop generator Looks like I’m missing something with for loop generators… I deliberately made this simple code to test what was happening when I converted my list comprehensions to generators. ...
user avatar
  • 190
2 votes
1 answer
63 views

Python inheritance: how to check the closest abstract parent class?

I'm dealing with two categories of machine learning algorithms. For simplicity, let's call them A and B. There are multiple concrete algorithms in each category, and my goal is to implement all of ...
user avatar
  • 123
-2 votes
1 answer
170 views

How to pre-enter a user input() in Python? [closed]

I am interacting with a 3rd party program that will request 2 user inputs to start running a process. I know what they are going to be and want to preempt them, so that I don't need to enter them ...
user avatar
  • 11
1 vote
0 answers
62 views

How to build a chatbot that is Open Domain but can intervene on specific intents?

As per my knowledge, there are 2 type of Chatbots- Open Domain and Closed Doman Intent Based. I was wondering how are conversational agents created such that can converse as if they are Open Domain, ...
user avatar
1 vote
0 answers
83 views

Correct structure of a data science project: keeping as functions versus object-oriented framework

I'm working on a data science project that involves data ingestion, cleaning and various aggregations and functions of timeseries equipment data primarily using Pandas. I began the project just ...
user avatar
1 vote
1 answer
247 views

Dealing with business logic on DTOs using a DDD

I have a scenario in which an Entity defines an EntityRepresentation that is created from another end. The EntityRepresentation is a value object that implements some logic that should be performed on ...
user avatar
1 vote
0 answers
39 views

Looking for the right design (pattern?) for my webapp and external APIs intgerations

I'm working on a simple CRM (Customer Relationship Management) webapp (Flask and Peewee ORM) and I found myself in a situation where I believe that I should have used a design pattern that I may not ...
user avatar
  • 11
0 votes
1 answer
268 views

Using the same function in two classes? [closed]

I have two classes for making pyqt5 windows. Inside class one I have a button with a function it is connected to. Class One: from PyQt5 import QtGui, QtWidgets, QtCore from PyQt5.QtWidgets import ...
user avatar
0 votes
2 answers
545 views

How can Python, a language that supports Object Oriented Programming, be implemented in C, which is not an Object Oriented Programming language?

To my understanding, C does not have the concept of objects, then how can Python be implemented in C to support something that C can not? How is the concept of "Object" modeled in C? What is ...
user avatar
  • 39
-1 votes
2 answers
86 views

Are there any advantages to using dockerfile dependencies over virtual environment for Python apps?

I am currently working solo on a very small simple python microservice. I started building this app, mostly by habit, in a virtual environment. As I started to get closer to the point where deployment ...
user avatar
  • 115
2 votes
2 answers
94 views

Dynamic loading of objects defined during development for a running system

So I have a design/architectural question. I want to develop a pattern in a programming language that is able to allow a app command-line shell to send commands to a running application to create ...
user avatar
  • 179
1 vote
1 answer
90 views

Imposing an object interface in Python

I wanted to implement a method insertCard in Python which interacts with only a specific type of object called a Card. The Card should always have a cardId and may have additional payload or fields ...
user avatar
  • 163
0 votes
1 answer
1k views

How to structure Python modules so they are accesible from different submodules

This is somewhat similar to this question but I could not find a solution there. I have a project that I've worked on over the past 4 years. I started without any Python knowledge and learned as I ...
user avatar
1 vote
1 answer
638 views

How to organize ORM models and business logic

I have a Python application using SQLAlchemy. It uses around 15 database tables. For example, I have an image table that lists image files manipulated by the application. Most operations involving ...
user avatar
  • 149
1 vote
2 answers
136 views

Automating performance tests for python code in a CI

My apologies in advance for a question that might seem trivial - I am a mostly solo dev in academic environment and a lot of industry best practices don't necessarily make it here. Several of my ...
user avatar
-1 votes
2 answers
483 views

Initializing instance variables from json file

I have a class whose instance variables should be initialized from a file ('settings.json'). That file does not always exist or is sometimes not filled with useful values, so I have to check for that ...
user avatar
-2 votes
4 answers
113 views

Is this a good approach to stop an API function and return relevant error message?

I am writing an API function using DRF where I want the API execution to stop if it fails in any of the steps and return an appropriate response. So I created a custom exception which takes an error ...
user avatar
  • 111
0 votes
1 answer
86 views

Designing a library API to allow for interactivity

The application I'm currently working on is deeply coupled to the UI framework in use (Qt at the moment). I would like to separate the UI-specific code from the rest as much as possible, with the goal ...
user avatar
  • 121
-2 votes
1 answer
74 views

How do you use ad hoc polymorphism/function overloading with functions in Python?

So, let's say you've got a function foobar() which can function with a variable number of parameters inputted into it, and has different behavior for each of them. How do you get this to function ...
user avatar
2 votes
4 answers
578 views

Do all dynamically typed languages not support function overloading?

I have noticed that JavaScript and PHP and Python do not support function overloading. Do all dynamically typed languages not support function overloading? If the answer is yes, then why is that?
user avatar
  • 55
-2 votes
1 answer
72 views

Use 1 to many relationship on same model or split it into two different models?

I'm building a forum application using the Django web framework but I'm not sure how to design the entity relationship diagram when it comes to the Post model. Since one Post can have many replies, ...
user avatar
2 votes
1 answer
141 views

Selective method inheritance

I have a lot of classes that are just a CRUD interface for microservices. They only have a param for the endpoint and some of the methods get_list / get_item / create / update / delete / activate / ...
user avatar
  • 129
-1 votes
1 answer
818 views

Will conversion of a string to a list, and vice versa count in time complexity?

Assume that there is a question where a string needs to be passed. Some modification needs to be done on the string and then returned back. For the programming languages like C where a string is a ...
user avatar
2 votes
3 answers
254 views

Are user-callable recursive functions an anti-pattern?

I have a function in python that calls itself recursively, and has some internal variables passed down the recursion using keyword arguments (that are not listed in the docstring) Is it a problem to ...
user avatar
  • 31
0 votes
2 answers
148 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 ...
user avatar
-1 votes
2 answers
424 views

How do you unit test methods with hard/impossible inputs to hardcode?

I have the following problem. I have a method which takes two other custom objects to work. This objects are required by the method because they have the information that the method requires to ...
user avatar
1 vote
3 answers
166 views

When is Model/View/Controller Too Much?

To preface, I work at a place that I feel misunderstands and overuses MVC, but I also recognize that maybe it's me who doesn't understand it. I'm used the model/view exclusively within the context of ...
user avatar
0 votes
0 answers
112 views

HTTPS headless browser to HTTP localhost Flask

I have two Docker containers: Headless browser grabbing images by running JavaScript Flask server analysing these images (and sending results to another server) I'm using a bridge network to ...
user avatar
  • 9
2 votes
3 answers
166 views

When does a function violate the SRP?

I have a function that was written because several methods used the same sequence of code so an abstraction was useful. The function takes a string representing the path to a registry key and then a ...
user avatar
  • 999
0 votes
2 answers
207 views

Loop outside method or method with internal loop?

If I have a list of objects that need to have an operation performed on each, is there a best practice in abstracting the loop or not? Looping over list and call def func(item): some_op(item) ...
user avatar
  • 999
6 votes
2 answers
221 views

How do you unit test functions split in smaller functions

The problem is the following, suppose we have this functions: from PIL import Image from magiclibrary import perform_some_operation, stack_images def load_image(path: str): if isfile(path): ...
user avatar
0 votes
2 answers
256 views

Which is the best data structure to use when you want to randomly pick elements & use them, but also delete them after use

I have 1000 lines in a text file. I want to read them into some data structure[DS]. After reading them, I will be randomly picking 50 lines from the DS (using a Random Number Generator). Next time 50 ...
user avatar
  • 441

1 2
3
4 5
38