Discover packages and classes in a python project.
Project description
Roman Discovery
Micro-framework initialization problem
Micro-framework-based projects are clean while they're small. Every micro-framework codebase I've seen, has a mess in the project initialization. With time, create_app()
becomes filled with ad-hoc settings, imports-within-functions, and plug-in initializations.
The Application Factory Pattern, proposed, for example, in the official Flask documentation, and the Flask Mega-Tutorial, legitimize this approach.
The nature of create_app()
leaves no place for the open-closed principle. We update this module every time we add a new plug-in, a new blueprint, or a new package.
# myproject/__ini__.py
#
# A common Flask application. The code is based on the Flask Mega-Tutorial.
def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(config_class)
db.init_app(app)
migrate.init_app(app, db)
login.init_app(app)
mail.init_app(app)
bootstrap.init_app(app)
moment.init_app(app)
babel.init_app(app)
from myproject.errors import bp as errors_bp
app.register_blueprint(errors_bp)
from myproject.auth import bp as auth_bp
app.register_blueprint(auth_bp, url_prefix='/auth')
return app
A common Flask application. The code is based on the Flask Mega-Tutorial.
With deescovery
, you can make the same code shorter, and remove the dependencies from implementation details.
# file: myproject/app.py
from flask import Flask
from deescovery import discover
from deescovery.flask import get_flask_rules
def create_app():
flask_app = Flask(__name__)
flask_app.config.from_object("myproject.config")
discover("myproject", get_flask_rules("myproject", flask_app))
return flask_app
Visit the deescovery documentation site to learn more.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for deescovery-0.3.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 224a2dbacb74b67f8af1f71e89f9b9b90193560b6fa27cf6283a2857975aeb16 |
|
MD5 | 2e6151117bcea8966f1ff1408f78458c |
|
BLAKE2b-256 | 04161b282e8d99d54d372709058c310705d3ad7e1e2a9c4eb77c38ded577981f |