File: example.py

package info (click to toggle)
picobox 4.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 248 kB
  • sloc: python: 1,666; makefile: 16
file content (31 lines) | stat: -rw-r--r-- 871 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import picobox


class Sender:
    def send(self, text):
        print(text)


class Controller:
    # Picobox supports injections by type (key may be any hashable object),
    # though in this case you have to explicitly map the key onto argument
    # name.
    @picobox.pass_(Sender, as_="sender")
    def __init__(self, sender):
        self._sender = sender

    # Many alternative solutions support injections to __init__ only while
    # Picobox allows to inject arguments wherever you want. You are the
    # only one to decide what would be the better way.
    @picobox.pass_("document")
    def process(self, document):
        self._sender.send("processing " + document)


box = picobox.Box()
box.put(Sender, factory=Sender, scope=picobox.singleton)
box.put("document", "cv.txt")

with picobox.push(box):
    controller = Controller()
    controller.process()