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
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 »
Q&A

Posts tagged python

17 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.

66%
+4 −1
Q&A Why are commas not needed for modulo string formatting when printing?

I'm still a novice in Python. But have come across a question about format specifiers. For example, if I have two variables that are called animal and age. And print a string in the console using f...

2 answers  ·  posted 24d ago by Bennshinpoes‭  ·  last activity 6d ago by Mithical‭

66%
+2 −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]} ...

0 answers  ·  posted 1mo ago by young_souvlaki‭

Question python pandas
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 2mo ago by kek‭  ·  edited 2mo ago by kek‭

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 2mo ago by ghost-in-the-zsh‭  ·  last activity 2mo ago by ghost-in-the-zsh‭

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 3mo ago by true_blue‭  ·  last activity 3mo 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 4mo ago by young_souvlaki‭  ·  last activity 4mo ago by cuzzo‭

Question python pandas
22%
+0 −5
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', '...

0 answers  ·  posted 5mo ago by Solaranfel1366‭  ·  edited 5mo ago by Alexei‭

Question python list
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 5mo ago by young_souvlaki‭  ·  last activity 5mo ago by Alexei‭

Question python exception
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 7mo ago by General Sebast1an‭  ·  last activity 7mo ago by Dirk Herrmann‭

Question python function
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 8mo ago by young_souvlaki‭  ·  last activity 7mo ago by __blackjack__‭

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 8mo ago by Moshi‭  ·  last activity 8mo 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 8mo ago by young_souvlaki‭  ·  last activity 8mo ago by samcarter‭

33%
+0 −2
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 9mo ago by roronoa_zoroDR‭  ·  last activity 8mo ago by Monica Cellio‭

Question python django-drf
75%
+4 −0
Q&A 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 10mo ago by celtschk‭  ·  last activity 10mo ago by Nick Alexeev‭

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 11mo ago by celtschk‭  ·  last activity 11mo 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 laserkittens‭  ·  closed 11mo ago by Alexei‭

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 11mo ago by boudewijn21‭

81%
+7 −0
Q&A 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 12mo ago by Dirk Herrmann‭

Question python
71%
+3 −0
Q&A 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 1y ago by celtschk‭  ·  last activity 12mo ago by luser‭

Question python hashmap
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 1y ago by sfrow‭  ·  last activity 1y ago by ibmx‭

Question python pandas
77%
+5 −0
Q&A 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 1y ago by hkotsubo‭

Question python date
81%
+7 −0
Q&A Open file in script's own folder

I have a Python script that needs to access some data (or configuration) file in its very own folder. For example, say script.py does something like this: with open('data.txt') as file: data ...

1 answer  ·  posted 1y ago by J-hen‭  ·  edited 1y ago by Alexei‭

71%
+3 −0
Q&A Docutils rst2pseudoxml.py reported error: No role entry for "meth"

docutils's rst2pseudoxml.py is reporting this error: Unknown interpreted text role "meth". even though the build command: sphinx-build -b html -d build/doctrees source build/html runs fine and pro...

1 answer  ·  posted 1y ago by CodeFarmer‭  ·  last activity 1y ago by J-hen‭

62%
+3 −1
Q&A Python looping 300 000 rows

Based on my last question comes new one. How to loop over 300 000 rows and edit each row string one by one? I have a list of 11-digit numbers stored in one single column in Excel, and I need to s...

1 answer  ·  posted 1y ago by sfrow‭  ·  last activity 1y ago by NoahTheDuke‭

87%
+12 −0
Q&A Why are list comprehensions written differently if you use `else`?

The following list comprehension worked when I tried it: [num for num in hand if num != 11] But this doesn't work: [num for num in hand if num != 11 else 22] which led me to believe that you ca...

1 answer  ·  posted 1y ago by true_blue‭  ·  last activity 1y ago by Moshi‭

66%
+4 −1
Q&A Separate digits of a number in groups with different sizes

I have a list of 11-digit numbers stored in one single column in Excel, and I need to separate the digits according to this pattern: 2-2-1-3-3. Example: 00002451018 becomes 00 00 2 451 018. How c...

2 answers  ·  posted 1y ago by sfrow‭  ·  edited 1y ago by Alexei‭

86%
+11 −0
Q&A How do I support tab completion in a python CLI program?

I spend a lot of time writing CLI tools in Python, and I would like to support tab-completion in a style similar to Git. For example, subcommands should be tab-completable, options should expand ba...

1 answer  ·  posted 1y ago by ajv‭  ·  last activity 1y ago by hkotsubo‭

88%
+14 −0
Q&A What is the purpose of `if __name__ == '__main__'`?

I often see the construct if __name__ == '__main__' in Python code. For example, the queens.py demo in the Python repository ends with these two lines: if __name__ == "__main__": main() Ho...

1 answer  ·  posted 1y ago by J-hen‭  ·  last activity 1y ago by hkotsubo‭

83%
+8 −0
Q&A Is it dangerous to use json.loads on untrusted data?

I manage a wsgi application that accepts JSON data via POST from potentially untrusted sources. Normally it is treated as a text blob and never parsed, but there is a value in the expected input th...

1 answer  ·  posted 1y ago by ajv‭  ·  last activity 1y ago by hkotsubo‭

Question python json security
42%
+1 −2
Q&A Objection CLI - SVM Conception - Freezing prompt

Now I build a Python script to execute automaticaly a dynamic analysis on apk and I block because I use this library and call objection. When I run my script, the objection command creates an inst...

0 answers  ·  posted 1y ago by Link213‭  ·  edited 1y ago by Alexei‭

25%
+0 −4
Q&A Adding support on mathematical expressions

