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
👋 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
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.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...
def do_database_work(..., session:SEssionDep):
# database operations here
def some_route(...):
# other work...
if some_condition:
do_database_work(..., session)
SessionDep and the with Session(...) context correct?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.
This is not a Python specific question, please delete your offtopic message, move to @PythonOfftopic and ask there.
Читать полностью…
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
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???
Читать полностью…
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
Читать полностью…
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
but ur Depends on the Flask already application looks so beautiful and most easy like sqlmodel docs say this...
Читать полностью…
Or even better, use flask-sqlalchemy and use any tutorial
Читать полностью…
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.
That is the point of DI
Bur you are clearly a beginner, so quit making stuff up
Learn flask with flask-sqlalchemy
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.
Читать полностью…
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?
And you are working with Flask
I think looking at FastAPI docs only confuses you.
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),)
...
def get_session():
with Session(engine) as session:
yield session
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?
This is not a Python specific question, please delete your offtopic message, move to @PythonOfftopic and ask there.
Читать полностью…
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")
Hello, I want to become a data analyst. Is there anyone who can help me?
Читать полностью…
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/
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
Читать полностью…
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
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.
This is not a Python specific question, please delete your offtopic message, move to @PythonOfftopic and ask there.
Читать полностью…
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
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
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)
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
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