Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

0
votes
1answer
38 views

Which is better approach - using subprocess vs communicating over socket?

I have been working on a python project for sometime which has been structured in following manner: We have a function like this: def execute_cmd(cmd): (out, err) = subprocess.Popen(cmd)....
0
votes
0answers
29 views

Neural Networks object creation from OOP perspective [duplicate]

Let's say that I have a class representing the Neural Network. The neural network is composed of three bigger units: a subpart_1, subpart_2 and subpart_3, being called in such a way, that the output ...
2
votes
3answers
270 views

How to deal with configuration files that are becoming too complex

There is a db with tables of genes. And these genes would need to be annotated based on the client's needs. So if a gene matches condition x, then add a new column y and value z to that gene. Every ...
0
votes
0answers
56 views

How should we do error/usage-logging and business metrics logging in one application / environment? [closed]

Challenge We offer several services for our clients which give them more insights in their data (sales, inventory, customers, leads). Currently we are investigating ways to get more hold on how our ...
1
vote
1answer
64 views

How to avoid confusion with possible referenced before assignment when it won't be (Python) [closed]

I have some code equivalent to this: state = True if xyz: state = False try: value = foo() except: handle_exception() state = False if state: bar(value) On the last line it will ...
0
votes
1answer
38 views

Combining web app and CLI to python package

I'm writing a program in Python that has two different entry points. There's a CLI that processes some data and needs to be installed to run hourly on a server, this populates a database. There's also ...
1
vote
0answers
12 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
1answer
89 views

Pythonic way of writing `for my_var in my_iterable` [closed]

A very common pattern in python is: for something in ['an', 'iterable']: func(something) I almost always see the iterable as a list (like in this example), but it could very well be a tuple, set,...
-4
votes
4answers
118 views

Creating one function for multiple purposes vs multiple functions for one purpose each [closed]

I have one function that is used to compute distances of an object in 3 different ways. Is one of the following two methods considered better practice: Creating 3 different functions, one each for ...
15
votes
6answers
3k views

How to implement float hashing with approximate equality

Let's say we have the following Python class (the problem exists in Java just the same with equals and hashCode) class Temperature: def __init__(self, degrees): self.degrees = degrees ...
0
votes
0answers
35 views

Querying the domain model

Background: I am working on a document classifier which needs to classify document based on some keywords. The list of keywords and the category is provided as the configuration parameter. The ...
0
votes
1answer
44 views

How do I save user data in a file and populate the GUI/Software based on that file?

So I'm developing a GUI that consists of many widgets such as checkbox, buttons, radio-buttons, combo-box using Python Qt5. This GUI is to perform some calcualtion based on user choices. I'm trying to ...
0
votes
0answers
13 views

Is there a way to import a trained Neural Network to the 32 bit Version of Python 2.7? [migrated]

I have a Neural Network control mechanism which I implemented for a model written in Python 3.6. using tensorflow. Now I would like to use this Neural Network control mechanism in the real world, ...
0
votes
0answers
70 views

Should I repeat unnecessary else condition? [duplicate]

I would like to know what the best way is for dealing with a situation where I repeatedly check a condition within a function using an if-elif-else chain, and if the condition is else, I raise an ...
1
vote
2answers
51 views

Balancing function call overhead and testability in a code that is a part of the deep learning model training loop

I am currently implementing the transformer architecture for sequence to sequence problems. Key part of the model is the attention mechanism, which is basically a matrix multiplication, followed by a ...
0
votes
1answer
163 views

Lazy assignment of python variables

I have the following if statement and I feel it could be made simpler to read by breaking the terms up into their own named variables. if os.path.isfile(os.path.join(dirname, 'file.json')) \ or (os....
12
votes
3answers
2k views

Working through the single responsibility principle (SRP) in Python when calls are expensive

Some base points: Python method calls are "expensive" due to its interpreted nature. In theory, if your code is simple enough, breaking down Python code has negative impact besides readability and ...
1
vote
1answer
56 views

Using semantic version with trunk based development for Python package

I am trying to understand trunk based development with semantic versioning for a Python project. Has anyone had any experience of adopting a trunk based development with semantic versioning for ...
0
votes
2answers
140 views

What is the definition of implementation in programing languages? What is CPython?

I came across this word "implementation". CPython is one of the most common implementations of Python. What exactly is an implementation? I researched a bit on how a Python code runs. First, it is ...
0
votes
0answers
56 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 ...
0
votes
2answers
61 views

Is it worth to use a sql db for just two columns (possibly extensible)?

So, first of all hello to everyone, I hope you are having a good day :) I will give you a little context. I coded a program for my job, in which I save to a database Errors ID and their solutions. It ...
2
votes
1answer
70 views

Best practices on testing a function based on third party service

I'm trying to figure out how to create unit tests for a function, which behavior is based on a third party service. Suppose a function like this: def sync_check(): delta_secs = 90 now = ...
1
vote
3answers
209 views

What is a good method/practice I can employ to keep identical code snippits in two places in sync? Also, help documenting functionals