MarkFuncs has really gotten huge progress, at least in my eyes, and now, I want to make math possible in the language. Background MarkFuncs is a programming language that I have been working on f...

0 answers  ·  posted 1y ago by General Sebast1an‭  ·  edited 1y ago by Lundin‭

81%
+7 −0
Q&A A simple game with pygame

I've just started playing around with pygame and have written a small game in it, of which I'd like a review. Note that I'm not only a complete beginner in pygame, but I also have very little exper...

1 answer  ·  posted 1y ago by celtschk‭  ·  last activity 1y ago by Peter Taylor‭

80%
+6 −0
Q&A Python Regex to parse multiple "word. word. word."

I'm trying to parse lines like "THIS. THAT..OTHER " so that "THIS. THAT." is found. There can be more than one &lt;word&gt;&lt;dot&gt; separated by a space except no space after the last one. ...

2 answers  ·  posted 2y ago by CodeFarmer‭  ·  last activity 1y ago by hkotsubo‭

Question python regex
40%
+0 −1
Q&A How to initialize variable assignment in a non-OOP interpreter?

I'm still in the process of making MarkFuncs and having given up from copy-pasting open-source interpreters, them using OOP too, I decided to make one without OOP and with scratch alongside with th...

1 answer  ·  posted 1y ago by General Sebast1an‭  ·  last activity 1y ago by Canina‭

66%
+2 −0
Q&A Unable to use pyttx3 (libespeak.so.1: cannot open shared object file: No such file or directory)

from tkinter import * from tkinter import messagebox, filedialog import pyttsx3 import PyPDF2 import os import webbrowser root = Tk() # root.geometry("500x500") root.title("Audio Book") ...

1 answer  ·  posted 1y ago by Anonymous‭  ·  last activity 1y ago by Alexei‭

70%
+5 −1
Q&A Where did my proper divisor sum program went wrong?

Here in Python, I created a program for this challenge and I'm having trouble debugging it. I already fixed most errors I have on my program but here's what I have left: x=y=z=[];i=0.0;a=int(input...

1 answer  ·  posted 2y ago by General Sebast1an‭  ·  last activity 2y ago by hkotsubo‭

Question python debugging
77%
+5 −0
Q&A Freeing sub-process resources?

I'm using Python to invoke another program in a sub-process. I've noticed my memory sometimes gets so large as to crash the system, and I'm wondering if I'm not correctly cleaning up the memory som...

0 answers  ·  posted 2y ago by Hyperlynx‭  ·  edited 2y ago by Patol75‭

33%
+1 −4
Q&A Convert .npy files in a directory to images (.png)

I have around 20,000 .npy files in a directory. That main directory has no subfolders:- Main_dir | |--1.npy |--2.npy |--3.npy |--........ The absolute file paths are stored ...

2 answers  ·  posted 2y ago by Neel_Gupta‭  ·  last activity 2y ago by hoverhell‭

Question python numpy
70%
+5 −1
Q&A How do I get the error message out of a requests exception?

I'm trying to log error messages from Requests exceptions. Example: try: make_web_request() except RequestException as ex: logging.error(ex) Example output: ERROR : ('Connection abo...

2 answers  ·  posted 2y ago by ajv‭  ·  last activity 2y ago by hoverhell‭

Question python http-request
75%
+4 −0
Q&A How to prompt a user for an expanded variable in Bash?

I work with CentOS operating system and my only shell is Bash. I want to create a script which prompts a user with a question like "What is your web application root?" The user should answer dire...

1 answer  ·  posted 2y ago by deleted user  ·  last activity 2y ago by Someone‭

44%
+2 −3
Q&A Help me understand why python3 string.format() raises Attribute error [closed]

I'd like some help understanding why the third call to print() raises AttributeError: 'NoneType' object has no attribute 'format' from os import path import inspect def myfunction(): pri...

0 answers  ·  posted 2y ago by Greg‭  ·  closed 2y ago by Alexei‭

83%
+8 −0
Q&A How to override default string formatter?

It is possible to create a new formatter class by subclassing string.Formatter class and then to use it like myformatter.format("{foo:customformat}", foo=foo). It's not super-convenient though, and...

1 answer  ·  posted 2y ago by luser‭  ·  edited 2y ago by Alexei‭

90%
+17 −0
Q&A Does the location of an import statement affect performance in Python?

When writing Python-based apps (e.g. Django, Flask, etc.), it's often the case that import statements can be found all over the place, often more than once for the same module. For example, you can...

1 answer  ·  posted 2y ago by ghost-in-the-zsh‭  ·  last activity 2y ago by ghost-in-the-zsh‭

70%
+5 −1
Q&A What do the number entries mean in the sympy poly.diff(...) tuple syntax?

I am looking to take a partial derivative of a sympy polynomial with respect to a symbol in the polynomial. In the sympy documentation for poly.diff(...) it gives sample code like this: from sympy ...

2 answers  ·  posted 2y ago by jrh‭  ·  last activity 2y ago by Derek Elkins‭

Question python sympy
83%
+8 −0
Q&A Why don't format specifiers work with lists, dictionaries and other objects?

When I want to print a number or a string, I can use f-strings (Python &gt;= 3.6) or str.format, and I can use just the variable between braces, or use format specifiers. Ex: num, text = 10, 'abc' ...

1 answer  ·  posted 2y ago by hkotsubo‭  ·  edited 2y ago by sth‭

86%
+11 −0
Q&A Different behavior with relative imports when using flask vs py

I have a small new Python 3.8 Flask project with some relative import quirks. For the DB I use SQLAlchemy with Flask-Migrate. My project has the following general structure: controllers/ static/ te...

1 answer  ·  posted 2y ago by luap42‭  ·  last activity 2y ago by ajv‭