/r/Python
If you have questions or are new to Python use r/LearnPython
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
Online Resources
Five life jackets to throw to the new coder (things to do after getting a handle on python)
PyMotW: Python Module of the Week
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
/r/Python
test.py
import sys
sys.setrecursionlimit(2)
class Foo:
class Foo:
foo = 1
python test.py
Please be honest, write your guess in comments about the output of this code before opening the spoiler =)
The answer: >!RecursionError: maximum recursion depth exceeded!<
Did you know?)
Today Cloudflare has announced support for Python in their Clouldflare Workers serverless environment. This allows Python to be run at the edge on their global network using a combination of Pyodine and the V8 runtime.
Announcement can be found on the Cloudflare Blog: https://blog.cloudflare.com/python-workers
I recently posted my opinions on the import system in Python and how it can be made more intuitive and straightforward.
I got some comments, some of them are just snappy as usual and some of them had some suggestions and workarounds.
Either way the post got removed, citing that it is not suitable for this sub-reddit. I mean, come on, I cannot talk about Python features in Python sub-reddit?
Security Code Challenge for Developers & Ethical Hackers – Damn Vulnerable RESTaurant
A FastAPI based intentionally vulnerable web application teaching the most common security vulnerabilities in API through a dedicated game.
Damn Vulnerable RESTaurant is operated by a mysterious Chef who has discovered that threat actors were able to compromise his restaurant’s API and the underlying system. He suspects that a competing restaurant located across the street might be involved in this attack! The goal of the challenge is to identify and fix vulnerabilities based on provided hints. During this adventure, you will have the opportunity to investigate how the attack was carried out and fix security issues to safeguard the application. Moreover, you can uncover the identity of the person behind the attack by the end of this adventure.
Ever wished your Python interpreter had the dramatic feeling of a 300 baud modem connection?
Today there's a solution: pip install dramatic
All text output by Python will print character-by-character.
It works as a context manager, as a decorator, or as a simple function call.
Other features include a dramatic REPL, ability to run specific Python modules/scripts dramatically, and a --max-drama argument to make all Python programs dramatic all the time.
Those seeking amusement.
Just like Python usually runs, but with the feeling that you're inside a text-based adventure game.
Source code: https://github.com/ben-n93/geojson-shave
What my Project Does
Reduces the file size of GeoJSON files by truncating coordinates to the specified decimal place, eliminating whitespace and (optionally) replacing the property key with an empty dictionary.
Target Audience
For anyone that works with geospatial data (and specifically GeoJSON files) - so data analysts, data engineers, data journalists, GIS professionals, etc.
Comparison
There is a website that reduces the size of GeoJSON files, however it didn't work for me when I tested it. The advantage of my tool is that you can run it from the command-line and don't need an Internet connection.
Dive deep into Python with our Advanced Questions thread! This space is reserved for questions about more advanced Python topics, frameworks, and best practices.
Let's deepen our Python knowledge together. Happy coding! 🌟
https://github.com/amalshaji/portr
Portr is a tunnelling solution that let's you expose local http/tcp connections to the public internet.
Developers that work with webhooks or callbacks. Or anyone who want to quickly share their dev server with others.
The admin backend is built using Python. I would appreciate a feedback.
As of PEP 4124 being accepted, the infamous pointers.py will be added to Python's standard library in 3.13! To quote Guido van Rossum's take on adding this, "Why the hell not?"
This will also introduce pointer literals, the sizeof operator, and memory errors!
from pointers import malloc
ptr = &"spam" # Pointer literal
print(*ptr)
mem = malloc(?"hello") # New sizeof operator
print(*mem) # MemoryError: junk 13118820 6422376 4200155 at 0x7649f65a9670
# MemoryWarning: leak at 0x7649f65a9670
However, it was decided in this discussion that segfaults would be added to the language for "extra flavor":
spam = *None
# Segmentation fault, core dumped. Good luck, kiddo.
What's the first step to building 90% of AI applications?
Chunking your documents! Unfortunately there's very few libraries for this and most rely on naively splitting text into N characters. You lose so much valuable information hidden in the layout of the document when you do this. If you give an LLM bad inputs, you're going to get bad results.
https://github.com/Filimoa/open-parse/
What it does:
The library can detect the layout of a PDF, convert to markdown and extract tables into html in one line of code. You can then feed this directly into your vector database or something like LlamaIndex. It does all this while preserving bounding boxes so you can display citations directly to the user.
Target Audience:
Anyone building a RAG AI app. I run a talk-to-your-file website that's seen millions of documents and this library represents a giant chunk of what I know about feeding documents to LLM's.
Comparison:
Open source libraries like LlamaIndex chunk your documents into strings of fixed length. There's a couple commercial solutions that do this but they're expensive (typically 100 pages / dollar) and honestly have less than ideal results (AWS Texttract, Adobe, Google).
Facebook has nougat for converting files to markdown but it's incredibly slow on anything without a massive GPU.
This is a project that I initially created in a weekend for my own use after I was surprised to find out there was no easy way to generate Draw.io diagrams in Python. I'm a huge fan of Draw.io for documentation since it's free, lightweight, and the files are plaintext.
After building basic initial functionality I decided to use this as a project to make a more mature Python library out of something I initially wrote for myself quick n dirty. I refactored (a lot), wrote docs, tests, build logic, etc. There's still a lot to do to make it as robust as it could be but I think it's good enough to share!
There's some basic functionality that allows you to manually create Draw.io objects and edges, pre-format them from libraries, and lay them out. I've also started to implement more automated diagram types that extend that basic functionality for specific use cases. The ony currently released diagram type is a TreeDiagram, that allows you to define a tree structure with parent and children nodes, then auto generate a nice looking layout.
For example, this code for a tree diagram generates this diagram! Or for something a little more manual, this code for a flowchart generates this diagram.
I use Draw.io for everything so your imagination is the limit. But I think this would be the best fit for auto generating documentation, specifically that which needs to be read by non-technical or non-programmers.
I couldn't find any other Python libraries for creating Draw.io graphs. There are other graphing options, most based on Graphviz. The advantage of Draw.io here is the ability to have finer control over formatting and have a great desktop UI for working with the diagrams.
edit:
Drawpyo is now on PIP! Thanks for the push everyone.
Discover the power of Modin in overcoming the performance bottlenecks of Pandas. We'll walk you through how a simple switch can reduce your waiting times and make your workflow smooth and speedy, all without leaving the comfort of Pandas behind.
Modin is an active open source project. Take a look to this blog post to see if it is applicable for your code: https://dchigarev.github.io/modin_perf_examples/
Hi everybody,
I'm happy to share AnyPathLib 🛣️, a pip package I created to simplify access to different storage resources - S3, Azure, and local storage.
What My Project Does:
Basically, instead of writing custom code to handle the different storage resources using the SDK (boto3, azure-sdk), you can now just use AnyPathLib:
from anypathlib import AnyPath
# Create an AnyPath instance for a local file
local_file = AnyPath("/path/to/local/file.txt")
# Create an AnyPath instance for an S3 object
s3_file = AnyPath("s3://bucket/path/to/object.txt")
# Copy a file from local to S3
local_file.copy(s3_file)
# Copy a directory from S3 to Azure
s3_dir = AnyPath("s3://bucket/path/to/dir")
azure_dir = AnyPath("https://account_name.blob.core.windows.net/container_name/path")
s3_dir.copy(azure_dir)
There are other cool features to save you some time, so check out this X thread for some more details or visit the Github project page.
Target Audience:
Anybody which works with both a local environment and Azure or AWS, and could use a simplified API to access their storage
Comparison:
I didn't find any similar library (that's why I wrote this one) - there are dedicated packages for S3 (boto3) and the Azure SDK, but they come with different, and somewhat unintuitive APIs.
I'd love to hear your feedback, issues, and of course - PRs :)
A dense Python cheat sheet with just what you need.
Design principles:
• Focus on Python core
• Comprehensive but selective (Just what you need)
• Densely packed
• Well-linked
• Linkable
• Responsive
• Printable
Issues and feedback are tracked at the best-python-cheat-sheet repository.
*It may not be the best Python cheat sheet, but it aspires to be.
Welcome to our weekly Project Ideas thread! Whether you're a newbie looking for a first project or an expert seeking a new challenge, this is the place for you.
Difficulty: Intermediate
Tech Stack: Python, NLP, Flask/FastAPI/Litestar
Description: Create a chatbot that can answer FAQs for a website.
Resources: Building a Chatbot with Python
Difficulty: Beginner
Tech Stack: HTML, CSS, JavaScript, API
Description: Build a dashboard that displays real-time weather information using a weather API.
Resources: Weather API Tutorial
Difficulty: Beginner
Tech Stack: Python, File I/O
Description: Create a script that organizes files in a directory into sub-folders based on file type.
Resources: Automate the Boring Stuff: Organizing Files
Let's help each other grow. Happy coding! 🌟
It's a program that searches through posts in a specified subreddit and downloads images, gifs etc.
It's a toy project at this point, but I might extend its functionality if I can find good resources. Maybe even add support for different websites (imgur et al.).
I found a few websites for downloading images from reddit but they claimed they didn't work because of the Reddit API changes, I was sceptical and wrote my own script.
Anyhow,
For the last few days I was trying to find good wallpapers for my system. I wrote a Python script for getting images and gifs from subreddits, here is the project repo. Please read the instructions before using it.
It's pretty basic but gets the job done. I am still working on the project.
I also wrote a flake.nix for my NixOS bros. Didn't test the project on Windows but I don't see why it wouldn't work.
Lastly, here is my NixOS config in case you are interested. Enjoy
I've created a simple API service https://github.com/koldakov/futuramaapi to access Futurama units with async Python + FastAPI + SQLAlchemy (PostgreSQL).
I've chosen Hypercorn for a web server to achieve HTTP/2 support - considering I didn't find yet cheap hosting that supports HTTP/2. Heroku doesn't support, render doesn't support.
Also it has some code related to GraphQL (I used strawberry).
+ I didn't find a lot of discussions about Heroku + FastAPI - the problem will be with redirect to https. As default fastapi (starlette) middleware doesn't work with proxies there will be an infinite loop if you try to use fastapi.middleware.httpsredirect.HTTPSRedirectMiddleware
. Chain will be: Client https-> Proxy(Hypercorn, for example) http-> fastapi, in that case as you can see the request is secure, but fastapi (starlette) won't understand this and will redirect to https. Here is a simple workaround to support https under proxy: https://github.com/koldakov/futuramaapi/blob/main/app/middlewares/secure.py
Constructive feedback is greatly appreciated.
Source code: https://github.com/koldakov/futuramaapi
Working example: https://futuramaapi.com
I have been reading a lot about these tools recently and wondered if they are used in production, especially when building a backend with Python. As far as I know, PyPy is not ideal because of its lack of package support, but Cython seems pretty neat.
Hello /r/Python! It's time to share what you've been working on! Whether it's a work-in-progress, a completed masterpiece, or just a rough idea, let us know what you're up to!
Let's build and grow together! Share your journey and learn from others. Happy coding! 🌟
I created a python library "dis-bg-remover" based off the "Highly Accurate Dichotomous Image Segmentation (https://arxiv.org/pdf/2203.03041.pdf), whose results are comparable, if not better, to the premium offerings in the market.
Explainer video here
Stumbled upon a useful Python resource? Or are you looking for a guide on a specific topic? Welcome to the Resource Request and Sharing thread!
Share the knowledge, enrich the community. Happy learning! 🌟
Hi there
I was tasked with migrating from mongoDb to PostgreSQL due to the performance issues.
The backend is FastAPI-based web server for a mobile APP with a decent amount if CRUD. We also heavily use Pydantic for data validation and the primary carried of data across the service layers
Would like to hear opinions from SQLModel users as to how easy it is to use or what kind of pitfalls am i setting myself up by using it. Last post that I could find was years old and even that was not really conclusive. Now SQLAlchemy 2.0 is released and even SQLModel has support for Pydantic V2, so would like some newer thoughts in this topic.
Our datamodel includes quite a bit of top level nestings, which I would prefer to be stored as `JSON` columns (Similar SO Question). SQLModel doesn't seem to have any documentation for such cases.
If possible I would prefer avoiding to write and maintain our own internal library with SQLAlchemy ORM to perform these conversions. But maybe just using plain SQLAlchemy and Pydantic might be a good solution too.
Seeking inputs from more experienced users. Thank you.
Welcome to Free Talk Friday on /r/Python! This is the place to discuss the r/Python community (meta discussions), Python news, projects, or anything else Python-related!
Let's keep the conversation going. Happy discussing! 🌟
Hey all,
I love what Django has with django-fsm. I require something but without Django, as there is no user interaction with the workflow. All inputs and outputs are either rabbitmq or another api. This is to be a workflow management system.
I am looking for state management backed up by database so we can not suffer if a k8s pod dies with all the states in memory. Some of our workflows could take weeks.
Is it still best to make this in Django or is there a database backed state management module available?
I see pytransitions but I would have to add database logging to it.
I just published a tutorial series on how to automate a Python script in Google Cloud using Cloud Functions and/or Cloud Run. Feedback would be great. Thanks!
I was trying to tackle the problem of queue fairness and here's how I solved it.
Hello everyone!
I am proud to introduce a Makefile Parser for Python that I think will be useful!
the link is Thanatisia/makefile-parser-python
As an introduction, I have been using python and been following this subreddit for quite awhile now, but this is my first post
Recently, I've been planning a side-project involving the use of Makefiles in Python, and I required the use of a Makefile parser ala json or pyyaml - whereby you would import a Makefile into python as objects/dictionary/lists. Hence, I started searching for a Makefile Parser.
The only parser I've found is PyMake(2) which is cool but it hasnt been updated since 2017 from what I recall and that it is more of a CLI utility, so with that in mind, I went about to make it
I hope that this will be as useful as it is for me, currently I am using this in a side project and i'm able to format it such that its printing out a typical Makefile structure right off the bat, which is pretty nice.
Additional information to the project
This is a Makefile Parser, made in Python. The operational workflow is basically
Start --> Import File into dictionary --> Manipulation and Usage --> Processing --> Output --> Export
This is a library/package/module by design, much like json or pyyaml as mentioned but a smaller scale at the moment as its just started.
I'm not sure if it applies for you but its a parser/importer
I'm not sure if there are any other Makefile Parsers other than Pymake2, but the idea is similar to the aforementioned ideas
Welcome to this week's discussion on Python in the professional world! This is your spot to talk about job hunting, career growth, and educational resources in Python. Please note, this thread is not for recruitment.
Let's help each other grow in our careers and education. Happy discussing! 🌟
I recently ran into some challenges while trying to work with Apple's .numbers files on Linux. After a bit of experimentation, I figured out a workflow that simplifies the process. If you're planning to use .numbers files and need to load them into pandas, I've created a tutorial that covers the required dependencies and the steps to follow: https://nezhar.com/blog/load-apple-numbers-files-python-pandas-using-containers/.
Has anyone else here worked with .numbers files in Python? I’d love to hear about your experiences or any tips you might have.