python | Unsorted

Telegram-канал python - Python

135201

You will be muted until you read the rules. Read them! @Rules_for_Python A group about the Python programming language. Offtopic things go here: @pythonofftopic Resources to learn python: @pythonres Group for Hy: @hylang Selenium: @SeleniumPython

Subscribe to a channel

Python

👋 Hello everyone!
I hope you guys are familiar with FastAPI, SQLModel, and Pydantic. I’m trying to understand something by reading the docs about how SessionDep works in FastAPI 🙏
When i came across the examples

def get_session():
with Session(engine) as session:
yield session

@app.post("/heroes/"):
def create_hero(hero: Hero, session: SessionDep) -> Hero:
session.add(hero)
session.commit()
session.refresh(hero)
return hero

My understanding is that when create_hero() starts executing by my fastapi get a request from user's browser, the session comes from get_session(), which means the function is effectively running while the with Session(engine) as session: context is active. Is that correct? 🤔 i think Yes.

Now, suppose I have a routes.py function that does several different things:
def some_route(..., session: SessionDep):
# some other tracking, keep record, loggin work...
if some_condition:
# database-related work
# suppose non-database work...

In this case, I don’t necessarily want the entire route function to depend on or use the database session. I would rather keep the database-related logic inside a separate function/service, and only pass/use the session there when database work is actually needed.
For example, something conceptually like:
def do_database_work(..., session:SEssionDep):
# database operations here

def some_route(...):
# other work...

if some_condition:
do_database_work(..., session)

❓ My questions:
Is my understanding of SessionDep and the with Session(...) context correct?
Is there a recommended/best-practice way in FastAPI/SQLModel to avoid injecting SessionDep outside the routes function. as i see the docs they use the SessionDep in the routes function directly, but maybe based on the application i will make the requirements is not always talking with database or conditionally.

What is the cleanest architecture for handling this in a real FastAPI project i think to not pass the SessionDep in the routs funciton, so where you say to keep this part and how when i will resturcter the routes fun small and call others function to do the work.
? 🏗

Читать полностью…

Python

This is not a Python specific question, please delete your offtopic message, move to @PythonOfftopic and ask there.

Читать полностью…

Python

today I was exploring Python’s built-in Decimal functionality and came to see something that confused me. 🤔
I used Ctrl + Click in VS Code to jump into the definition, and it opened this file:
~/.vscode/extensions/ms-python.vscode-pylance-2026.3.1/dist/typeshed-fallback/stdlib/_decimal.pyi
At the beginning of the file, I noticed imports like this:

import sys
from decimal import (
Clamped as Clamped,
Context as Context,
ConversionSyntax as ConversionSyntax,
Decimal as Decimal,
DecimalException as DecimalException,
DecimalTuple as DecimalTuple,
DivisionByZero as DivisionByZero,
DivisionImpossible as DivisionImpossible,
DivisionUndefined as DivisionUndefined,
FloatOperation as FloatOperation,
Inexact as Inexact,
InvalidContext as InvalidContext,
InvalidOperation as InvalidOperation,
Overflow as Overflow,
Rounded as Rounded,
Subnormal as Subnormal,
Underflow as Underflow,
_ContextManager,
)
from typing import Final, TypeAlias

what i see most of the thigns it did import xyz as xyz, why it need to write like this, is this how logic, and in our code, should we have somethigns like this, i am confused why official python write like this so duplicated thigns as the write the same thigns after import the name was also same so why they write like same.

What I don't understand is: why are they importing something with as and giving it exactly the same name? 😅Since the name before and after as is identical, isn't this basically the same so why???

Читать полностью…

Python

Im new to gen ai i wanna learn gen ai and agentic ai .it feels the trend r complicated as im new cany anyone help me where to start

Читать полностью…

Python

Thanks Again Again Again 💚 i will try to learn this

Читать полностью…

Python

I Recently see one things when reading the sqlmodel docs, see there it say,
like, 1.1 + 2.2 will be not equals to 3.3 but it will near of 3.3 like,

price = 1.1
tax = 2.2
total = price + tax
print(total)
—->
3.3000000000000003

so as i though in my website when i will do the money storing, i will not use this like upper things, so do you guys really say i should not use this or when i will get the total i will use f"{total:2f}", what do you guys say like i will keep record of the prices, maybe i dont know beforehand what the user input will be given.
so i will never use any column:float?

or do you guys say i will really use the Decimal.
but when in my frontend it will send me value as float so i will use Decimal will this really ok to use or do you guys think point a little more is very problamatic or not?

Читать полностью…

Python

but ur Depends on the Flask already application looks so beautiful and most easy like sqlmodel docs say this...

Читать полностью…

Python

Or even better, use flask-sqlalchemy and use any tutorial

Читать полностью…

Python

i tried to learn and watch the library you made so that i can use this in my application of flask,
see i found there,

https://github.com/razorblade23/di-flask/blob/master/tests/test_basic.py
here how i will do test? i cloned and did uv sync already.
but how the test i will perform on this repo you wrote here.

Читать полностью…

Python

That is the point of DI
Bur you are clearly a beginner, so quit making stuff up

Learn flask with flask-sqlalchemy

Читать полностью…

Python

i found by reaction that most of here say to learn using flask-sqlalchemy, but i did some docs checking here, and thinks when i can do this easily with less code and clean code in di like concept of fastapi with sqlmodel + flask so so will not it be more problamatic or waste of time to learn and use flask-sqlalchemy as here is also session like concept not properly understandable.

