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

or if i will use normall call the database crud functions i will use with session(engin) this concept so in this time i dont need the session:Session=Depends(..) i dont need in fastapi also in flask i dont need those?
or my what i say is not understandable to you understna or not, i search about di, it looks like that example, but the di, here whcih comes into my hearth-attack of understand and use in reality.

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

Python

You can use any general DI library for that - it's justa google away

But many people argue that in Python you dont really need DI...

Thats up to you to use it or not 🤷‍♂

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

Python

i really trying to understand and do things as u say i will really do one day like this💪

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

Python

this is what on most confusing things u say to do .
its the most hardest and most not understanding things i ever realize, who in reality do this type of things 😔

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

Python

owo what a magical idea you give me.
so the important part i understand is that on case of fastapi, on each request from the web request only the SessionDep got executed is this the concept of Depends() of the fastapi and equivalent on the flask-di 's Depends() ?

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

Python

see from this a docs i found this, and the things is, when you say the fun which do yield somethings, this time it returns a value based on each time it call the fun, but the fun not executes, rather the loop works and return values one by one, like this in case of yield,

def give_date(
sdate: date,
edate: date,
) -> Iterator[date]:

delta = edate - sdate
print("This is executing")
for i in range(delta.days + 1):
day = sdate + timedelta(days=i)
print("Today is", day)
yield day

in this case when i call this fun each time it returns one day then another like this, so still the for loop works continuously right, i think yes?

but when i came to see the code there,
def get_session():
with Session(engine) as session:
yield session

i found here is not any loop or iterator so how it send new session each time after one yield session has done, next time what happens, what iterator it get and return what session value?

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

Python

those are just meaningless job titles

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

Python

Python groups in other languages
(⚠️Not managed by us ‼️)

Arabic @arabipython
Azerbaijani @azepug
Chinese @pythonzh
French @python_francais
German @pythonde
Hebrew @pythonisrael
Tamil @pythontamilcommunity
Indonesian @pythonid
Italian @python_ita @pythonita
Kazakh @python_kz
Malaysia @pythonmalaysia
Persian @pyfarsi
Portuguese @pythonbr
Russian @ru_python_beginners @ru_python @python_scripts
Spanish @PythonEsp
Swahili @python_eafrica
Turkish @python_tr
Uzbek @python_uz

If you have an active and well managed group that you want to be listed there, ask in the Meta group, link in the rules page.

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

Python

Just use flask-sqlalchemy
And use any tutorial out there

Why are you so into this SQLModel when you do not understand the basics of their usage

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

Python

Hello all i was reading sqlmodel docs and it include the fastapi concepts see maybe i asked this but now i rethink and researched about htis and i have a doubt now:

HeroBase, HeroCreate, HeroUpdate, Hero, HeroUPdate, HeroPublic
These uppers are some classes for my hero related thigns,

@app.post("/heroes/", response_model=HeroPublic)
def create_hero(hero: HeroCreate, session: SessionDep):
# some code of working with db
db_hero:Hero # this Hero class is database class contains all info
return db_hero

From this function i return a Hero class obj, but response_model = HeroPublic so it means even i return the Hero class the fastapi actually sending the HeroPUblic to the end user upon this request fastpi received.
right so i am confused why we not actually do return HeroPUblic.Model_validate(db_hero) without need to write response_model,
why the docs say to use the response_model alwasy there, why not like the upper model_validate!!!
pls i am very not getting this, as this is the problem i am not able to fully understand and integrate sqlmodel with my flask app, pls help me i have this problem still now, what i will do now with this.


as i will use flask, that there is no response model in flask as u said

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

Python

Why are you jumpimg from Flask to FastAPI - to use SQLmodel?? What the f...

Best way to split the work is one function, one job. Update the user - 1 route, add new job listing - 1 route, etc...

And dependacies should be resolved at route level as that is how FastAPI works

You dependacies are created when request comes and destroyed when response leaves. Its request-based scoping.

An no, the get_session is not running the whole time, yield returnes the result and stops execution of function

You should learn about yield and how it works to understand get_session.

This article seams resonable to learn from
https://amitamola.medium.com/understanding-yield-in-python-b11e7e23d674

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

Python

This is your type checker (pylance)
Its not your concern why they do what they do.
Ig you really want to know about it, go work on the project, contribute, so you may learn

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

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

see what i found, the di logic is like, instead of passing the whole object to need a class work i pass them on time of construction of this object not with tight coupling. like for pay money we can use upi, paypal, credit card like this and on time of creating the obj which has a .pay() methods we pass one of the upper 3 thigns that willl a di, right,? but in the case of
session:Session= Depends(get_session)
this is also a di -concept like the upper i say?
but so in this case i always need to use this in the routs function, suppose i am using DDD, where my database function are kept in a separet moduel and i call this via applicaiton's service fun, which again call the database fun, so still i need to write the sessionDep in the routs fun why not in the service fun which call the database, ?

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

Python

i really read this a lot last some days see what i understand is,

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

@app.post("/heroes/", response_model=HeroPublic)
def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate):


so this session=Depends() is only valid for fastapi web dev things, so suppose i am using a normal cli app or anything that will do some database writting on some several time on my private os, then in this case i have a another thing which is not a web app ,so in thay case i cannot use the SessionDep logic like this, it is for only fastapi web and other case like tg bot api code, cli code i need to write full sqlmodel databsse operation function with session(engine) block always?

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

Python

Every good developer
Reading other peoples code is a valuable skill, and people are payed to review other peoples code.

Not only it makes you a better developer, but opens your eyes on the ways other people deal with some of their problems.

A lot of inspiration comes from reading other people source code. 🤷‍♂

The thing is that you don't understand it, any of it...
Thats the real issue, not that its hard (it's not, python reads like english most of the time)

So maybe - just maybe
Go back to basics
How it all works - from the start

Build a calculator
A todo list
A weather app (pulling live weather data from API)

Do not attempt to jump into web apps if you dont understand the basics of how they work

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

Python

SessionDep is Session Dependancy for short. Its annotated type which resolves get_session() function on each request that needs the session (as stated in route definition)

Depends is something FastAPI uses to resolve those dependacies - and yes, di-flask works the same way

Start working on your projects and stop overthinking it.

If you want to learn how all this works internally, go read python source code 🤷‍♂

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

Python

If i say its magic, would you belive me?
If so, just trust its magic and move on

If not, go learn and practice until you get it.
Its what context managers do (that with keyword)

You need to learn a lot more
Practice make perfect

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

Python

Hello good evening guyz I haven't been active here but have been a member pls I wanna learn python I needed an assistant

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

Python

This is such an interesting question:

Having studied and learned that, being able to write, you can consider yourself a middle Python developer?


In short: What you need to be able to do and what you need to know to consider yourself a middle

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

Python

i feel cry, i really trying hard to read and understand the sqlmodel as maybe now i am using flask later i will switch to fastapi like thing, as u say in fastapi i can do all the things of flask in fastapi, so now i will make code in flask with pure DDD separation, and later i can easily switch to flask, i learnt little db work in sqlmodel and its done so easily so i wish i will use sqlmodel

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

Python

SQLModel deals with databases
FastAPI is a web app framework

Two completly diffrent things - made by the same author

And no, in flask there is no response model, in flask you usually return HTML templates, not raw JSON data

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

Python

And Rana
Start learning as these questions clearly say you have not read or practiced any of this

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

Python

That depends on the database you use and datatypes it can handle 🤷‍♂

Like in postgres you have many ways to define precision on specific type on numeric data, while in SQLite, its just a float

Learn about database (you use) datatypes - read its documentation

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

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

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