If I could get some input on the design of this, I would be grateful as well. Note that I'm programming in python. There's a function F that takes lots of data, runs some analysis on it (taking ...
1
vote
2answers
116 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, ...
4
votes
2answers
102 views

Pandas based visual data cleaning tool

I am looking to build an web-application which would allow user to upload csv file - see preview of few rows in the file and then apply different cleansing and standardisation rules. For example the ...
-4
votes
1answer
115 views

Running python script in a node server? [closed]

My younger brother has made a small rock, scissor and paper python game running in console. I want to host it on my own domain, running on a digitalOcean droplet. I have no more experience with python,...
1
vote
2answers
88 views

Python method docstring: to use or not to use Args: Returns:

Default docstring from PyCharm automatically creates template with Args: and Returns: sections. However I have observed that this prompts people to say obvious things, frequently just stating the ...
0
votes
1answer
114 views

Is the complexity of this code quasilinear time?

Algorithm explanation: based on an unsorted list I want to find the indexes of the values in another sorted list. Note that all values are unique and the two lists have the same values, only in ...
4
votes
1answer
75 views

Maintaining parallel libraries: Binary files access + Metadata Database ORM design

I'm dealing with a higher-level data abstraction that I would appreciate some input on. I'm working on an application that uses a large data lake. The data lake is consisted of thousands of large ...
-2
votes
2answers
117 views

What is the best way to run untrusted hooks/plugins?

I'm building a data processing system where users can submit hooks to execute on incoming data. The hooks are untrusted and should execute in a sandbox with access only to a limited API that I expose ...
0
votes
1answer
59 views

Python - Type Hinting specific sized Vectors

Considering a class method that takes a "vector" (Tuple or List of either int or float) of defined values such as the following: import sys from numpy import isnan, array, float64 class Shape: """ ...
1
vote
1answer
81 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. ...
-2
votes
1answer
65 views

method taking a class parameter

I have recently begun studying UML. All is going fine so far until I saw the following: This is a class Called Point2D It has 2 attributes which are x, type float and y, type float. It has 3 methods ...
1
vote
2answers
133 views

Python - Should a function write to a file or return text to be written?

Currently I have something like the following def writeThis(fileHandle, name): fileHandle.write('Hello ' + name) writeThis(fh, 'Alice') Something about this doesn't feel right however, it ...
-2
votes
1answer
71 views

Packages with only __init__.py - Possible issues?

Considering a Python Project structure such as the following, where there are "empty" packages with __init__ files simply pulling code from the lib folder: . ├── foo │   ├── fred │   │   └── __init__....
0
votes
1answer
100 views

Best practices for handling application specific exceptions?

Is it considered to be a good practice to convert all types of exceptions (exceptions from internal logic of application + exceptions from application's external dependencies - for example: File ...
8
votes
1answer
142 views

Importing Python modules at the time of use

Often in my own personal Python libraries, I do something like this: class MyClass: # ... def plot(self): import someGraphicsLibrary as graphicslib graphicslib.plot(self....
-1
votes
2answers
487 views

How do I extract only bold text from this image? [closed]

I am looking for an approach / algorithm for using OCR (like Tesseract) to extract only bold text from an image. The Python code I wrote can already identify small letters and numbers, but it cannot ...
1
vote
1answer
63 views

Declaring constants: one file or separate?

I currently have a small list (< 20) of constants that can be segmented into the following three parts: - main script (tokens, log file location) - database setup (username, passwords) - API (API ...
2
votes
0answers
56 views

Can/Should I make an automatic installation of new python libraries after a git pull?

Imagine the following situation: I'm working on a python project, and I install the library antigravity with pip. I add the function fly() which uses the library, and I commit and push the changes. ...
1
vote
1answer
55 views

Sharing Docstrings between similar functions?

Assuming we have different classes with methods that possess the exact same description, however execute code a bit differently for the same return type. class Foo: """This is the Foo class ...
0
votes
2answers
75 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 ...
0
votes
2answers
75 views

Are there any issues with having having the main data in an object's state?

class DataFrameAnnotation: def __init__(self, df: pd.DataFrame): self.df = df def transformation_1(self): self.df + 1 def transformation_2(self): self.df + 1 def main(self): ...
1
vote
1answer
83 views

Is using enums to filter attributes considered a code smell?

I was doing some reading here and one of the suggested answers states: In short: Don't try to decide how an object might react to some action from the outside, tell it what happened, and let it ...
2
votes
0answers
73 views

Is assigning a method in the constructor good practice? [closed]

Here's a class with a method that calls different functions based on a parameter set in the constructor: functions = { "arg1": f1, "arg2": f2, "arg3": f3 } class C: def __init__(self,...
-1
votes
1answer
25 views

python library for stdin stdout managing [closed]

I am writing a test in pytest for a software. This test relies a lot on console output generated by the software. The flow is something like: * start the program * send some commands to the program *...
-1
votes
1answer
105 views

time complexity for 3 foor loops different leangth [duplicate]

I have this method arr // input new_ seq = [] for i in arr: new_seq.append(i) __new_seq = [x for i, x in enumerate(arr) if x not in new_seq] for j in __new_seq: new_seq.append(j) ...
1
vote
2answers
389 views

How can I integrate Python code with c# code? [closed]

I want to integrate code python (hierarchical clustering algorithm) with code C#. (The idea of ​​the project is to divide similar people and put them into classes using the algorithm. We use language ...
0
votes
0answers
19 views

what's the proper way to interpret SREM results from REDIS

Background I am writing a REST API that exposes a method that allows the caller to delete a resource from the REDIS database. Because I'm using a SET, when you try to do an SREM, it will just return ...
0
votes
1answer
37 views

WebApp for ETL with visual mapping - read csv and map it to data model

a few years ago I wrote a python script for reading CSV, handling the headers, filtering data, renaming stuff via RegEx...bascially to do various ETL stuff. This was done using a exhaustive ...