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
-1 votes
0 answers
9 views

Can you help in converting the JSON into a pandas dataframe

[Converting the below JSON to PYTHON DATAFRAME is not working. I have tried to flatten the nested JSON using metadata and record path but I am getting error and unable to convert to a flattened ...
1 vote
1 answer
29 views

The notion of configurable strategies

I'm designing an algorithm that matches entries based on some notion of "proximity" (for the sake of discussion, assume we're matching floats). Furthermore: The input is a scalar and a ...
0 votes
1 answer
37 views

Best practices for tuning an eval of an ML model

Let's say that I have a service like that takes in some text, classifies it, and then outputs the classification. The critical path between entry and classifier is as follows: Query -> ...
1 vote
0 answers
45 views

Business rule or repository implementation rule in Clean Architecture

I'm studying Clean Architecture with python and got into this "problem" of choosing where to implement a rule (that I considered a Business Rule, maybe I'm wrong) I have 2 Entities @...
3 votes
2 answers
529 views

Method object creation in Python data model

I've been reading the article on the data model of Python on its Reference website, and have been confused about this part: When the attribute is a user-defined method object, a new method object ...
1 vote
1 answer
63 views

Dependency resolution of tasks which have crontab information

I want to illustrate my problem statement with a use case. I am building a trading system that acts as a container for deploying trading strategies. The trading strategy comprises dependent tasks. Let'...
2 votes
3 answers
108 views

Referencing transient class attributes

I've just started dipping my feet into OOP. Is it considered bad practice to have classes that reference attributes that depend on another function being called and thus may not exist (version 1)? I'...
16 votes
2 answers
22k views

What are hooks? [closed]

I keep coming across this term hooks in various programming articles. However I don't understand what they are, and how can they be used. So I just wanted to know what is the concept of hooks; if ...
0 votes
0 answers
13 views

python tkinter GUI design, when to use the withdraw() and when to use the destroy() method

I am building my first app in Python tkinter, and have been struggling with the proper structure to do so. One of the issues I am having is when it would be better to destroy a window, and when I ...
1 vote
1 answer
61 views

When and how to be more loose when checking for equivalence of two objects?

Code in Python as example. For instance I have an emoji class with following attributes and dunder method: class Emoji: def __init__(self, name: str, animated: bool = False, id: Optional[int] = None)...
0 votes
1 answer
85 views

Disambiguating Python terms, what is the difference between "type hint" vs "type annotations"?

Colloquially, type hint and annotations are used interchangeably in Python. After reading PEP 484, I was unable to disambiguate the terms "type hint" vs "type annotations" from the ...
4 votes
1 answer
420 views

random.choice in other languages

Python has a nice function in the standard library random.choice which returns a random element from a sequence. But none of the other languages I've developed in have thought to include such a ...
6 votes
5 answers
2k views

Chess: Efficiently deciding whether a check mate has been made

This question addresses building a Chess engine. I've done just that, and in my humble opinion quite elegantly. But I'm missing my ending conditions. Here are some pseudopython functions that I use. ...
1 vote
2 answers
158 views

How to document pass-through parameters

I'm currently working with a pre-existing class (not written by me) that has functions which call other functions, passing through the parameters to the helper functions. Sometimes there are layers to ...
0 votes
1 answer
65 views

Dependency Injection for dynamic objects

I am learning about Dependency Injection and I have been recently implementing the following classes for an app that executes commands over ssh using Python. I am confused about whether I am using it ...
-2 votes
1 answer
120 views

Is it recommended practice to put a "slash" after the self parameter in Python methods?

