Global State, How To Do IT?
im kind of a newbie so take me easy i face a problem every time i make a project specially Client projects, which is Global State i always struggle to do it for example, here is the structure of one of my Project Templates TelegramBot data/ -- settings.py # configs -- text.yaml # message handling plugins/ # telegram SDKs stuff src/ # this can be "api", "utils", whatever helpers/ # sepcific functions to some user flows middlewares/ # middlewares, duh -- utils.py # functions used all over app -- strings.py # uses text.yaml and provides an API for messages -- database.py # DB API -- step.py # User Prompting API -- logger.py # logging app.py # entry point .gitignore (etc, etc) i always struggle on how to define a Global State or Config which has Database Session, Strings API, Prompting AP every way i think of it doesn't feel right one soloution i found is a global main.py files on the src directory # src/main.py from database import DB from step import Step from strings import Strings from data.settings import * db = DB() step = Step(db) strings = Strings() but this has many problems like What if the step API needed something from the strings API What if i needed to do circular imports (python) Why Would i even need to do a circular import?, this does not seem like best practice and i end with: This Template Sucks (deletes it and start a new one) is there a BEST or CLEAN way or practice of passing global state into all the project? im still a new Coder, i have learned python 1 and a half year ago
im kind of a newbie so take me easy
i face a problem every time i make a project specially Client projects, which is Global State i always struggle to do it
for example, here is the structure of one of my Project Templates
TelegramBot
data/
-- settings.py # configs
-- text.yaml # message handling
plugins/ # telegram SDKs stuff
src/ # this can be "api", "utils", whatever
helpers/ # sepcific functions to some user flows
middlewares/ # middlewares, duh
-- utils.py # functions used all over app
-- strings.py # uses text.yaml and provides an API for messages
-- database.py # DB API
-- step.py # User Prompting API
-- logger.py # logging
app.py # entry point
.gitignore
(etc, etc)
i always struggle on how to define a Global State or Config which has Database Session, Strings API, Prompting AP
every way i think of it doesn't feel right
one soloution i found is a global main.py
files on the src
directory
# src/main.py
from database import DB
from step import Step
from strings import Strings
from data.settings import *
db = DB()
step = Step(db)
strings = Strings()
but this has many problems like
What if the step API needed something from the strings API
What if i needed to do circular imports (python)
Why Would i even need to do a circular import?, this does not seem like best practice
and i end with: This Template Sucks (deletes it and start a new one)
is there a BEST or CLEAN way or practice of passing global state into all the project?
im still a new Coder, i have learned python 1 and a half year ago