Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Code Reviews

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Posts tagged python

19 child tags

Use for questions about the Python programming language. Python is a multi-paradigm, dynamically typed, multipurpose programming language.

Consider adding a major version tag such as [python-2.7] or [python-3.x] when this is relevant for the question.

This tag doesn't have a detailed wiki yet.

80%
+6 −0
Q&A Understanding mutable default arguments in Python

Consider this code example: def example(param=[]): param.append('value') print(param) When example is repeatedly called with an existing list, it repeatedly appends to the list, as ...

3 answers  ·  posted 11d ago by Karl Knechtel‭  ·  last activity 11d ago by Karl Knechtel‭

Question python
50%
+1 −1
Q&A Is there a text version of pickle?

Is there a Python serialization format that has capabilities similar to Pickle, but is text based? The problem I always have with pickle is that it's binary, so I can't manually view or edit the d...

0 answers  ·  posted 13d ago by matthewsnyder‭

Question python
84%
+9 −0
Q&A What is the point of pipx?

Background Many Python programs now recommend installing with pipx, and there is a sense that you shouldn't install with pip anymore, you should use tools like pipx. Main Question However, what ...

3 answers  ·  posted 1mo ago by matthewsnyder‭  ·  last activity 14d ago by tripleee‭

Question python-3
50%
+0 −0
Q&A Best practices to write functions for both execution modes in Tensorflow, eager and graph mode

I regularly run into the problem that I have a Python function that I want to use in both, eager and graph execution mode. I therefore have to adjust the code so that it can handle both situations....

1 answer  ·  posted 24d ago by daniel_s‭  ·  edited 20d ago by daniel_s‭

Question python
66%
+2 −0
Q&A Is it okay to use python operators for tensorflow tensors?

TL;DR Is (a and b) equivalent to tf.logical_and(a, b) in terms of optimization and performance? (a and b are tensorflow tensors) Details I use Python with Tensorflow. My priorities are Make t...

1 answer  ·  posted 25d ago by daniel_s‭  ·  edited 21d ago by Anonymous‭

Question python
42%
+1 −2
Q&A Listen for key events in a CLI app

I have a Python program like this: done = False while u and not done: i = u.pop() print(f"Processing {i}") do_big_task(i) finish_up() Since this takes a long time, the user m...

1 answer  ·  posted 25d ago by matthewsnyder‭  ·  last activity 22d ago by mr Tsjolder‭

Question python-3
60%
+1 −0
Q&A Can't use tf.timestamp() from within @tf.function with XLA / jit_compile=True