With the introduction of the slash character to specify position-only arguments, is it recommended to put a slash in every method after the self parameter? For example: class A: def something(self,...
-1 votes
1 answer
69 views

Calling helper functions in a Python `__init__` function

Problem I am currently working with a class that necessarily has a very complicated initialization function (>350 lines of code) given that many computations and attributes need to be performed and ...
0 votes
2 answers
2k views

pep8 (D103) I need docstrings for my unit test functions too?

Perhaps im not defining my pytests right but im seeing this: Does pep8 demand a docstring for each unit test function too? I cant find pep8 docs specific to this, wondering if pep8/flake8 is unit ...
1 vote
1 answer
120 views

What to name class that applies methods of another class?

This is a pretty vague question, but, sometimes, I'm not very good at naming specific tasks, but I know it's very important and I don't want to name it something ungood. I have the following file ...
5 votes
2 answers
4k views

What is considered best practice for custom exception classes?

Python has many strong conventions but I'm unclear on how best to manage exceptions for my module. I know it's generally good practice to define a custom exception for your module. E.g.: class ...
0 votes
1 answer
464 views

Exchange data between python and angular with flask

I am trying to determine which is the best arquitecture to my application, I am planning to use python, mysql, angular and flask as an intermediate between python and angular. I have all the shots of ...
5 votes
2 answers
726 views

Why should imports be made at the beginning

Why is it considered good style to do all imports in a python application at the beginning, even if what is imported is used only once? I have been programming only shorter pieces of software for a ...
8 votes
2 answers
18k views

Exception handling in Python - Am I doing this wrong (and why?)

I've read many questions and articles on exception handling in Python (and in general), but I still think that it's the most confusing thing ever. I ended up doing something like this: # error class ...
1 vote
1 answer
882 views

Django - should I create related objects immediately or on demand?

Let's say you have a ForeignKey MyModel.related_model and this related_model has all fields either null=True or default=something. Like User.userprofile which I use on several places in my project. ...
3 votes
0 answers
68 views

How to structure python modules/packages according to dependecy inversion

If I am working on a project, say it has this file structure: car/ body/ __init__.py doors.py bonnet.py engine/ cyclinderhead/ __init__.py pistons.py ...
1 vote
1 answer
55 views

How would you implement a execution graph like dependency for python scrips?

I am often confronted to this problem in my scripts and I feel I am lacking some software design culture, I hope this is the right place to ask and sorry if this sounds too simple. I write many ...
1 vote
2 answers
190 views

What's a good workflow for including mechanical sketches in code comments?

My team writes a lot of Python code in PyCharm to do quick simulations and calculations for our mechanical engineering work. Often, we'd like to include a little image (a hand drawn sketch, CAD ...
1 vote
1 answer
1k views

How to distribute a project with all its dependencies?

We are developing a system for a customer that does not want to allow installation of packages from outside repositories. The project is in Python and defines its dependencies via setuptools; most of ...
-1 votes
1 answer
142 views

Clean code about constants in Python

My question regards to constants in Python. Which is more "convenient" or "acceptable" among software engineers in general: Putting classes of constants in another file called &...
-2 votes
1 answer
141 views

Is it possible to predict last 3 digits of a closing stock price? [closed]

So, i just start learning in AI field and i want to develop my skill more. So, i just curious is it possible to predict last 3 digits of a closing stock price? which mean the output result will run ...
0 votes
5 answers
117 views

Should I "modularize" my configuration file into different files?

I have a simulation in Python which reads its configuration from a toml file. Since I have tons of parameters, the toml file can grow quite large. This is an example file, similar in structure to my ...
1 vote
1 answer
118 views

Long-running compute-intensive tasks in APIs: background workers?

I'm working on a design for an HTTP based API that takes in requests to perform a long-running task that requires CPU and RAM intensive processing. To give an impression of the compute requirements, ...
0 votes
1 answer
160 views

Clean Architecture: How to decouple the Repository from a CommandHandler?

We are just starting with event driven (and clean) architecture. So far we have two main entrypoints, a consumer (reader from a Redis Stream / Kafka Topic), and an API. As this is almost a modular ...
5 votes
2 answers
197 views

Why does CPython has both C and .python versions of modules?

The CPython implementation seems to have the same modules written both in C (e.g., datetime in .c) and also in .py for the same module (e.g., datetime in .py). My question is which version is used ...
0 votes
3 answers
169 views

Python best practice when logging optional arguments

I have a method that accepts one or more optional arguments and I'd like to log them, following the best practice of lazy interpolation of log values: def frobnicate(a: str, b: int, c: typing.Optional[...
0 votes
1 answer
75 views

PyPI Package Names Governance

The Python Package Index (PyPI) is a public repository, and it seems that anyone can upload packages to it using a tool like twine. The openness of the system raises some questions: Say, a person/...
7 votes
2 answers
5k views

Guidelines for returning None vs. raising Error for Python string-validating functions

Let's say we have a function that checks if a given pattern occurs in a string. The default behaviour is that if the pattern matches, then it returns the pattern. So far so good but what is the ...
1 vote
2 answers
313 views

add function to input of builtin python logger

For simplicity, assume my application logs only dictionaries. I want to add a step to Python logging for my application to prevent logging any dictionary with the key password, i.e., def clean_log(...
-1 votes
1 answer
37 views

How to handle config/env vars in a library project

I am building a new Python library project to be consumed by several of my application projects. The existing code consumes environment variables for various configuration settings. Should my ...
34 votes
4 answers
7k views

A concrete example of Python's "only one way to do it" maxim? [closed]

I am learning Python and am intrigued by the following point in PEP 20 The Zen of Python: There should be one-- and preferably only one --obvious way to do it. Although that way may not be ...
-1 votes
1 answer
130 views

Best practices for naming python utils / extending core modules?

So a lot of the time my utils end up with a structure that mirrors the core library. I might end up writing a multiline version of str.center, an itertools-y function that returns the first or last ...
1 vote
1 answer
200 views

Properly "exposing" a variable from a class in Python

I have a Launcher class that manages (check if they're alive and if not respawns them, also starts, etc.) Clusters which are processes launched through the multiprocessing Discord module. To post ...
1 vote
2 answers
123 views

What is a good way to call a unit-tested function provided by a library/package?

Consider the function foo provided by package X in Python. I want to test the different functionalities of X.foo, and then use X.foo in my code. To make sure that I am using X.foo as it was tested, I ...
12 votes
3 answers
2k views

Should I choose repeated code in unit test or test logic? Can I avoid both?

When writing unit tests, I feel that there is a trade-off between code repetition and test logic. Example of my current (likely flawed) approach: To test this function (overly simple function for ...
0 votes
2 answers
57 views

Design Review: Queuing tasks to add rows of data followed by task to send email

I need to send personalized emails to a filtered set of users (~100,000) on every fortnight. I am using Marketing cloud API for it. The way I am thinking of designing the system is as follows: ...
0 votes
2 answers
95 views

TL suggests that testing, refactor, and tool usage could avoid the need for verbose logging

Additional Context I make infrastructure for automated device testing. Debugging can be very time consuming because the devices have many states and are constantly being updated with new builds which ...
0 votes
1 answer
53 views

How to properly isolate tests for dataframes with grouping?

I have the following problem: I would like to test complex business logic for each test case completely separately, i.e. all tests should run in parallel. I don't want the test for customer #43 to ...
3 votes
1 answer
211 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 ...
-1 votes
2 answers
185 views

What is the best practice of conditions style in Python?

We have a function that processes the user request if it is not None, how best to style it? I did not find a better choice in PEP8 Style 1 def user_handler(user): if db.getUser(user) is not None: ...
-1 votes
1 answer
88 views

How to use lazy import to speed up a list of known types in python? [closed]

I have an application in which the user can describe an analysis job via a yaml like analysis: learner: name: LinearRegression args: solver: liblinear and then it is parsed to ...

1
2 3 4 5
38