/r/pythontips
A place to get a quick fix of python tips and tricks to make you a better Pythonista.
Enter a python tip or trick.
Other related Python subs:
/r/pythontips
The tool allows you to view the line that is being executed at every step in a Python program.
It can help you understand the basic Python concepts like loops, functions, generators. e.t.c
from threading import Thread,Lock,Condition
from time import sleep
from random import random,randrange
'''
Soluzione commentata esercizio sul gioco delle sedie.
In questo sorgente potete sperimentare con tre possibili soluzioni: soluzione A senza lock (sbagliata), soluzione B con i lock ma usati male (sbagliata), soluzione C con i lock usati bene (corretta)
Soluzione A:
- Fatta creando un array di PostoUnsafe e usando come thread PartecipanteUnsafe
In questa soluzione non viene usata alcuna forma di locking. Facendo girare il gioco più volte, riscontrerete che a volte tutti i Partecipanti riescono a sedersi,
poichè qualcuno si siede sulla stessa sedia
Soluzione B:
- Fatta creando un array di PostoQuasiSafe e usando come thread PartecipanteUnSafe
Questa soluzione ha lo stesso problema della soluzione A.
Anche se libero() e set() sono, prese singolarmente, thread-safe, queste vengono chiamate in due tempi distinti (PRIMO TEMPO: chiamata a libero; SECONDO TEMPO: chiamata a set() ), acquisendo e rilasciando il lock entrambe le volte.
In mezzo ai due tempi, eventuali altri partecipanti avranno la possibilità di acquisire il lock su self.posti[i] e modificarne lo stato. Noi non vogliamo questo. E' una race condition.
Soluzione C:
- Fatta usando un array di PostoSafe e usando come thread PartecipanteSafe
'''
class PostoUnsafe:
def __init__(self):
self.occupato = False
def libero(self):
return not self.occupato
def set(self,v):
self.occupato = v
class PostoQuasiSafe(PostoUnsafe):
def __init__(self):
super().__init__()
self.lock = Lock()
def libero(self):
'''
Il blocco "with self.lock" è equivalente a circondare tutte le istruzioni in esso contenute con self.lock.acquire() e self.lock.release()
Notate che questo costrutto è molto comodo in presenza di return, poichè self.lock.release() verrà eseguita DOPO la return, cosa che normalmente
non sarebbe possibile (return normalmente è l'ultima istruzione di una funzione)
'''
with self.lock:
return super().libero()
def set(self,v):
self.lock.acquire()
super().set(v)
self.lock.release()
class PostoSafe(PostoQuasiSafe):
def __init__(self):
super().__init__()
def testaEoccupa(self):
with self.lock:
if (self.occupato):
return False
else:
self.occupato = True
return True
def reset(self):
self.occupato = False
class Display(Thread):
def __init__(self,posti):
super().__init__()
self.posti = posti
def run(self):
while(True):
sleep(1)
for i in range(0,len(self.posti)):
if self.posti[i].libero():
print("-", end='', flush=True)
else:
print("o", end='', flush=True)
print('')
class PartecipanteUnsafe(Thread):
def __init__(self,posti):
super().__init__()
self.posti = posti
def run(self):
sleep(randrange(5))
for i in range(0,len(self.posti)):
#
# Errore. Anche se libero() e set() sono, prese singolarmente, thread-safe, queste vengono chiamate in due tempi distinti (PRIMO TEMPO: chiamata a libero; SECONDO TEMPO: chiamata a set() ), acquisendo e rilasciando il lock entrambe le volte.
# In mezzo ai due tempi, eventuali altri partecipanti avranno la possibilità di acquisire il lock di self.posti[i] e modificarne lo stato. Noi non vogliamo questo. E' una race condition.
#
if self.posti[i].libero():
self.posti[i].set(True)
print( "Sono il Thread %s. Occupo il posto %d" % ( self.getName(), i ) )
return
print( "Sono il Thread %s. HO PERSO" % self.getName() )
class PartecipanteSafe(Thread):
def __init__(self, campionato):
super().__init__()
self.campionato = campionato
def run(self):
while True:
sleep(randrange(5))
for i in range(0,len(self.campionato.posti)):
#print(f"SONO ENTRATO NEL FOR {i} e questo è il {len(self.campionato.posti)}")
if self.campionato.posti[i].testaEoccupa():
self.campionato.vincitori.append(self.getName())
print(f"Sono il Thread {self.getName()}. Occupo il posto {i}")
return
self.campionato.perdente = self.getName()
print(f"Sono il Thread {self.getName()}. HO PERSO")
self.notifyPerdente()
def notifyPerdente(self):
with self.campionato.lock:
self.campionato.condition.notifyAll()
class Campionato:
def __init__(self, nposti):
self.nposti = nposti
self.posti = [PostoSafe() for i in range(0, nposti)]
self.partecipanti = [PartecipanteSafe(self) for i in range(0,nposti+1)]
self.vincitori = []
self.perdente = ''
self.lock = Lock()
self.condition = Condition(self.lock)
def avvia_campionato(self):
with self.lock:
lg = Display(self.posti)
lg.start()
for elemento in self.partecipanti:
elemento.start()
for i in range(5): #5 round
print(f"{i+1} round:")
self.condition.wait()
self.partecipanti = self.vincitori
self.vincitori = []
self.perdente = ''
self.posti.pop(0)
for j in range(0, len(self.posti)):
self.posti[j].reset()
NSEDIE = 5
#
# Qui si può sperimentare con i vari tipi di posti e verificare se si verificano delle race condition
#
#
# Soluzione A
#posti = [PostoUnsafe() for i in range(0,NSEDIE)]
# Soluzione B
#posti = [PostoQuasiSafe() for i in range(0,NSEDIE)]
# Soluzione C
## posti = [PostoSafe() for i in range(0,NSEDIE)]
## partecipanti = [PartecipanteSafe(posti) for i in range(0,NSEDIE+1)]
## lg = Display(posti)
## lg.start()
#
# I partecipantiSafe accedono ai posti senza race condition. I PartecipantiUnsafe NO.
#
# Per le soluzioni A e B usare PartecipanteUnsafe
# Per la soluzione C usare PartecipanteSafe
#
#
c = Campionato(NSEDIE)
c.avvia_campionato()
##for elemento in partecipanti:
## elemento.start()
# for t in range(0,NSEDIE+1):
# #t = PartecipanteUnsafe(posti)
# t = PartecipanteSafe(posti)
# t.start()
Here, we will explore two scenarios: Nth highest salary in the whole dataset and Nth highest salary in a specific group like department, country, etc. Here, Nth means, any positive integer like 2nd highest salary, 3rd highest salary, 4th highest salary, etc.
I have already prepared small CSV datasets along with some records. Throughout this article, we will find the 3rd and 2nd highest salaried employees in complete data and each department.
- Without Considering Department
Find 3rd Highest Salary in the Whole Data
import pandas as pd
df = pd.read_csv('../../pyspark_tutorials/sample_data.csv')
# Getting nth highest salaried employee in whole dataset
n = 3
nth_highest_salary = df.nlargest(3, columns='salary', keep="first").reset_index().loc[[2]]
print(nth_highest_salary)
With Considering Department
import pandas as pd
df = pd.read_csv('../../pyspark_tutorials/sample_data.csv')
# Getting nth highest salaried employee in specific department
n = 2
df.sort_values(by=['salary'], ascending=False, inplace=True)
nth_highest_salary = df.groupby("department").nth(1)
print(nth_highest_salary)
This is how you can find the Nth highest salary using Pandas in a specific department.
Thanks
Hello, I'm a beginner in python and I'm looking for some good course & resource for DSA in python. Any recommendations?
As the title says, I'm working on a travel website that draws data using apis. We've gotten to a point where we're getting all the api info we need but it comes in the form of blank html. How can we style these results like with css?
I am trying to use pymediainfo which has a dependency libmediainfo.so.0 file im >=3.8 runtime configuration. And I am ending up in the following error:
Exception::: libmediainfo.so.0: cannot open shared object file: No such file or directory.
It seems we get this error on when a mandatory dependency is missing for libmediainfo to load. I tried to download zenLib as well. But nothing works!
Anyone able to use the combination of pymediainfo on a 3.9 python runtime environment im aws lambda?
So I have generated a Python 3 code that does a specific computing job. Let's say it requires 1 CPU and 32gb ram to run. I've created a Dash App as a front end, where someone can load in there data, basically press 'go', and then export the processed result. It is very niche service for a specific industry, that might not be used that often, but I'm interested in making it more public via web hosting it to support this niche industry. Either free, adding in a small ad, or say, a donation button.
As you can see, I only need the compute to function when the software is being used. I have deployed a dash app previously in Render. But if I were to grab an instance with 32gb ram it would need to be the Pro Ultra Plan which is $450 per month. The cost of this is way too much for what I need, and I don't need 8 CPU's realistically as there is no parallel computing. Just the CPU, 32gb ram for instance, and some temporary storage. I'm happy with cold starts if it does reduce costs, and can just provide a notification to users.
What are my options here to deploy my processing algorithm with a user friendly Dash App front end publicly? I'm on the low end of advanced in python, but very novice in deploying them in a user friendly way.
I have created a simple password generator as part of my projects while I learn, This generator creates the password and also shows you the strength of the password..
Any one who is a debutant on python like me hit me let’s study together
Quiero aprender programación, pero no tengo pensando ingresar a ningún centro de educación por ahora. ¿Que me recomiendan para empezar?
If you have a Base64 string and you want to turn it back into an image, Python’s base64
library makes this just as easy.
Steps to Create base64 to image Converter in Python
Step 1: Import the Required Library
we will use the built-in base64
library, so make sure to import it:
import base64
Step 2: Get the Base64 String
You need a Base64 string that you want to convert back into an image. This could be one that you’ve stored or received from an API. Here’s a shortened example:
base64_string = "iVBORw0KGgoAAAANSUhEUgAAABAAAAA..."
Step 3: Decode the Base64 String
Once you have the Base64 string, use the base64.b64decode()
function to convert it back into binary data.
Step 4: Write the Binary Data to an Image File
Now that you have the binary data, the final step is to save it as an image file. Use the open()
function in "write binary" mode ('wb'
).
with open("output_image.png", "wb") as image_file:
image_file.write(image_data)
Full Code Example for Converting Base64 to an Image
Here’s the complete Python code that converts a Base64 string back into an image:
import base64 # Step 1: Import the base64 library
# Step 2: Example Base64 string
base64_string = "iVBORw0KGgoAAAANSUhEUgAAABAAAAA..."
# Step 3: Decode the Base64 string back into binary data
image_data = base64.b64decode(base64_string)
# Step 4: Write the binary data to an image file
with open("output_image.png", "wb") as image_file:
image_file.write(image_data)
Explanation:
base64.b64decode()
: Decodes the Base64 string back into binary data.open("output_image.png", "wb")
: Opens a new file in write-binary mode.image_file.write()
: Writes the binary data into the file, creating the image.https://youtu.be/Yu8z0Lg53zk?si=lGIC0TGvMG3fnyUm This tutorial explains 3 python packages to add text and image watermark in images using single line code
error:PS C:\Users\kauan\OneDrive\estudo python> & C:/Users/kauan/AppData/Local/Microsoft/WindowsApps/python3.11.exe "c:/Users/kauan/OneDrive/estudo python/welcome2"
code:
import os
import shutil
def criar_diretorios(diretorios):
for diretorio in diretorios:
if not os.path.exists(diretorio):
try:
os.makedirs(diretorio)
print(f"diretório {diretorio} criado.")
except PermissionError:
print(f"sem permissão para criar o diretório {diretorio}.")
except Exception as e:
print(f"erro inesperado ao criar {diretorio}: {e}")
def mover_arquivos(diretorio_origem):
for arquivo in os.listdir(diretorio_origem):
caminho_arquivo = os.path.join(diretorio_origem, arquivo)
if os.path.isfile(caminho_arquivo):
extensao = arquivo.split('.')[-1]. lower()
if extensao in ['pdf', 'txt' 'jpg']:
diretorio_destino = os.path.join(diretorio_origem, extensao)
try:
shutil.move(caminho_arquivo, diretorio_destino)
print(f"{arquivo} movido para {diretorio_destino}.")
except PermissionError:
print(f"sem permissão para mover {arquivo}.")
except Exception as e:
print(f"erro inesperado ao mover {arquivo}: {e}")
else:
print(f"extensão {extensao} de {arquivo} não é suportada.")
def main():
diretorio_trabalho = "diretorio_trabalho"
diretorios = [os.path.join(diretorio_trabalho, 'pdf'),
os.path.join(diretorio_trabalho, 'txt'),
os.path.join(diretorio_trabalho, 'jpg')]
criar_diretorios(diretorios)
mover_arquivos(diretorio_trabalho)
if __name__ == "__main__":
main()
I’m relatively new to python, but pretty good with physics and math. at least good enough for the simulations i want to make. does anyone have advice on the libraries i should use. Panda 3d seems like a good pick but I figured i should get more views from more experienced people.
Is JIT (Just-In-Time) compilation really useful in Python? 🤔 While other languages rely on JIT for speed, CPython doesn’t! Why JIT is considered "useless" in Python and what Python does to boost performance instead.
Video : JIT compiler is useless… but Python has something else
PyGenTree is a Python package that generates ASCII tree representations of directory structures. It's a simple command-line tool that allows you to visualize the structure of your project or any directory on your system. With PyGenTree, you can easily document your project's structure, quickly understand unfamiliar codebases, or generate directory trees for README files.
🔗 Check it out on GitHub: https://github.com/taeefnajib/pygentree
If you like this project, please ⭐ it. It would encourage me to make better tools in the future.
PyGenTree is designed for developers, programmers, and anyone who works with directory structures on a regular basis. It's a useful tool for:
There are existing tools that generate directory trees, such as tree
on Linux and dir
on Windows. There are online ASCII Tree Generators where you have to manually add files and directories. There are some python packages similar to this, but I tried to combine all the useful features from these alternatives and create this one. PyGenTree differs from these alternatives in several ways:
pip install pygentree
Here's a quick example of what you can do:
# Basic usage (current directory)
pygentree
# Specify a directory and limit depth
pygentree /path/to/directory -l 2
# Sort files and folders, ignore hidden, exclude specific directories
pygentree -s asc --ignore-hidden -e "node_modules,venv,dist"
PyGenTree is perfect for anyone who wants a simple and powerful tool for generating ASCII directory trees. Feel free to try it out and let me know what you think!
🔗 Check it out on GitHub: https://github.com/taeefnajib/pygentree If you like this project, please ⭐ it. It would encourage me to make better tools in the future.
Hi everyone! I’m a beginner Python developer based in Saudi Arabia, and I’m looking for an opportunity to get a remote internship or job in programming. I live in a different country from most companies. Is it possible to find remote opportunities in programming? Any tips or resources that could help? Thanks in advance for your help! *note: I don’t have CS degree
I recently explored Manim, an open-sourced python package for generating animated videos for explaining maths. It includes animations for shapes, equations, codes, graphs, etc. The repo is trending on GitHub as well. The demo also looks very impressive. Check it out here : https://youtu.be/QciJxVjF4M4?si=Bk_gU4Tj5f6gPpiq
I have some XP as a frontend dev. Im pretty decent with javascript and react and though im not a master by any means im somewhat comfortable in these languages. I think knowing python would help me interact with BE devs more and also allow me to understand how backend db’s work instead of it being a mysterious endpoint/api call.
For those not aware yet, OpenAI did not create their voice mode. A company called LiveKit created the voice agent framework that allows for OpenAI's models to plug and play as a realtime, interruptible voice assistant.
LiveKit didn't just create this code for OpenAI, they have completely open-sourced their entire voice agent framework and made it surprisingly simple for us to download and run the code on our own computers.
This allows people who know a little bit of Python code, to start off with a voice assistant that is a clone of ChatGPT Voice mode, and start customizing it to be far more powerful than what ChatGPT can give to billions of users profitably.
In my latest YouTube video, I show you how to get a LiveKit voice agent installed and running on your own PC:
https://youtu.be/_8VHac0M7d8?si=fotsxgvMrw-ZgxAT
I have free time from highschool and want to get into coding, and tips on diving in and learning.
Floyd’s triangle is a right-angle triangle where the numbers are printed in a continuous sequence.
Source Code:
n = 5
num = 1
for i in range(1, n + 1):
for j in range(1, i + 1):
print(num, end=" ")
num += 1
print()
Output:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Credit: allinpython
I have already done the basics of python, including variables, sets, lists, and tuples. Now I am looking for a preferably free python course (paid is fine) which is more advanced which has like recursion and data structures (Linked Lists, Queues, Stacks, etc). Please help me out
I have a code like this in a file called function.py:
class_A = classA()
sample = class_A.receive_file() #contains an API Call
def function():
x = sample + 'y'
y = sample + 'z'
print(x)
print(y)
Pretty simple code but in my test I want to test this with pytest as follows:
import pytest
from unittest import mock
from function import function
class FunctionTest(unittest.TestCase):
@mock.patch("function.classA")
def setUp(self, mockA):
self._mockA = mockA.return_value
self._mockA.return_value = MagicMock()
The issue is that when I import function in my test it causes the API call to go out immediately and I get an error. Putting the import statement inside my setUp says 'function not found' since my __init__.py file is empty. What am I doing wrong in this case? I figure it really shouldnt be this hard to do something like this
Hello, I'm working on a project where I need to compare SQL queries to determine if both queries actually resolve the same problem/exercise. Essentially, I want to check if they return the same result set for any given input, even if they use different syntax or structures (e.g., different JOIN orders, subqueries vs. CTEs, etc.).
I know that things like execution plans might differ, but the result set should ideally be the same if both are solving the same problem. Does anyone know of a reliable algorithm or approach for doing this? Maybe some clever SQL transformation, normalization technique, or even a library/tool that can help?
The main objective is to build a platform where the system has a stored solution. And the user should insert theirs and the system should compare both and determine if the entered query is a possible and valid response.
Thanks in advance for any suggestions! 🙏
Olá, tudo bem?
Pessoal, eu preciso de uma ajuda.
Estou tentando implementar duas bibliotecas do python: Flet Pyrebase4.
Quando estou construindo uma aplicação apk com flet, acaba ficando travado na extração dos arquivos.
Poderiam me ajudar?
Creating asset directory: C:\Users\rafae\AppData\Local\Temp\flet_flutter_build_5uElf1KtMh\app
Copying Python app from C:\Users\rafae\Documents\Python_Projects\mobile\Animes Online to C:\Users\rafae\AppData\Local\Temp\serious_python_temp3ca83e7e
(● ) Packaging Python app ⏳... Configured mobile platform with sitecustomize.py at C:\Users\rafae\AppData\Local\Temp\serious_python_sitecustomize956fd7e7\sitecustomize.py
Installing dependencies [flet-embed==0.24.1, Pyrebase4==4.8.0, pycryptodome==3.21.0, gcloud==0.18.3, googleapis-common-protos==1.62.0, protobuf==4.25.2, httplib2==0.22.0, pyparsing==3.1.1, oauth2client==4.1.3, pyasn1==0.5.1, pyasn1-modules==0.3.0, rsa==4.9, python-jwt==4.1.0, jws==0.1.3, requests==2.32.3, certifi, chardet==3.0.4, idna==2.10, urllib3==1.26.20, requests-toolbelt==0.10.1, jwcrypto==1.5.6, cryptography==43.0.0, deprecated==1.2.14, wrapt==1.16.0] with pip command to C:\Users\rafae\AppData\Local\Temp\serious_python_temp3ca83e7e\__pypackages__
Extracting Python distributive from C:\Users\rafae\AppData\Local\Temp\cpython-3.11.6+20231002-x86_64-pc-windows-msvc-shared-install_only.tar.gz to C:\Users\rafae\AppData\Local\Temp\hostpython3.11_d93f38dc
( ●) Packaging Python app ⏳...
Só fica assim, já tentei deixar só a biblioteca do flet e pyrebase e mesmo assim não vai.
Python exam that consists of problem-solving questions that satisfy specific outputs. I was wondering if there are any VS Code extensions that could potentially give me an edge. I'm looking for extensions that might help with debugging, visualization, catching common mistakes easily, or anything that gives a ridiculous advantage. Has to be offline.
Can anyone suggest some great Python project ideas that would be useful?
As a student, I successfully created a Voice Recognition system with Manual Response Integration. Now, I want to add the OpenAI API to it without incurring any costs.
guys i am learning python and i am trying to install " pip install pyttsx3" and i did install after hours now i am trying to run a code using this module but it is not working and keep giving me error it s been two days since i am stuck on this can of you help me in this please i cant paste because this subreddit isnt letting my ulload them anyody please help i can give you access to my pc through "anydesk", google meet or whatever just please help me