From f89ba4c5a9ada2ce84c9d46d6100fc5aec13f758 Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Sun, 10 Apr 2016 16:54:34 +0300 Subject: [PATCH] Add example of catalog copying into MovieLister example --- examples/stories/movie_lister/apps_db_csv.py | 76 ++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 examples/stories/movie_lister/apps_db_csv.py diff --git a/examples/stories/movie_lister/apps_db_csv.py b/examples/stories/movie_lister/apps_db_csv.py new file mode 100644 index 00000000..e6de7745 --- /dev/null +++ b/examples/stories/movie_lister/apps_db_csv.py @@ -0,0 +1,76 @@ +"""A naive example of dependency injection in Python. + +Example implementation of dependency injection in Python from Martin Fowler's +article about dependency injection and inversion of control: + +http://www.martinfowler.com/articles/injection.html + +This mini application uses ``movies`` library, that is configured to work with +csv file movies database. +""" + +import sqlite3 + +from dependency_injector import catalogs +from dependency_injector import providers +from dependency_injector import injections + +from movies import MoviesModule +from movies import finders + +from settings import MOVIES_CSV_PATH +from settings import MOVIES_DB_PATH + + +class ApplicationModule(catalogs.DeclarativeCatalog): + """Catalog of application component providers.""" + + database = providers.Singleton(sqlite3.connect, MOVIES_DB_PATH) + + +@catalogs.copy(MoviesModule) +class DbMoviesModule(MoviesModule): + """Customized catalog of movies module component providers.""" + + movie_finder = providers.Factory(finders.SqliteMovieFinder, + *MoviesModule.movie_finder.injections, + database=ApplicationModule.database) + + +@catalogs.copy(MoviesModule) +class CsvMoviesModule(MoviesModule): + """Customized catalog of movies module component providers.""" + + movie_finder = providers.Factory(finders.CsvMovieFinder, + *MoviesModule.movie_finder.injections, + csv_file=MOVIES_CSV_PATH, + delimeter=',') + + +@injections.inject(db_movie_lister=DbMoviesModule.movie_lister) +@injections.inject(csv_movie_lister=CsvMoviesModule.movie_lister) +def main(db_movie_lister, csv_movie_lister): + """Main function. + + This program prints info about all movies that were directed by different + persons and then prints all movies that were released in 2015. + + :param db_movie_lister: Database movie lister instance + :type db_movie_lister: movies.listers.MovieLister + + :param csv_movie_lister: Database movie lister instance + :type csv_movie_lister: movies.listers.MovieLister + """ + print db_movie_lister.movies_directed_by('Francis Lawrence') + print db_movie_lister.movies_directed_by('Patricia Riggen') + print db_movie_lister.movies_directed_by('JJ Abrams') + print db_movie_lister.movies_released_in(2015) + + print csv_movie_lister.movies_directed_by('Francis Lawrence') + print csv_movie_lister.movies_directed_by('Patricia Riggen') + print csv_movie_lister.movies_directed_by('JJ Abrams') + print csv_movie_lister.movies_released_in(2015) + + +if __name__ == '__main__': + main()