Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
Undo an objects save? Database transactions?

I know enough Django to have a reasonably straight forward app with some complexity, with a dozen models, several dozen CBV and FBV, etc. But I don't know much about databases under the hood of Django. A friend was commenting on how a similar program/app to mine did not have an undo feature. From his perspective, when he changes a value on the website (and object save), he thinks the user should be able to undo the change. For example, a django app could have a form with 10 fields on it, where the form/view is using htmx and the fields are pre-filled with existing data. A user enters in some new values. My friend thinks a user should be able to undo any changes to revert to a previous value. My initial thought was that this would require a huge number of objects to be created. I then learned a bit about database transactions. My friend was essentially saying that database transactions are saved so that things can be rolled back, and that this is one of the primary features of a modern database. He gave an example of banks needing to

/r/django
https://redd.it/1cr3cs5
Making my filrst serious ecommerce website, need help....

Iam going to develop an ecommerce site from scratch all by myself to refresh my knowledge.

I have experience in handling amazon seller central and shopify at my current sales related job and wants to implement its features into my site as much as my abilities will allow me,
My current plan is to add 3 apps which are,
shop
contains list and display view of products will contain models for product, category

order
handles ordering , tracking etc...
will contain order table

seller
this is like an admin section, in which the sellers can schedule orders, add products, updatet its quantities, decide the categories it will fall into etc ...

but iam confsed about these things right now.

1. Should i add products manually to the database or call an api.
2. How can i add main and subcategories, iam clueless on this.
3. should i add a seplerate model for product images and add it as a foreign key to the product table and later many to many field to display them?

/r/django
https://redd.it/1cr1ii1
Frame - a new language for programming state machines in Python

Hey,

I am (re)releasing a project called Frame that I've been working on to create a language and transpiler to easily create state machines/automata in Python. It also is able to generate UML documentation as well.

This project is for people who are interested in programming state machines for a wide range of purposes such as game programming, workflows, MBSE modeling as well as school projects for comp sci theory. It is also useful simply for generating flow documentation.

The Framepiler (Frame transpiler) is in beta at this time. It would be great to get feedback from the Python community on any gaps in key functionality or bugs.

Low-code/no-code workflow tools are often problematic for creating state machine like flows. Frame is intended to give a textual way to accomplish the same thing, but without having to "draw" your software and with the ability to use all the standard devops tooling and processes for "normal" development processes.

There is also a VSCode extension and a playground environment to experiment in.

Very much hoping to connect with people who might find this interesting and useful. If that is you, please take a look at the Overview and the Getting Started articles. Here is a link to the GitHub