I would like to use tf.timestamp() when it is available (eager mode and graph mode without XLA), and use 0. (or a better fallback if there is one) when it is not available (with XLA; @tf.function(j...

0 answers  ·  posted 24d ago by daniel_s‭

Question python
75%
+7 −1
Q&A Automatically install all packages needed

When running various Python scripts, I often need to do this annoying dance: $ python script.py ... ModuleNotFoundError: No module named 'foo' $ pip install foo $ python script.py ... Module...

3 answers  ·  posted 4mo ago by matthewsnyder‭  ·  last activity 29d ago by Anonymous‭

Question python-3 pip
75%
+4 −0
Q&A How can I schedule a later task in Python?

I want my CLI Python program to schedule a task, and then exit. After some times has passed (say 10 minutes) the task should execute. The task can be a Python method or a shell command, whatever i...

3 answers  ·  posted 1mo ago by matthewsnyder‭  ·  last activity 30d ago by [Object object]‭

Question python
77%
+5 −0
Q&A How can I properly type-hint methods in different files that would lead to circular imports?

I am writing a Python package where I have two classes in different files that (indirectly) depend on each other. If I wouldn't care about type-hints, there would be no problem, but unfortunately,...

2 answers  ·  posted 1mo ago by mr Tsjolder‭  ·  edited 1mo ago by Alexei‭

Question python mypy
71%
+3 −0
Q&A How can a Python program send itself to the background?

Is it possible for a Python program to send itself in the background? For example, on Linux you can do nohup some_cmd & and any program will run in the background. Some programs also support s...

1 answer  ·  posted 1mo ago by matthewsnyder‭  ·  last activity 1mo ago by r~~‭

Question python
50%
+1 −1
Q&A How to resolve a "ValueError: dimension 't' already exists as a scalar variable" arising when I am using xarray.Dataset.assign_coords()?

I have the following xarray Dataset: d: <xarray.Dataset> Dimensions: (x: 79, y: 63, t: 1) Coordinates: * x (x) float64 0.9412 1.882 2.824 3.765 ... 71.53 72.47 73.41 74.35 * ...

1 answer  ·  posted 2mo ago by Ivan Nepomnyashchikh‭  ·  last activity 2mo ago by mr Tsjolder‭

Question python xarray
75%
+4 −0
Q&A Readable syntax for executing many callables with useful side effects

In Python, multiprocessing is easy to do if you follow a "list projection" paradigm. Say you want to take a list of inputs X and apply some function f to every x_i, such that y_i = f(x_i) and the y...

1 answer  ·  posted 2mo ago by matthewsnyder‭  ·  last activity 2mo ago by mr Tsjolder‭

Question python-3
66%
+4 −1
Q&A How can I build a string from smaller pieces?

Suppose I have some variables like: >>> count = 8 >>> status = 'off' I want to combine them with some hard-coded text, to get a single string like 'I have 8 cans of Spam®; b...

2 answers  ·  posted 2mo ago by Karl Knechtel‭  ·  last activity 2mo ago by Karl Knechtel‭

Question python
71%
+3 −0
Q&A How can I output / display multiple values, with controlled spacing, without building a string?

I know that I can display a single string in the terminal window like print("example") (or similarly with a string in a variable), and I know how to open text files and write to them. I also know ...

1 answer  ·  posted 2mo ago by Karl Knechtel‭  ·  last activity 2mo ago by Karl Knechtel‭

Question python
61%
+6 −3
Q&A Why are commas not needed for modulo string formatting when printing?

Suppose I have two variables that are called animal and age, and print them as a string in the console like so: animal = "giraffe" age = 25 print("A %s can live up to %d years" %(animal,age)) ...

3 answers  ·  posted 7mo ago by Bennshinpoes‭  ·  edited 2mo ago by Karl Knechtel‭

Question python
71%
+3 −0
Q&A How to compress columns of dataframe by function

Problem How can I compress each column of a dataframe to the output of a function (i.e., mean), preserving columns? MWE import pandas as pd data = {"A": [1, 2, 3, 4], "B": [5, 6, 7, 8]} ...

2 answers  ·  posted 8mo ago by young_souvlaki‭  ·  last activity 2mo ago by mr Tsjolder‭

Question python pandas
71%
+3 −0
Q&A How to resolve the mypy error "Returning Any from function declared to return 'Dict[str, Any]'" in Python?

I have a function that loads JSON data and is declared to return a dictionary with string keys and values of any type (Dict[str, Any]). However, mypy is raising an error stating that I am returning...

1 answer  ·  posted 2mo ago by ShadowsRanger‭  ·  last activity 2mo ago by Moshi‭

Question python mypy
66%
+2 −0
Q&A "Move symbol" refactor for Python in Visual Studio Codium

Is there a way to add support for "move symbol" in Python code for VS Codium? PyCharm can do this, but I am looking for a VSC solution. PyLance can do this, but PyLance itself is closed source, a...

0 answers  ·  posted 2mo ago by matthewsnyder‭  ·  edited 2mo ago by matthewsnyder‭

Question python
33%
+2 −6
Q&A How do I use an existing AI model to classify pornographic images? [closed]

In the last few months, AI has advanced considerably, notably in the area of generating images. We now have powerful models like DALL-E, Stable Diffusion, etc. These are quite competent at generati...

0 answers  ·  posted 3mo ago by matthewsnyder‭  ·  closed 2mo ago by Alexei‭

Question python-3
50%
+2 −2
Q&A Slicing a dictionary using a string variable

I am working on adding uncertainty to some of my dynamics within a larger Monte Carlo simulation. As a way to perform multiple operations on each value, I decided to make and use a new function to ...

1 answer  ·  posted 2mo ago by megalomaniac‭  ·  edited 2mo ago by megalomaniac‭

Question python
42%
+1 −2
Q&A Program freezing in `post_comments_to_lemmy` function due to potential infinite loop

I'm experiencing an issue where my program appears to freeze when executing the post_comments_to_lemmy function. This function fetches comments from the GitHub API and posts them to a Lemmy instanc...

1 answer  ·  posted 3mo ago by ShadowsRanger‭  ·  last activity 3mo ago by __blackjack__‭

Question python
20%
+0 −6
Q&A how to use one list to find a similar list in another list python

I want a way to make it so that I can use list B to find out if a copy of itself exists in list A. listA = ['1', '1', '0', '1', '0', '1', 'down'] 2 ['0', '0', '1', '1', '1', '1', 'up'] 2 ['1', '...

1 answer  ·  posted 11mo ago by Solaranfel1366‭  ·  last activity 3mo ago by Alias Cartellano‭

Question python
57%
+2 −1
Q&A Can pandas be used as a database backend for persistent storage?

Question What is the current state of the art database app? How does it compare to SQL? Can pandas be used in place of either? If not, is there something that bridges the gap between SQL and pand...

2 answers  ·  posted 3mo ago by young_souvlaki‭  ·  last activity 3mo ago by matthewsnyder‭

Question python pandas
80%
+6 −0
Q&A How do I find disjoint sets in a dataset

I have a dataset of car bookings like this: car_id user_id 1 1 2 1 1 2 3 3 1 2 3 3 In this dataset, two separate groups/sets of cars and users...

2 answers  ·  posted 3mo ago by andreas‭  ·  edited 3mo ago by matthewsnyder‭

Question python-3
66%
+2 −0
Q&A Remove entries by two-column ID everywhere, that meet a condition somewhere

I have a dataset showing electrical current and resistance measurements on various cells of different dies. There are multiple measurements for each cell. If a cell is ever observed to have resist...

3 answers  ·  posted 5mo ago by young_souvlaki‭  ·  edited 3mo ago by Alexei‭

Question python pandas
77%
+5 −0
Q&A How to configure Python pip to look for packages in a private index first?

When I run pip install foo, pip looks for foo in PyPi. I want it to look for it first in a private repo, let's say pypi.bar.com. Only if foo cannot be found in pypi.bar.com, should pip then look f...

1 answer  ·  posted 3mo ago by matthewsnyder‭  ·  last activity 3mo ago by tripleee‭

Question python-3 pip
50%
+1 −1
Q&A How to migrate after SQLalchemy schema changes?

When I set up my object mappings with SQLalchemy, everything works well enough. It even creates the tables for me if they don't exist. However, if I decide to add/remove columns (aka fields) from ...

0 answers  ·  posted 4mo ago by matthewsnyder‭  ·  edited 4mo ago by Alexei‭

Question python-3 sqlalchemy
71%
+3 −0
Q&A Is it possible to get the current function in a trace function?

I'm trying to subclass pdb to have a debugger that, in case of a call to a decorated function, can "step in" the decorated function directly and skip the decorator content altogether. A well-behave...

1 answer  ·  posted 5mo ago by luser‭  ·  last activity 5mo ago by r~~‭

Question python
77%
+5 −0
Code Reviews JSON log formatter

Here's a JSON log formatter for Python. I want to be able to log details of exceptions (and have some capability to debug-by-logs). I want to be able to log extra data in JSON format (in addition...

1 answer  ·  posted 5mo ago by misk94555‭  ·  last activity 5mo ago by SLott56‭

Question python
57%
+6 −4
Q&A Why can parentheses cause exceptions in Python when using for loops?

Using parentheses in Javascript is always needed when you want to create functions, if/else statements and for/while loops. But in Python it has a function that is called tuples. When using an if s...

1 answer  ·  posted 6mo ago by Bennshinpoes‭  ·  edited 6mo ago by trichoplax‭

Question python
71%
+3 −0
Q&A How do I get something similar to dictionary views, but for sequences?

The dictionary methods .keys(), .values(), and .items() all return view objects. Said objects reflect any changes to the underlying dictionary. This is often useful. Is there a way to get such a v...

1 answer  ·  posted 6mo ago by ajv‭  ·  last activity 6mo ago by r~~‭

Question python python-3
71%
+3 −0
Q&A Python Parallel plotting and and input reading

Hello everyone, I am seeking help with understanding why multithreading does not work correctly in this example: import time import matplotlib.pyplot as plt import concurrent.futures voltage...

1 answer  ·  posted 8mo ago by kek‭  ·  edited 8mo ago by kek‭

Question python
75%
+4 −0
Q&A How to programmatically get current wallpaper file path?

I have written a screensaver program that, in part, uses the current wallpaper for some visual effects. However, finding the current wallpaper in different operating systems and environments is les...

1 answer  ·  posted 8mo ago by ghost-in-the-zsh‭  ·  last activity 8mo ago by ghost-in-the-zsh‭

Question python-3
75%
+4 −0
Q&A What's causing mypy to give an `[assignment]` error in this nested for loop?

I started adding types to my (working) solution to Exercism's "Kindergarten Garden" exercise, to learn how typing with python and Mypy (strict) works. While doing so, I ran into a Mypy error that I...

2 answers  ·  posted 9mo ago by true_blue‭  ·  last activity 9mo ago by Moshi‭

Question python mypy
28%
+0 −3
Q&A Built-in way to compute coefficient of variation in pandas

Background The coefficient of variation is: defined as the ratio of the standard deviation to the mean Question Is there a built-in function for this? Tried I know I can do df.std() / df...

1 answer  ·  posted 10mo ago by young_souvlaki‭  ·  last activity 10mo ago by cuzzo‭

Question python pandas
66%
+4 −1
Q&A How to provide additional information when raising exception

MWE filename = "bad_dir" print(f"File not found: {filename}.") raise(FileNotFoundError) Question How do I raise an exception while adding additional information? In the example above, the...

1 answer  ·  posted 11mo ago by young_souvlaki‭  ·  last activity 11mo ago by Alexei‭

Question python
45%
+3 −4
Q&A Create a list of Niven numbers in Python

There's this new challenge on Code Golf CD and I'm using Python to do it. A little bit of golfing already took place, so the code might look a bit messy for you. Anyway, I'm proud of what I've writ...

2 answers  ·  posted 1y ago by General Sebast1an‭  ·  last activity 1y ago by Dirk Herrmann‭

Question python
66%
+2 −0
Q&A How to plot table from pandas dataframe

MWE import pandas as pd import numpy as np import matplotlib.pyplot as plt df = pd.DataFrame(np.random.rand(9, 4), columns=['a', 'b', 'c', 'd']) df.plot.bar(table=True) # don't want plot, ...

1 answer  ·  posted 1y ago by young_souvlaki‭  ·  last activity 1y ago by __blackjack__‭

Question python pandas
77%
+5 −0
Q&A What is the Python Global Interpreter Lock?

What exactly is the Python Global Interpreter Lock (GIL)? As someone who is relatively new to Python, is this something I need to be aware of, or is this just some implementation detail of the inte...

1 answer  ·  posted 1y ago by Moshi‭  ·  last activity 1y ago by Derek Elkins‭

Question python python-gil
71%
+3 −0
Q&A How to add vertical lines for visual separation in pandas plot

MWE import pandas as pd import numpy as np import matplotlib.pyplot as plt df = pd.DataFrame(np.random.rand(9, 4), columns=['a', 'b', 'c', 'd']) df.plot.bar() plt.show() Question Ho...

2 answers  ·  posted 1y ago by young_souvlaki‭  ·  last activity 1y ago by samcarter‭

Question python pandas
28%
+0 −3
Q&A Need help in migrating a Python2 custom middleware to current Python3 version

class ExtendUserSession(MiddlewareMixin): """ Extend authenticated user's sessions so they don't have to log back in following 15 minutes (set by Django's default `SESSION_COOKIE_A...

0 answers  ·  posted 1y ago by roronoa_zoroDR‭  ·  last activity 1y ago by Monica Cellio‭

Question python django-drf
75%
+4 −0
Code Reviews A state machine in Python

I've written the following code implementing a state machine in Python, and I'd like some feedback on it. The basic idea is that each state has a set of corresponding actions that trigger a state ...

2 answers  ·  posted 1y ago by celtschk‭  ·  last activity 1y ago by Nick Alexeev‭

Question python-3
75%
+4 −0
Q&A Is it possible to write protocols for enumerations in Python?

Having recently learned about protocols in Python, I now wonder if you can write a protocol for enumerations. That is, a protocol that says that you are supposed to pass an enum that has certain it...

1 answer  ·  posted 1y ago by celtschk‭  ·  last activity 1y ago by r~~‭

66%
+4 −1
Q&A Load environment variables from .env file in Python 3 [closed]

In Python 2, I was able to create a file named .env within a project folder like so: # .env MY_ID=abc123 TOKEN=4567890 Then in a Python file in the same directory, I could read these variable...

0 answers  ·  posted 1y ago by qohelet‭  ·  closed 1y ago by Alexei‭

Question python python-3
81%
+7 −0
Q&A Console scripts in virtual environment do not output to terminal in git bash

I have console scripts in my virtual environment in \env\Scripts, installed with packages (e.g. black, pytest, coverage). If I try to execute these in Git Bash, the output is not returned to the te...

1 answer  ·  posted 1y ago by boudewijn21‭  ·  last activity 1y ago by boudewijn21‭

Question python venv
81%
+7 −0
Code Reviews Detecting balanced parentheses in Python

The problem Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets are closed by ...

4 answers  ·  posted 2y ago by Vinicius Brasil‭  ·  last activity 1y ago by Dirk Herrmann‭

Question python
71%
+3 −0
Code Reviews A class to access dicts using attribute syntax

I've written a class that allows to access dictionary entries of arbitrary dicts with appropriate string keys through attribute access syntax on an instance of the class. My questions are: Is...

2 answers  ·  posted 2y ago by celtschk‭  ·  last activity 1y ago by luser‭

Question python
62%
+3 −1
Q&A Comparing two excel files with Python based on changes

I have two tables: Table1: Name Description Amount 123 Description123 123 456 Description456 456 789 Description789 666 101 Description777 101 133 De...

1 answer  ·  posted 2y ago by sfrow‭  ·  last activity 1y ago by ibmx‭

Question python pandas
77%
+5 −0
Code Reviews Counting Sundays without Python datetime module

The problem You are given the following information, but you may prefer to do some research for yourself. 1 Jan 1900 was a Monday. Thirty days has September, April, June and November. All t...

1 answer  ·  posted 2y ago by Vinicius Brasil‭  ·  edited 2y ago by hkotsubo‭

Question python