Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #174 from ets-labs/feature/factory-aggregate
Create FactoryAggregate provider
- Loading branch information
Showing
13 changed files
with
6,737 additions
and
4,232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
"""`FactoryAggregate` providers example.""" | ||
|
||
import sys | ||
|
||
import dependency_injector.providers as providers | ||
|
||
from games import Chess, Checkers, Ludo | ||
|
||
|
||
game_factory = providers.FactoryAggregate(chess=providers.Factory(Chess), | ||
checkers=providers.Factory(Checkers), | ||
ludo=providers.Factory(Ludo)) | ||
|
||
if __name__ == '__main__': | ||
game_type = sys.argv[1].lower() | ||
player1 = sys.argv[2].capitalize() | ||
player2 = sys.argv[3].capitalize() | ||
|
||
selected_game = game_factory(game_type, player1, player2) | ||
selected_game.play() | ||
|
||
# $ python example.py chess John Jane | ||
# John and Jane are playing chess | ||
# | ||
# $ python example.py checkers John Jane | ||
# John and Jane are playing checkers | ||
# | ||
# $ python example.py ludo John Jane | ||
# John and Jane are playing ludo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"""Example games module.""" | ||
|
||
|
||
class Game(object): | ||
"""Base game class.""" | ||
|
||
def __init__(self, player1, player2): | ||
"""Initializer.""" | ||
self.player1 = player1 | ||
self.player2 = player2 | ||
|
||
def play(self): | ||
"""Play game.""" | ||
print('{0} and {1} are playing {2}'.format( | ||
self.player1, self.player2, self.__class__.__name__.lower())) | ||
|
||
|
||
class Chess(Game): | ||
"""Chess game.""" | ||
|
||
|
||
class Checkers(Game): | ||
"""Checkers game.""" | ||
|
||
|
||
class Ludo(Game): | ||
"""Ludo game.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"""FactoryAggregate provider prototype.""" | ||
|
||
|
||
class FactoryAggregate(object): | ||
"""FactoryAggregate provider prototype.""" | ||
|
||
def __init__(self, **factories): | ||
"""Initializer.""" | ||
self.factories = factories | ||
|
||
def __call__(self, factory_name, *args, **kwargs): | ||
"""Create object.""" | ||
return self.factories[factory_name](*args, **kwargs) | ||
|
||
def __getattr__(self, factory_name): | ||
"""Return factory with specified name.""" | ||
return self.factories[factory_name] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
cython==0.27 | ||
cython==0.27.1 | ||
tox | ||
unittest2 | ||
coverage | ||
|
Oops, something went wrong.