Use Key in README example.

This commit is contained in:
Alec Thomas 2010-11-26 09:27:18 +11:00
parent bfef950c4f
commit f6ef41e2ea
1 changed files with 7 additions and 4 deletions

View File

@ -4,17 +4,20 @@ Injector - Python dependency injection framework, inspired by Guice
This framework is also similar to snake-guice, but aims for simplification. This framework is also similar to snake-guice, but aims for simplification.
While being inspired by Guice, it does not slavishly replicate its API. While being inspired by Guice, it does not slavishly replicate its API.
Providing a Pythonic API trumps faithful replication. Providing a Pythonic API trumps faithfulness.
An Example An Example
---------- ----------
*TODO: Write a more useful example.*
Here's a brief, completely contrived, example from the unit tests:: Here's a brief, completely contrived, example from the unit tests::
from injector import Injector, Module, Key, injects, provides from injector import Injector, Module, Key, injects, provides
Weight = Key('Weight') Weight = Key('Weight')
Age = Key('Age') Age = Key('Age')
Description = Key('Description')
class MyModule(Module): class MyModule(Module):
@provides(Weight) @provides(Weight)
@ -25,10 +28,10 @@ Here's a brief, completely contrived, example from the unit tests::
def provide_age(self): def provide_age(self):
return 25 return 25
# TODO(alec) Make provides/inject order independent. @provides(Description)
@provides(str)
@inject(age=Age, weight=Weight) @inject(age=Age, weight=Weight)
def provide_description(self, age, weight): def provide_description(self, age, weight):
return 'Bob is %d and weighs %0.1fkg' % (age, weight) return 'Bob is %d and weighs %0.1fkg' % (age, weight)
assert_equal(Injector(MyModule()).get(str), 'Bob is 25 and weighs 50.0kg') injector = Injector(MyModule())
assert_equal(injector.get(Description), 'Bob is 25 and weighs 50.0kg')