/r/Python
[https://redd.it/1cqoyuq
SQLPage - a Python library to add string token based pagination easily

What My Project Does - This is a Python package to easily add string token based pagination. Currently it supports SQLModel and SQLAlchemy ORMs.

Recently I wanted to add pagination in one of my Python projects and in the API response, I had to return a string next page token. Now I could not find a straight-forward way of doing this in Python. All of the tutorials or blog posts I saw, there in the response the server always returned a page_number, page_size, and total_elements and then the onus was on the calling service to adjust this accordingly. 

Comparison - The current packages and methods requires some changes in the app layer as well. I tried using a few but those did not satisfy the use case and were also a bit harder to implement. I could not find a easy to use option. The present ones returned integers instead of a string token

I wanted it to be simpler, just like OpenSearch - you call its search API and it returns 10 elements and a next_page_token and then for the next 10 (or you configure this using the size parameter) you use the next_page_token in the subsequent request to get to

/r/Python
https://redd.it/1cqzf6o
UXsim 1.3.0 released with vehicle tracking and improved vehicle routing

Main Changes

Add GUI functions
Vehicle tracking: You can now track a specific vehicle to see their route
Dataframe viewer: Stats can be confirmed
Improve vehicle routing functions
Add [example of routing optimization](https://github.com/toruseo/UXsim/blob/main/demos_and_examples/demo_notebook_07en_optimal_routing.ipynb)
Change documentation's theme for better indexing

UXsim

UXsim is a free, open-source macroscopic and mesoscopic network traffic flow simulator written in Python. It simulates the movements of car travelers and traffic congestion in road networks. It is suitable for simulating large-scale (e.g., city-scale) traffic phenomena. UXsim is especially useful for scientific and educational purposes because of its simple, lightweight, and customizable features, but users are free to use UXsim for any purpose.

/r/Python
https://redd.it/1cqstyd
Automatic face registration using python and face_recognition library.

We have achieved good accuracy of face recognition and after registration of faces of people captured in frames only if played high quality video, However, we get very low accuracy when video quality goes down. What can be done?

Your suggestion will be appreciable.

/r/Python
https://redd.it/1cr5ggr
collectstatic command not detecting new changes using s3 bucket

hello everyone i am facing an issue. when i first moved all my staticfiles to s3 bucket it all went fine but as time went on and i started adding new files or making changes to my css i noticed that whenever i push and try to run collectstatic command it shows no 0 static files copied which then leads me to start manually uploading new files to my bucket every time i make a change. please how can i solve this issue.

/r/djangolearning
https://redd.it/1crb068
Issue testing view function w/SQLAlchemy and pytest

Hey all, I'm having an issue with a user route to soft delete the user. My goal is to append '\_deleted' to the username and email, and set the active status to zero. Here's the route:

@users.route("/delete-account", methods=['GET', 'POST'])
@login_required
def delete_account():
current_user.active = 0
current_user.email = current_user.email + '_deleted'
current_user.username = current_user.username + '_deleted'
db.session.commit()
logout_user()
flash("Your account has been deleted.", category='info')
return redirect(url_for('users.login'))



My conftest.py file mimics the blog post from Alex Michael ([here](http://alexmic.net/flask-sqlalchemy-pytest/)) and contains the following:

import pytest
from application import create_app, db as _db
from application.config import UnitTestConfig
from application.models import User
from werkzeug.security import generate_password_hash


@pytest.fixture(scope='session')
def app(request):


/r/flask
https://redd.it/1crdtr2
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](#daily-beginner-thread-link) 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](https://discord.gg/python) 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

/r/Python
https://redd.it/1crefjo
Implementing your own pypi clone

Hi,

Just want to know how difficult is it to manage your own pypi clone and how do you recommend to create a seperation between dev and prod systems.

/r/Python
https://redd.it/1crksf7
D Full causal self-attention layer in O(NlogN) computation steps and O(logN) time rather than O(N^2) computation steps and O(1) time, with a big caveat, but hope for the future.

*Update*: Actually O(N) computation steps(not O(Nlog N)) and O(log N) time.

I think I figured out how to do self-attention in transformer models in O(NlogN) computation steps rather than O(N\^2), with a caveat. I'm not trying to be an academic, so I don't care to publish this formally, but I thought that some people might be interested. My construction is not efficient or practical, but the fact that it can be done at all might motivate further work to find efficient alternatives.

tl;dr Use the parallel scan[1\] technique to compute taylor series basis functions needed to compute the causal self-attention layer and sum these together weighted by the values vector and 1 to get the numerator and denominator of the softmax activation of the full causal self-attention layer. The basis functions that you have to compute are both the basis functions for the numerator of the self-attention layer, $$\\sum_{i=0}\^{j-1} k(i)_a\^n q(j)_b\^m v(i)$$ and the normalization $\\sum_{i=0}\^{j-1} k(i)_a\^n q(j)_b\^m$. k(i)_a\^n is component-a of the ith key vector raised to the power of n multiplied by q(j)_b\^m which is component-b of the jth query vector raised to the power of m, which is multiplied by the value vector at position i in the first

/r/MachineLearning
https://redd.it/1cri6h6
modern_colorthief - Modified Median Cut Quantization algorithm in rust + python

* [github](https://github.com/baseplate-admin/modern_colorthief)
* [documentation](https://modern-colorthief.readthedocs.io/en/latest/)

## What my project does :

It gets the dominant color/color palette from given image.


## Target Audience:

Anyone

## Usage

modern_colorthief exposes two functions get_color and get_palette

Here is how to use get_color:

```python
from modern_colorthief import get_color

# Path to any image
path = ...

print(get_color(path)) # returns tuple[int,int,int]
```


Here is how to use get_palette:

```python
from modern_colorthief import get_color

# Path to any image
path = ...

print(get_palette(path)) # returns list[tuple[int,int,int]]
```



# Goals:

- Bring [color-thief-rs](https://github.com/RazrFalcon/color-thief-rs/) to python

# Benchmarks:

[Written in deatils](https://modern-colorthief.readthedocs.io/en/latest/benchmarks.html)

Gist:

```python
Python Took: 0.09976800000004005
CPP Took: 0.008461299999908078
RUST Took: 0.008549499994842336


Python Took: 0.0960583999985829
CPP Took: 0.008564600000681821
RUST Took: 0.007692700004554354
```



# Differences

## With fast-colorthief

- Supports more architectures. ( pybind11 vs pyo3 )
- Doesn't have a hard dependency on numpy
- Code is simple compared to fast-colorthief's CPP codebase
- Automated tooling powered by maturin and github-actions
- The size

/r/Python
https://redd.it/1crouri
Changing flask session in a middleware

Hi,

I would like to change the flask session to set a default value whenever the user is authenticated and if there isn't a value yet:

class VersionSessionMiddleware:
    def init(self, app):
        = app
       
    def call(self, environ, startresponse):
        if not constants.SESSION
VERSIONNAME in session:
            session[constants.SESSION
VERSIONNAME] = constants.DEFAULTVERSION

        return self.app(environ, startresponse)self.app

This fails with `RuntimeError: Working outside of request context`. And I can access to current\
user from Flask-Login, it is None.

From these results, I think a middleware is not the solution I'm looking for. What tool can I use to do that?

Thanks.

/r/flask
https://redd.it/1crq4wu
Flask MySQL database session best practice – resolve Broken Pipe Error

My question for this post: Is my approach for using and relying before\_request and after\_request to handle opening and closing database sessions ok or just a mess?

# Background

I have a Python Flask Website, a different Flask app that is the API, and a service Python app that the API can call when needed that connect to the same MySQL database. They all use SQLAlchemy to communicate with the same MySQL database.

Every morning, I find a similar Broken Pipe Error which prevents the first user for the day from logging in right away. They will need to try a few times to rollback and close the session. This is the problem I am ultimately trying to resolve.

My approach has been, instead of trying to find the loose end database session, I thought I could cut off an expiring database session by using the before_request decorator to instantiate a db session. Then after_request decorator to close the session after each request.

This doesn’t seem to be working. I have not prevented Broken Pipe errors.

Should I just create a database session when I need it and close it? Then if I find Broken Pipe error try to find the source of the problem.

Diagram of

/r/flask
[https://redd.it/1cqus6q
Why isn't IPython more popular?

In the past years, I've had a lot of frustration with the python console:

- pasting multiline is complicated

- editing is impossible

- no syntax highligthing etc


Now I discover the IPython console...


It's wonderful... I use it all the time to try snippets, which I used to run in colab or in yupyter.

Why isn't this shell more used and more known?



/r/IPython
https://redd.it/1crp70l
D How do you get better at reading proof in the ML papers, with background in CS only?

Hi everyone, as the title, how do you get better at reading proof in the ML papers? The ML papers I mentioned are those in adversarial ML, e.g. Certified Adversarial Robustness via Randomized Smoothing. For context, I have basic knowledge of calculus, linear algebra, but most of the time when reading the proof, sometime I feel that one line just come out of nowhere, and I can't reason why or how they do it. Maybe because my background is CS, with focus on software, so I'm lacking of the rigorous proof-based math stuff. Please help!!

/r/MachineLearning
https://redd.it/1crr0fa
Is PyGame still alive?

So it was a long time ago in the good old Python 2.x days (circa 2010 probably) that I had learned PyGame with some tutorials at my former work place. But nowadays since I mostly freelance with business apps, I never felt the need for it.

But since such a game development project is on the horizon after all these years, I was wondering if PyGame can still be up for the task with Python 3.x? Or is there a better Python library available these days?

I don't need any advanced gaming features of modern day VFX or anything, all I need is some basic Mario/Luigi style graphics, that's all!

/r/Python
https://redd.it/1crsktg
Framework to use for backend

Hello guys

I recently decided to move from nodejs(expressjs) to python for general purposes but mostly for backend.
I have couple of questions.

1. Will i regret my migration to python? :)

2. Which framework you suggest for backend solo dev?

And what tips are you suggesting me in general to get used to python.


/r/Python
https://redd.it/1croj1r
OAuth flow for API integrations

Hey guys,


I want to start planning out a system so our multi-tenant application can connect to customer's CRM API via OAuth.

We'd be connecting to more than one CRM(obviously) and I'm trying to make the OAuth process as generalized as possible. Does anyone have any good references or tutorials I can follow?


Thanks a lot



/r/flask
https://redd.it/1crvelc
Declarative GUI Slint v1.6 released with Design Mode (WYSIWYG) Improvements

https://slint.dev/blog/slint-1.6-released

Slint is a declarative GUI toolkit to build native user interfaces for desktop and embedded applications. Find more information at https://slint.dev/ or check out the source code at https://github.com/slint-ui/slint.

EDIT: The Python APIs are currently in alpha. More info -- https://github.com/slint-ui/slint/tree/master/api/python

/r/Python
https://redd.it/1crsmbe