Skip to content

Instantly share code, notes, and snippets.

@lastlegion
Created September 1, 2020 00:03
Show Gist options
  • Save lastlegion/a80fdf6f6e388d64ad612d1a243e2c00 to your computer and use it in GitHub Desktop.
Save lastlegion/a80fdf6f6e388d64ad612d1a243e2c00 to your computer and use it in GitHub Desktop.
import pinject
import os
class Repository:
def __init__(self, db: str):
self.db = db
def get_data(self):
return 'Hello World'
class RepositoryBindingSpec(pinject.BindingSpec):
def provide_repository(self):
return Repository(db=os.getenv('dbname'))
class Service:
def __init__(self, repository: Repository):
self.repository = repository
def process(self):
return self.repository.get_data()
class UI:
def __init__(self, service: Service):
self.service = service
def render(self):
print(self.service.process())
if __name__ == '__main__':
object_graph = pinject.new_object_graph(binding_specs=[RepositoryBindingSpec()])
ui = object_graph.provide(UI)
ui.render()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment