Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- def _dependency_injection():
- from abc import ABC, abstractmethod
- class DataBaseAbstract(ABC):
- @abstractmethod
- def connect(self):
- pass
- class SQLite(DataBaseAbstract):
- def connect(self):
- print('SQLite: connection with sqlite is completed')
- class MySQL(DataBaseAbstract):
- def connect(self):
- print('MySQL: connection with sqlite is completed')
- class DataBase:
- def __init__(self, db: DataBaseAbstract):
- self.db: DataBaseAbstract = db
- def get_column(self):
- self.db.connect()
- db_sqlite: DataBase = DataBase(SQLite())
- db_sqlite.get_column()
- db_mysql: DataBase = DataBase(MySQL())
- db_mysql.get_column()
- def _dependency_injector():
- from dependency_injector import containers, providers
- class Sensor:
- def __init__(self, ip, id):
- self.ip = ip
- self.id = id
- class Server:
- def __init__(self, clients: [Sensor]):
- self.clients: [Sensor] = clients
- class Container(containers.DeclarativeContainer):
- config = providers.Configuration()
- server = providers.Singleton(
- Server,
- clients=config.clients
- )
- sensor = providers.Factory(
- Sensor,
- ip=config.ip,
- id=config.id
- )
- container = Container()
- server = container.server()
- server.clients = [Sensor('127.0.0.1', random.randint(1, 100)) for _ in range(10)]
- print(server.clients)
- print(server.clients[0].ip)
- if __name__ == '__main__':
- _dependency_injection()
- _dependency_injector()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement