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
i understand, but all moments i read one new docs i think i am done with this and i am just capable to use the things but again new problem or concept comes quickly
Читать полностью…
@razorblade23 one thing i understand from ur idea that in di thing.class UserService:
def __init__(self, repository):
self.repository = repository
this is the di we use but for fastapi i have the Depends() only this things, which just help us to wirte so easily.
So what i will do actually like in my case like calling mail_service, payment_serbice i will erite those in the Depends or externally?
You cannot mix sync and async code
Flask being sync, you cannot have async functions (without spinning up your own async loop)
And that function returns a dictionary, and you can use it just like in the examples so i do not see any problems there.
One Request to you, from the reading and reading from many places about fastapi, di and so on i found one issue, is this issue or not...
As i read a docs of fastapi, it say with a code example demo:
async def common_parameters(q: str | None = None, skip: int = 0, limit: int = 100):
return {"q": q, "skip": skip, "limit": limit}
CommonsDep = Annotated[dict, Depends(common_parameters)]
@app.get("/items/")
async def read_items(commons: CommonsDep):
return commons
@app.get("/users/")
async def read_users(commons: CommonsDep):
return commons
And you can declare dependencies with async def inside of normal def path operation functions, or def dependencies inside of async def path operation functions, etc.
It doesn't matter. FastAPI will know what to do.
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, ?
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?
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
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 🤷♂
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
Hello good evening guyz I haven't been active here but have been a member pls I wanna learn python I needed an assistant
Читать полностью…
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
i also tried to use the flask-sqlalchemy like thing, but, i always think it a more hardwork to learn a full new library to do some crud operations , and it looks so problamatic not easy docs like sqlmodel have one.
Читать полностью…
What the hell are you talking about?
Just give up from DI, use flask-sqlalchemy and be done with it
Why are you so persistent on something you still have a long way to understand.
You are mixing up a lot of concepts and that is just spinning in circles, nothing good comes out of it.
One thing I understood after a lot of brainstorming, btw 😅
So, like in FastAPI route functions, we can define things like sessionDep, CommonParametersDep, etc., where they ultimately come from Depends(function_name).
Those dependencies just sit there as part of the route function's parameters. When a request comes in from the browser, FastAPI starts resolving the dependencies required by that route.
so once the dependency is executed and its value is generated, FastAPI injects that value into the corresponding parameter of the route function. Then the route function executes using those resolved dependency values.
So, basically:
Request → resolve dependencies → dependency functions get their values from the request → dependencies return their results → FastAPI injects those results into the route function → route function executes.
Is that understanding correct?
and thsi will be fully same if i will use the di-flask and use those in the fun parameters, so i can use many Depends in my fun paramters will this no problem at all and i just need to use all in only def funcion, not async def and then i am fully ok to use the library to use the sesion thigns?
i mentioned his razorblade message i dont know how it happened, what warnign of 1/3 i get? by the way i lost the big message i wrote last 5 minute long i typed the message and the bot delete the message 😅😢,
Читать полностью…
Have you any repo or code example written with di-flask's library codes?
Читать полностью…
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.
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 🤷♂
i really trying to understand and do things as u say i will really do one day like this💪
Читать полностью…
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 😔
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() ?
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
def get_session():
with Session(engine) as session:
yield session
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.