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

so in your library if i will use, the ._resolve_dependency() this will call any Depends, if i have

SessionDep = Annotated[...]

will this also work, is htis means, even fun is not part of the current response-request thsi dependency will work always?

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

Python

and see i listen that this your library will not work with yield, as when i getting explanation of this library in some library with my little code i got to know, as i am not understand very very python concept i got to know like this,

Below Response from ai: was this how right can you check ur code:

There is one important issue here, though.
Your DIFlask implementation does:

value = dep_func(**kwargs)

It does not understand generator dependencies.
So if get_session() literally contains:
yield session

then calling:
get_session()

returns a:
Generator

not a Session.
⚠️ This is an important limitation/bug in the di-flask implementation you showed me.
FastAPI understands generator dependencies and performs cleanup.
This small library does not.

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

Python

For now, you can
I will implement a public one soon, so it will be just

db = current_app.resolve(get_db)

For now use the private method

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

Python

will i call like this private methods in my application?

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

Python

as thsi is not coming from response fun, so the user_repository:UserRepositoryDep will not resolve right?
see the message of this

i did integrate your library with many codes dependency, now i am not able actually to resolve this as my app just stop at this user_loader in open

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

Python

i make like this,

def create_app() -> Flask:
# app = Flask(
# import_name=__name__,
# )

app = DIFlask(
import_name=__name__,
)

login_manager.init_app( # type: ignore
app=app,
)
csrf.init_app( # type: ignore
app=app,
)
app.secret_key = settings.app.secret_key.get_secret_value()

app.register_blueprint(blueprint=auth_bp)
app.register_blueprint(blueprint=general_bp)

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

Python

I have this big issue, why this not shows how i can use factory pattern here easily, as per the docs,
app = DIFlask(name)

But see, for another case we use,

def create_app() -> Flask:
app = Flask(
import_name=__name__,
)

login_manager.init_app( # type: ignore
app=app,
)
csrf.init_app( # type: ignore
app=app,
)
di_flask = DIFlask()

di_flask.init_app( #This is not working why???
app=app,
)

app.secret_key = settings.app.secret_key.get_secret_value()

app.register_blueprint(blueprint=auth_bp)
app.register_blueprint(blueprint=general_bp)
return app

Where is the way i will integrate this library, as the docs say this is really differnet using the (__name) but will this upper , di_flask.init__app will i need or not if then how i will add, as this is a flask_realated library but it not working to integrate so how i will use ?

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

Python

My app workflow:

register route


RegistrationServiceDep


get_registration_service()


UserRepositoryDep


get_sqlmodel_repository()


SessionDep


get_session()

My confusion is maybe,

i dont know how i can connect this user_loader fun to use the session generated by the current request! or am i thinking little differently that my userloader callback can also automatically get the session anyhow?

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

Python

And nobody is forcing them to use anything
They can use whatever they want, and python venv is the basics of what they need

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

Python

why u are not saying them to use uv from beginning, as u say me at last in reality for production we should use uv as this will easy and so on...

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

Python

i think, i don't know

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

Python

actually you teached me all the DDD and python stuff from 0 stage, so you understand those concept of how the reality the structer i will follow, thats why before complete my code i wanted to know if that was doing correctly or this how to make the real production code . i am really trying to solve the all code in my repo with proper ddd, and i am trying to make code like you do so that one day you will hire me for your repo

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

Python

I’m trying to follow the DDD (Domain-Driven Design) pattern properly, but I’m still a little confused about one part only 😢.

see pls In Python When we follow to use the DDD structer to keep different database User logically separte, we need to convert one class to another.
Example: in my sql database User TAble has 30+ columns, but real business logic case it need only id_, email, password_hash this 3 thing for login implementation part.
Now i need to convert the UserModel to UserFlaskLoginClass (maybe any other class name having very few needy parameter using the @dataclass), as flask_login need a differnt class which need to inherit from UserMixin to have 4 propery and method availabe.
So i need to convert my database's table user to UserOfFlaskLogin class, in this case what do you say about should i convert from class to class using Pydantic's baseModel will be justified in real production.
As i listen pydantic class i can easily convert one into another, will it ok to use pydantic class for htis i need to use pydantic in presentation layer and also in the sqlalchemy layer will this justified and not breaks any rule???

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

Python

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

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

Python

Thats not how that works

https://flask.palletsprojects.com/en/stable/testing/

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

Python

And where exactly do i use yield in my *litle library* docs?
I dont see it?

Please stop, if you think i should add something, raise an issue or make a PR like a normal developer.

And nobody is forcing you to use it 🤷‍♂
Nobody is forcing you to do anything 🤷‍♂

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

Python

I said implement it for your own application 🤷‍♂

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

Python

why you write like this why not

def get_session() -> Generator[Session, None, None]:
with Session(engine) as session:
yield session

will it work or not, as i have not any get_db function?

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

Python

This is actually something i may need to add to library

You can use this example as a workaround until i implement a public method for this specific usecase (and others flask may need)

from flask import current_app
from flask_login import LoginManager
from flask_di import Depends

login_manager = LoginManager()

def get_db():
return SessionLocal()

@login_manager.user_loader
def load_user(user_id):
db = current_app._resolve_dependency(Depends(get_db))
return db.get(User, int(user_id))

