mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-25 19:14:00 +03:00
Refactor provider examples
This commit is contained in:
parent
1eee0fe529
commit
addaadac72
|
@ -1,6 +1,6 @@
|
|||
"""`Callable` providers with positional arguments example."""
|
||||
|
||||
from dependency_injector import providers
|
||||
import dependency_injector.providers as providers
|
||||
|
||||
|
||||
# Creating even and odd filter providers:
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
"""`Callable` providers delegation example."""
|
||||
|
||||
import sys
|
||||
|
||||
from dependency_injector import providers
|
||||
import dependency_injector.providers as providers
|
||||
|
||||
|
||||
# Creating some callable provider and few delegates of it:
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
"""`Callable` providers with keyword arguments example."""
|
||||
|
||||
import passlib.hash
|
||||
|
||||
from dependency_injector import providers
|
||||
import dependency_injector.providers as providers
|
||||
|
||||
|
||||
# Password hasher and verifier providers (hash function could be changed
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
"""Custom `Factory` example."""
|
||||
|
||||
from dependency_injector import providers
|
||||
import dependency_injector.providers as providers
|
||||
|
||||
|
||||
class User(object):
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
"""`DelegatedCallable` providers example."""
|
||||
|
||||
from dependency_injector import providers
|
||||
import dependency_injector.providers as providers
|
||||
|
||||
|
||||
def command1(config):
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
"""`DelegatedFactory` providers example."""
|
||||
|
||||
from dependency_injector import providers
|
||||
import dependency_injector.providers as providers
|
||||
|
||||
|
||||
class User(object):
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
"""`DelegatedSingleton` providers example."""
|
||||
|
||||
from dependency_injector import providers
|
||||
import dependency_injector.providers as providers
|
||||
|
||||
|
||||
# Some delegated singleton provider:
|
||||
|
|
|
@ -2,8 +2,7 @@
|
|||
|
||||
import sqlite3
|
||||
import contextlib
|
||||
|
||||
from dependency_injector import providers
|
||||
import dependency_injector.providers as providers
|
||||
|
||||
|
||||
class UserService(object):
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
"""`Factory` providers example."""
|
||||
|
||||
from dependency_injector import providers
|
||||
import dependency_injector.providers as providers
|
||||
|
||||
|
||||
class User(object):
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
"""`Factory` providers with attribute injections example."""
|
||||
|
||||
from dependency_injector import providers
|
||||
from dependency_injector import injections
|
||||
import dependency_injector.providers as providers
|
||||
import dependency_injector.injections as injections
|
||||
|
||||
|
||||
class User(object):
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
"""`Factory` providers with init positional injections example."""
|
||||
|
||||
from dependency_injector import providers
|
||||
import dependency_injector.providers as providers
|
||||
|
||||
|
||||
class User(object):
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
"""`Factory` providers with init injections priority example."""
|
||||
|
||||
from dependency_injector import providers
|
||||
import dependency_injector.providers as providers
|
||||
|
||||
|
||||
class User(object):
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
"""`Factory` providers with init keyword injections example."""
|
||||
|
||||
from dependency_injector import providers
|
||||
import dependency_injector.providers as providers
|
||||
|
||||
|
||||
class User(object):
|
||||
|
|
|
@ -1,60 +0,0 @@
|
|||
"""`Factory` providers with method injections example."""
|
||||
|
||||
from dependency_injector import providers
|
||||
from dependency_injector import injections
|
||||
|
||||
|
||||
class User(object):
|
||||
"""Example class User."""
|
||||
|
||||
def __init__(self):
|
||||
"""Initializer."""
|
||||
self.main_photo = None
|
||||
self.credit_card = None
|
||||
|
||||
def set_main_photo(self, photo):
|
||||
"""Set user's main photo."""
|
||||
self.main_photo = photo
|
||||
|
||||
def set_credit_card(self, credit_card):
|
||||
"""Set user's credit card."""
|
||||
self.credit_card = credit_card
|
||||
|
||||
|
||||
class Photo(object):
|
||||
"""Example class Photo."""
|
||||
|
||||
|
||||
class CreditCard(object):
|
||||
"""Example class CreditCard."""
|
||||
|
||||
# User, Photo and CreditCard factories:
|
||||
credit_cards_factory = providers.Factory(CreditCard)
|
||||
photos_factory = providers.Factory(Photo)
|
||||
users_factory = providers.Factory(User,
|
||||
injections.Method('set_main_photo',
|
||||
photos_factory),
|
||||
injections.Method('set_credit_card',
|
||||
credit_cards_factory))
|
||||
|
||||
# Creating several User objects:
|
||||
user1 = users_factory()
|
||||
# Same as: user1 = User()
|
||||
# user1.set_main_photo(Photo())
|
||||
# user1.set_credit_card(CreditCard())
|
||||
user2 = users_factory()
|
||||
# Same as: user2 = User()
|
||||
# user2.set_main_photo(Photo())
|
||||
# user2.set_credit_card(CreditCard())
|
||||
|
||||
# Making some asserts:
|
||||
assert user1 is not user2
|
||||
|
||||
assert isinstance(user1.main_photo, Photo)
|
||||
assert isinstance(user1.credit_card, CreditCard)
|
||||
|
||||
assert isinstance(user2.main_photo, Photo)
|
||||
assert isinstance(user2.credit_card, CreditCard)
|
||||
|
||||
assert user1.main_photo is not user2.main_photo
|
||||
assert user1.credit_card is not user2.credit_card
|
|
@ -1,7 +1,7 @@
|
|||
"""`Factory` specialization for limitation to provided type example."""
|
||||
|
||||
from dependency_injector import providers
|
||||
from dependency_injector import errors
|
||||
import dependency_injector.providers as providers
|
||||
import dependency_injector.errors as errors
|
||||
|
||||
|
||||
class BaseService(object):
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
"""Simple providers overriding example."""
|
||||
|
||||
from dependency_injector import providers
|
||||
import dependency_injector.providers as providers
|
||||
|
||||
|
||||
class User(object):
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
"""Overriding user's model example."""
|
||||
|
||||
from dependency_injector import providers
|
||||
import dependency_injector.providers as providers
|
||||
|
||||
|
||||
class User(object):
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
"""`Singleton` providers example."""
|
||||
|
||||
from dependency_injector import providers
|
||||
import dependency_injector.providers as providers
|
||||
|
||||
|
||||
class UserService(object):
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
"""`Singleton` providers delegation example."""
|
||||
|
||||
from dependency_injector import providers
|
||||
import dependency_injector.providers as providers
|
||||
|
||||
|
||||
# Some singleton provider and few delegates of it:
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
"""`Singleton` specialization for limitation to provided type example."""
|
||||
|
||||
from dependency_injector import providers
|
||||
from dependency_injector import errors
|
||||
import dependency_injector.providers as providers
|
||||
import dependency_injector.errors as errors
|
||||
|
||||
|
||||
class BaseService(object):
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
"""`Singleton` providers resetting example."""
|
||||
|
||||
from dependency_injector import providers
|
||||
import dependency_injector.providers as providers
|
||||
|
||||
|
||||
class UserService(object):
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
"""Static providers example."""
|
||||
|
||||
from dependency_injector import providers
|
||||
import dependency_injector.providers as providers
|
||||
|
||||
|
||||
# Provides class - `object`:
|
||||
|
|
Loading…
Reference in New Issue
Block a user