mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-02-07 07:00:49 +03:00
Removing old @inject decorator examples
This commit is contained in:
parent
3d354a1cb5
commit
a94864d654
|
@ -1,61 +0,0 @@
|
||||||
"""`@inject` decorator example.
|
|
||||||
|
|
||||||
Flask is required to make this example work.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import sqlite3
|
|
||||||
|
|
||||||
from flask import Flask
|
|
||||||
|
|
||||||
from objects.providers import Factory
|
|
||||||
from objects.providers import Singleton
|
|
||||||
from objects.injections import KwArg
|
|
||||||
from objects.injections import Attribute
|
|
||||||
from objects.decorators import inject
|
|
||||||
|
|
||||||
|
|
||||||
class ObjectA(object):
|
|
||||||
|
|
||||||
"""ObjectA has dependency on database."""
|
|
||||||
|
|
||||||
def __init__(self, database):
|
|
||||||
"""Initializer.
|
|
||||||
|
|
||||||
Database dependency need to be injected via init arg."""
|
|
||||||
self.database = database
|
|
||||||
|
|
||||||
def get_one(self):
|
|
||||||
"""Select one from database and return it."""
|
|
||||||
return self.database.execute('SELECT 1').fetchone()[0]
|
|
||||||
|
|
||||||
|
|
||||||
# Database and `ObjectA` providers.
|
|
||||||
database = Singleton(sqlite3.Connection,
|
|
||||||
KwArg('database', ':memory:'),
|
|
||||||
KwArg('timeout', 30),
|
|
||||||
KwArg('detect_types', True),
|
|
||||||
KwArg('isolation_level', 'EXCLUSIVE'),
|
|
||||||
Attribute('row_factory', sqlite3.Row))
|
|
||||||
|
|
||||||
object_a_factory = Factory(ObjectA,
|
|
||||||
KwArg('database', database))
|
|
||||||
|
|
||||||
|
|
||||||
# Flask application.
|
|
||||||
app = Flask(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
# Flask view with inject decorator.
|
|
||||||
@app.route('/')
|
|
||||||
@inject(KwArg('database', database))
|
|
||||||
@inject(KwArg('object_a', object_a_factory))
|
|
||||||
def hello(database):
|
|
||||||
one = database.execute('SELECT 1').fetchone()[0]
|
|
||||||
return 'Query returned {0}, db connection {1}'.format(one, database)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
app.run()
|
|
||||||
|
|
||||||
# Example output of "GET / HTTP/1.1" is:
|
|
||||||
# Query returned 1, db connection <sqlite3.Connection object at 0x1057e4030>
|
|
|
@ -1,23 +0,0 @@
|
||||||
"""`@inject` decorator example."""
|
|
||||||
|
|
||||||
from objects.providers import NewInstance
|
|
||||||
from objects.injections import KwArg
|
|
||||||
from objects.decorators import inject
|
|
||||||
|
|
||||||
|
|
||||||
new_object = NewInstance(object)
|
|
||||||
|
|
||||||
|
|
||||||
@inject(KwArg('object_a', new_object))
|
|
||||||
@inject(KwArg('some_setting', 1334))
|
|
||||||
def example_callback(object_a, some_setting):
|
|
||||||
"""This function has dependencies on object a and b.
|
|
||||||
|
|
||||||
Dependencies are injected using `@inject` decorator.
|
|
||||||
"""
|
|
||||||
assert isinstance(object_a, object)
|
|
||||||
assert some_setting == 1334
|
|
||||||
|
|
||||||
|
|
||||||
example_callback()
|
|
||||||
example_callback()
|
|
Loading…
Reference in New Issue
Block a user