Читать полностью…

Python

Hey Guys,
Do You think, using Flask+ Sqlmodel is ok or
flask + flask-sqlalchemy will more better choice,

as i am confusd that sqlmodel to use i need to use session like concept and need di for writing real db codes, or with flask-sqlalchey with flask.g and find seassion will be more better, i am confusd what looks more realistic for real production?

Читать полностью…

Python

And you are working with Flask
I think looking at FastAPI docs only confuses you.

Читать полностью…

Python

see in sqlmodel > fastapi docs it shows the database function in the routes level code.
But see in my flask routes.py when i my fun execute for a endpoint like
get /heros
a function is executed, and this fun call another database related function, 'get_all_heroes' , traditionay i have a with session block in that db function, but as u say to use Depends from ur library, so in this case i will use

def get_all_heroes(*,session: Session = Depends(get_session),)
...

i will use like this right?
but i not get to understand as in ur readme u shows ur function return session, but for fastapi it yield session in a with block, so will i use the same

def get_session():
with Session(engine) as session:
yield session


when i will use ur library?

will it effects full same for this!

Читать полностью…

Python

yes i will try to learn and understand the di more better way, then maybe read and use the ur library this,
why don't you have not any documentation page for this library, is this because ur library is just use in one fun with Depends only this is the work of this library?

Читать полностью…

Python

This is not a Python specific question, please delete your offtopic message, move to @PythonOfftopic and ask there.

Читать полностью…

Python

from this things what i found, on time of adding, subtract, multiple, devision and others like this math thigns with flaot values, i need to use the

Decimal("float_value") +-*/ Decimal("another_float_value")

and keep the result to shows to outer things, but i am confused in my database in sqlmodel when i will want to store the price of some items/course and also the discounted price also, i will store the value as float and later when showing to use i will calculate using Decimal() or i will store as str, and later calculate with Decimal() and shows this final result to the user. what do you think in reality what my column idea will be there?

Читать полностью…

Python

Please best source for numpy

Читать полностью…

Python

Hello, I want to become a data analyst. Is there anyone who can help me?

Читать полностью…

Python

You may want to read up on
https://www.freecodecamp.org/news/what-is-a-floating-point-arithmetic-problem/

Then after reading that article, read this one
https://realpython.com/ref/stdlib/decimal/

Читать полностью…

Python

honestly at this point just pick flask-sqlalchemy and stop overthinking the DI stuff, youre going in circles 😭 but if you really want the yield pattern it does work the same

Читать полностью…

Python

Rule 1️⃣: Speak English.

Читать полностью…

Python

What is that got to do with using the library?
And why do you keep existing on using DI?
You dont need it, you can just use with Session(engine) each time and be done with it

Читать полностью…

Python

Anyone Who useed sqlmodel and sqlite like this thigns can say me how this happens?

the docs say thsi exactly, but how i can apply this the sqlmodel docs not say this can anyone know how to do this in real production code,


Some databases don't support foreign key constraints.
For example, SQLite doesn't support them by default. They have to be manually enabled with a custom SQL command:
PRAGMA foreign_keys = ON;
So, in general it's a good idea to have both cascade_delete and ondelete configured.

Читать полностью…

Python

This is not a Python specific question, please delete your offtopic message, move to @PythonOfftopic and ask there.

Читать полностью…

Python

Look, you dont need to use this DI or anything
But i think you should start with flask-sqlalchemy as its much easyer to find tutorials on this combination, then when you learn how sqlalchemy works (which SQLModel is based on) it will be easyer to adapt SQLModel to Flask

Читать полностью…

Python

actually i will use Sqlmodel, and sqlmodel docs includes the fastapi section, so it got little problamatic, one thing if i will use yield to generate the session and use this in my database function, then when does this with block ends in my routes or in my database function, so how it knows when to finish
flask-di> Depends
knows when to finish this with block!
as fastapi docs say after ends of one request the session got removed automatically.

docs:-

If it had yield, then it will continue the rest of the execution once you are done sending the response. In the case of the session, it will finish the cleanup code from the with block, closing the session, etc.And because dependencies can use yield, FastAPI will make sure to run the code after the yield once it is done, including all the cleanup code at the end of the with block. So we are also fine with that. ✅

The with Block

Читать полностью…

Python

Yes, the usecase is the same, it does not care how you get the object or what that injected object is.

In the end, you inject into the function whatever you want, it can be anything you want.

Examples on FastAPI site are meant for beginners, separating routing from your models and controllers is the usual pattern in real life called MVC (Model-View-Controller)

Читать полностью…

Python

Its a very simple thing that adds request-based dependancy injection

There is like 5 things it does, so i dont feel like spinning up the webpage just to show what is in the readme.

Its very simple peace of code (~130 lines of code) and something i use personally on my flask apps.

Readme with a few examples is really everything you need to use it.

As a beginner, i would advise you to not force yourself onto these specific design patterns, but to build your project in the most crude and simplest manner

Then when you are happy with how it WORKS, then you can refactor it and make it more "pythonic" or whatever

Читать полностью…

Python

Just read the description in readme.

It provide Depend() like functionality for flask apps.

You won't use FastAPI one, that's a completely different framework.

My wrapper library just provides dependency injection in Depends() style.

You should maybe learn about dependency injection first?
https://dev.to/hongster85/dependency-injection-understand-in-3-minutes-3a5k

Читать полностью…
Subscribe to a channel