/r/Python

Photograph via snooOG

The official Python community for Reddit! Stay up to date with the latest news, packages, and meta information relating to the Python programming language.

If you have questions or are new to Python use r/LearnPython

News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python

Current Events

Upcoming Events

Full Events Calendar

Please read the rules

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

Online exercices

programming challenges

Asking Questions

Try Python in your browser

Docs

Libraries

Related subreddits

Python jobs

Newsletters

Screencasts

/r/Python

1,221,934 Subscribers

0

Python Inner class feature

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?)

3 Comments
2024/04/02
21:09 UTC

0

Serverless Python Support in Cloudflare Workers

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

0 Comments
2024/04/02
20:58 UTC

0

Genuine question: Where should I share my opinion about Python language?

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?

21 Comments
2024/04/02
19:14 UTC

16

Security Code Challenge for Developers & Ethical Hackers - Damn Vulnerable RESTaurant API

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.

0 Comments
2024/04/02
15:59 UTC

185

Python isn't dramatic enough

Ever wished your Python interpreter had the dramatic feeling of a 300 baud modem connection?

Today there's a solution: pip install dramatic

dramatic on PyPI

dramatic on GitHub

What My Project Does

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.

Target Audience

Those seeking amusement.

Comparison

Just like Python usually runs, but with the feeling that you're inside a text-based adventure game.

20 Comments
2024/04/01
18:12 UTC

15

geojson-shave: a command-line tool for reducing the file size of GeoJSON files

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.

5 Comments
2024/04/02
02:36 UTC

3

Tuesday Daily Thread: Advanced questions

Weekly Wednesday Thread: Advanced Questions 🐍

Dive deep into Python with our Advanced Questions thread! This space is reserved for questions about more advanced Python topics, frameworks, and best practices.

How it Works:

  1. Ask Away: Post your advanced Python questions here.
  2. Expert Insights: Get answers from experienced developers.
  3. Resource Pool: Share or discover tutorials, articles, and tips.

Guidelines:

  • This thread is for advanced questions only. Beginner questions are welcome in our Daily Beginner Thread every Thursday.
  • Questions that are not advanced may be removed and redirected to the appropriate thread.

Recommended Resources:

  • If you don't receive a response, consider exploring r/LearnPython or join the Python Discord Server for quicker assistance.

Example Questions:

  1. How can you implement a custom memory allocator in Python?
  2. What are the best practices for optimizing Cython code for heavy numerical computations?
  3. How do you set up a multi-threaded architecture using Python's Global Interpreter Lock (GIL)?
  4. Can you explain the intricacies of metaclasses and how they influence object-oriented design in Python?
  5. How would you go about implementing a distributed task queue using Celery and RabbitMQ?
  6. What are some advanced use-cases for Python's decorators?
  7. How can you achieve real-time data streaming in Python with WebSockets?
  8. What are the performance implications of using native Python data structures vs NumPy arrays for large-scale data?
  9. Best practices for securing a Flask (or similar) REST API with OAuth 2.0?
  10. What are the best practices for using Python in a microservices architecture? (..and more generally, should I even use microservices?)

Let's deepen our Python knowledge together. Happy coding! 🌟

0 Comments
2024/04/02
00:00 UTC

4

Portr: Open source self hosted ngrok alternative designed for teams

https://github.com/amalshaji/portr

What my project does?

Portr is a tunnelling solution that let's you expose local http/tcp connections to the public internet.

Target audience

Developers that work with webhooks or callbacks. Or anyone who want to quickly share their dev server with others.

Comparison

  • Monitor connections, and manage teams and members via the admin dashboard. Watch video
  • Inspect and replay HTTP requests using portr inspector. Watch video.

The admin backend is built using Python. I would appreciate a feedback.

2 Comments
2024/04/01
17:14 UTC

513

pointers.py being added to the standard library!

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.
34 Comments
2024/04/01
16:09 UTC

0

Better PDF Layout Detection for LLM's + table support

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.

3 Comments
2024/04/01
16:00 UTC

97

Drawpyo: create Draw.io diagrams with Python

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!

GitHub Page

Docs

What My Project Does:

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.

Target Audience:

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.

Comparison:

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.

18 Comments
2024/04/01
14:52 UTC

0

How to parallelize Pandas with Modin

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/

6 Comments
2024/04/01
14:17 UTC

44

AnyPathLib - A unified PathLib-like API for cloud storages

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 :)

21 Comments
2024/04/01
07:03 UTC

173

The Best* Python Cheat Sheet

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.

14 Comments
2024/04/01
03:15 UTC

0

Monday Daily Thread: Project ideas!

Weekly Thread: Project Ideas 💡

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.

How it Works:

  1. Suggest a Project: Comment your project idea—be it beginner-friendly or advanced.
  2. Build & Share: If you complete a project, reply to the original comment, share your experience, and attach your source code.
  3. Explore: Looking for ideas? Check out Al Sweigart's "The Big Book of Small Python Projects" for inspiration.

Guidelines:

  • Clearly state the difficulty level.
  • Provide a brief description and, if possible, outline the tech stack.
  • Feel free to link to tutorials or resources that might help.

Example Submissions:

Project Idea: Chatbot

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

Project Idea: Weather Dashboard

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

Project Idea: File Organizer

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! 🌟

1 Comment
2024/04/01
00:00 UTC

48

Wrote a Python script for downloading images/GIFs from subreddits in bulk.

  • What my Project Does

It's a program that searches through posts in a specified subreddit and downloads images, gifs etc.

  • Target Audience

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

  • Comparison

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

10 Comments
2024/03/31
23:21 UTC

8

Futurama API written in async Python

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

9 Comments
2024/03/31
19:24 UTC

70

Does anyone actually use PyPy or Cython?

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.

65 Comments
2024/03/31
10:54 UTC

1

Sunday Daily Thread: What's everyone working on this week?

Weekly Thread: What's Everyone Working On This Week? 🛠️

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!

How it Works:

  1. Show & Tell: Share your current projects, completed works, or future ideas.
  2. Discuss: Get feedback, find collaborators, or just chat about your project.
  3. Inspire: Your project might inspire someone else, just as you might get inspired here.

Guidelines:

  • Feel free to include as many details as you'd like. Code snippets, screenshots, and links are all welcome.
  • Whether it's your job, your hobby, or your passion project, all Python-related work is welcome here.

Example Shares:

  1. Machine Learning Model: Working on a ML model to predict stock prices. Just cracked a 90% accuracy rate!
  2. Web Scraping: Built a script to scrape and analyze news articles. It's helped me understand media bias better.
  3. Automation: Automated my home lighting with Python and Raspberry Pi. My life has never been easier!

Let's build and grow together! Share your journey and learn from others. Happy coding! 🌟

4 Comments
2024/03/31
00:00 UTC

27

remove background from image using AI in just 5 lines of python code

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

https://www.youtube.com/watch?v=js7AYKkZvFI

31 Comments
2024/03/30
15:54 UTC

1

Saturday Daily Thread: Resource Request and Sharing! Daily Thread

Weekly Thread: Resource Request and Sharing 📚

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!

How it Works:

  1. Request: Can't find a resource on a particular topic? Ask here!
  2. Share: Found something useful? Share it with the community.
  3. Review: Give or get opinions on Python resources you've used.

Guidelines:

  • Please include the type of resource (e.g., book, video, article) and the topic.
  • Always be respectful when reviewing someone else's shared resource.

Example Shares:

  1. Book: "Fluent Python" - Great for understanding Pythonic idioms.
  2. Video: Python Data Structures - Excellent overview of Python's built-in data structures.
  3. Article: Understanding Python Decorators - A deep dive into decorators.

Example Requests:

  1. Looking for: Video tutorials on web scraping with Python.
  2. Need: Book recommendations for Python machine learning.

Share the knowledge, enrich the community. Happy learning! 🌟

1 Comment
2024/03/30
00:00 UTC

25

SQLModel vs native SQL Alchemy ORM for a web backend?

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.

15 Comments
2024/03/29
22:22 UTC

1

Friday Daily Thread: r/Python Meta and Free-Talk Fridays

Weekly Thread: Meta Discussions and Free Talk Friday 🎙️

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!

How it Works:

  1. Open Mic: Share your thoughts, questions, or anything you'd like related to Python or the community.
  2. Community Pulse: Discuss what you feel is working well or what could be improved in the /r/python community.
  3. News & Updates: Keep up-to-date with the latest in Python and share any news you find interesting.

Guidelines:

  • All topics should be related to Python or the /r/python community.
  • Be respectful and follow Reddit's Code of Conduct.

Example Topics:

  1. New Python Release: What do you think about the new features in Python 3.11?
  2. Community Events: Any Python meetups or webinars coming up?
  3. Learning Resources: Found a great Python tutorial? Share it here!
  4. Job Market: How has Python impacted your career?
  5. Hot Takes: Got a controversial Python opinion? Let's hear it!
  6. Community Ideas: Something you'd like to see us do? tell us.

Let's keep the conversation going. Happy discussing! 🌟

2 Comments
2024/03/29
00:00 UTC

19

Python state management

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.

7 Comments
2024/03/28
16:12 UTC

7

Creating a GeoTIFF raster XYZ tile service in python with caching capability

1 Comment
2024/03/28
16:10 UTC

117

Automating Python with Google Cloud

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!

28 Comments
2024/03/28
15:29 UTC

0

No More Cutting in Line: Crafting Fairness in Queues using RQ in Python

I was trying to tackle the problem of queue fairness and here's how I solved it.

2 Comments
2024/03/28
12:29 UTC

51

Makefile Parser for Python

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

  • What My Project Does

This is a Makefile Parser, made in Python. The operational workflow is basically

Start --> Import File into dictionary --> Manipulation and Usage --> Processing --> Output --> Export
  • Target Audience (e.g., Is it meant for production, just a toy project, etc.)

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

  • Comparison (A brief comparison explaining how it differs from existing alternatives.)

I'm not sure if there are any other Makefile Parsers other than Pymake2, but the idea is similar to the aforementioned ideas

22 Comments
2024/03/28
02:36 UTC

5

Thursday Daily Thread: Python Careers, Courses, and Furthering Education!

Weekly Thread: Professional Use, Jobs, and Education 🏢

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.


How it Works:

  1. Career Talk: Discuss using Python in your job, or the job market for Python roles.
  2. Education Q&A: Ask or answer questions about Python courses, certifications, and educational resources.
  3. Workplace Chat: Share your experiences, challenges, or success stories about using Python professionally.

Guidelines:

  • This thread is not for recruitment. For job postings, please see r/PythonJobs or the recruitment thread in the sidebar.
  • Keep discussions relevant to Python in the professional and educational context.

Example Topics:

  1. Career Paths: What kinds of roles are out there for Python developers?
  2. Certifications: Are Python certifications worth it?
  3. Course Recommendations: Any good advanced Python courses to recommend?
  4. Workplace Tools: What Python libraries are indispensable in your professional work?
  5. Interview Tips: What types of Python questions are commonly asked in interviews?

Let's help each other grow in our careers and education. Happy discussing! 🌟

1 Comment
2024/03/28
00:00 UTC

22

Load Apple's .numbers Files into Pandas

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.

0 Comments
2024/03/27
21:44 UTC

Back To Top