Just implement it for your usecase

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

Python

the problem is
@login_manager.user_loader

here i cann't able to pass the Annotated's Depends from your library, as how this will resolve the Dependency?


@login_manager.user_loader
def load_user(
user_id: str,
user_repository: UserRepositoryDep,
) -> FlaskLoginUser | None:
obj_entity = user_repository.get_by_id(
user_id=user_id,
)

if obj_entity is None:
return None

return FlaskLoginUser(obj_entity)

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

Python

What part of

For automatic injection of dependecies we need to wrap Flask class

You dont understand?

Instead of importing flask you import DIFlask and use that as your flask app

from flask_di import DIFlask

app = DIFlask(__name__)

Copied from the docs 🤷‍♂

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

Python

i also want to for checking i used this type of code, but the problem is the dependency will how work in this user_loader callback


@login_manager.user_loader # type: ignore
def load_user(
user_id: str,
user_repository: UserRepositoryDep,
) -> FlaskLoginUser | None:
"""
https://flask-login.readthedocs.io/en/latest/#how-it-works
As per the docs it should return the obj or the None.

This will read the user_id from the cookie and convert into the user obj
from the user_obj i will get differnt data to use.
"""
obj_entity = user_repository.get_by_id(
user_id=user_id,
)

if obj_entity is None:
return None

return FlaskLoginUser(obj_entity)

the problem i found by other ai places also say here di will not work as user_loader decorator will not see the 2nd parameter of the Dep so it will not call, i tried a lot of ask to there but still this is not found any solution.

My main problem is when i am using session: by external dependency.py this got confused, else if i will use single session across my app all is become so easy to write my code

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

Python

Hello i have started to explore the di and so on and then integrat the di things in my own application and i want to use this perfectly, i used in my DDD structer of code where i am using like this:
for registration new user or login case i need to talk with sqlmodel so it need session, and as the docs satwe will use one engine over my repo and a new session per request so your di-flask library i used here like this i say below i am summarizing the codes form different parts in below:

def get_session() -> Generator[Session, None, None]:
with Session(engine) as session:
yield session

# TODO i need to confirm using this di-flask will ok
SessionDep = Annotated[Session,Depends(get_session)]

def get_sqlmodel_repository(
session: SessionDep,
) -> UserRepository: #This userrepository is a protocol class
return SQLModelUserRepository(
session=session,
)

if config.login_provier_value == "SQLMODEL":
user_repository_provider = get_sqlmodel_repository

UserRepositoryDep = Annotated[
UserRepository,
Depends(
user_repository_provider,
),
]

def get_registration_service(
user_repository: UserRepositoryDep,
) -> RegistrationService:
return RegistrationService( #this has business logics in methods
user_repository=user_repository,
)
# for login_service i will use like this upper UserRepositoryDep

# i will call this below Dep in the routes.py' function
RegistrationServiceDep = Annotated[
RegistrationService,
Depends(
get_registration_service,
),
]

# Below is my routes.py code having this,

@auth_bp.route(
rule="/register",
methods=["GET", "POST"],
)
def register(
register_service: RegistrationServiceDep,
)...

See like this i use the Depends() from your library which is now like the fastapi dependency, now what i think is fully correct to use now right fully?
As this readme says it will work like Nested Dependencies, so i thought this will work, but i cannot test this until i impliment the real flask_login login things, the problem i am getting is:
flask_login need a @user_loader which will call .find_user_by_id() method from the UserRepository, but how i will give there the session dep when it will use the Sqlmodel,

@login_manager.user_loader
def load_user(user_id):
return User.get(user_id)

How and where i will use the session if this will use sqlmodel, if it will use other then session will not use here, pls i really searched about this a lot lot full i dont get how i will do so pls help me how i will insert per session on this user_loader fun ?

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

Python

As that comes later
Whats the use of uv if you dont know what it does?

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

Python

Those are ready blocks of code for you to download, install and use in your own code.

You install them with pip and you mostly import them to your code

As how a specific library works, you have to consult its documentation to learn.

I higly recommend you learn about virtual environments in Python if you are plannning to use third party libraries
https://docs.python.org/3/library/venv.html

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

Python

Check out @PythonRes, a channel for Python resources - video courses, pdf books, tutorials and other info to help you learn Python.

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

Python

No, you dont need a special class for flask login
And you dont need to use UserMixin, just implement the functions iz needs from the docs

DDD does not mean you need a class for every thing in your code
Its the separating things into their own domains which is the point

So your store code does not mix with your users code.

So your payment logic is not intermixed with users and store

And please stop tagging me.
When you have a REAL question regarding actuall code, come back

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

Python

Check out @PythonRes, a channel for Python resources - video courses, pdf books, tutorials and other info to help you learn Python.

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

Python

Hey everyone. I started doing leetcode in python, could anyone find me a resource where I can get a quick recap of python and its functions that can help me do DSA?

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

Python

what are like this,
as i think i make one login mechanism and otp then i think for testin , i will serve my app in local and then there iw ill open the url and see all looks as i want and then also i will try to give wrong otp, correct otp, multple time i will submit and see if i am getting what i needed like this and then i think my app is ready to test on this new things i have implimentated.
this upper is what i know and learnt, what another test you means or how this come in a website i am making in flask

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