R
R
Relrin2015-10-07 23:20:09
Python
Relrin, 2015-10-07 23:20:09

What is the best way to initialize "internal" objects?

I have a certain class Application, in which a number of objects are defined inside that describe the application itself directly: a set of routes + views for them. It is natural to add support for middlewars, so that before calling the necessary handler for a request, perform a number of preprocessing actions (for example, check / add a token). For these middlewars, I need an initialization that, for example, would take either user settings (if defined) or default ones that I specify (for example, store tokens somewhere in memory if the user does not want to store in the database (or did not specify )).
The question is how to solve this problem beautifully and elegantly? So far it has occurred to me:
1) Forward the arguments that came to the Application class, and from it, inside, into the depth (that is, all constructors have a signature:

def __init__(self, *args, **kwargs):
  # берем какие-то настройки из аргументов/словаря и делаем нужное
  ....

what has been done at the current time, but not to say that it completely satisfies me completely.
2) Make something like a settings mechanism like in Django (there are some default + user settings). It seems to be cool, but then you also need to write your own script, through which the user will injure his script (again, like junge), instead of just running a script that contains:
if __name__ == '__main__':
    cmd = CommandLine()
    cmd.define('-ip', default='127.0.0.1', help='used ip', type=str)
    cmd.define('-port', default=8080, help='listened port', type=int)
    args = cmd.parse_command_line()

    app = Application()
    app.run(ip=args.ip, port=args.port, router=router)

Tell me which implementation method is optimal / better and why. Perhaps there are some even more interesting options?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question