From 4e66c47f59ac8965a484f24307c8af8ee6542257 Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Thu, 22 Sep 2016 14:40:45 +0300 Subject: [PATCH] Update movie lister example --- examples/miniapps/movie_lister/README.rst | 2 - examples/miniapps/movie_lister/app_csv.py | 38 ++++++------ examples/miniapps/movie_lister/app_db.py | 39 ++++++------ examples/miniapps/movie_lister/app_db_csv.py | 59 +++++++++++-------- examples/miniapps/movie_lister/create_db.py | 33 ----------- .../miniapps/movie_lister/data/.gitignore | 5 ++ .../miniapps/movie_lister/example/__init__.py | 1 + examples/miniapps/movie_lister/example/db.py | 35 +++++++++++ .../miniapps/movie_lister/example/main.py | 17 ++++++ .../miniapps/movie_lister/movies/__init__.py | 6 +- .../miniapps/movie_lister/movies/finders.py | 20 +++---- examples/miniapps/movie_lister/settings.py | 9 ++- 12 files changed, 146 insertions(+), 118 deletions(-) delete mode 100644 examples/miniapps/movie_lister/create_db.py create mode 100644 examples/miniapps/movie_lister/data/.gitignore create mode 100644 examples/miniapps/movie_lister/example/__init__.py create mode 100644 examples/miniapps/movie_lister/example/db.py create mode 100644 examples/miniapps/movie_lister/example/main.py diff --git a/examples/miniapps/movie_lister/README.rst b/examples/miniapps/movie_lister/README.rst index e81aa815..616b9106 100644 --- a/examples/miniapps/movie_lister/README.rst +++ b/examples/miniapps/movie_lister/README.rst @@ -11,8 +11,6 @@ Instructions for running: .. code-block:: bash - python create_db.py - python app_csv.py python app_db.py python app_db_csv.py diff --git a/examples/miniapps/movie_lister/app_csv.py b/examples/miniapps/movie_lister/app_csv.py index e54f27ad..75dfef42 100644 --- a/examples/miniapps/movie_lister/app_csv.py +++ b/examples/miniapps/movie_lister/app_csv.py @@ -9,42 +9,40 @@ This mini application uses ``movies`` library, that is configured to work with csv file movies database. """ -import dependency_injector.containers as containers -import dependency_injector.providers as providers -import dependency_injector.injections as injections - import movies import movies.finders +import example.main +import example.db + import settings +import dependency_injector.containers as containers +import dependency_injector.providers as providers + @containers.override(movies.MoviesModule) class MyMoviesModule(containers.DeclarativeContainer): """IoC container for overriding movies module component providers.""" movie_finder = providers.Factory(movies.finders.CsvMovieFinder, - csv_file=settings.MOVIES_CSV_PATH, - delimeter=',', + csv_file_path=settings.MOVIES_CSV_PATH, + delimiter=',', **movies.MoviesModule.movie_finder.kwargs) -@injections.inject(movies.MoviesModule.movie_lister) -def main(movie_lister): - """Main function. +class CsvApplication(containers.DeclarativeContainer): + """IoC container of csv application component providers.""" - This program prints info about all movies that were directed by different - persons and then prints all movies that were released in 2015. + main = providers.Callable(example.main.main, + movie_lister=movies.MoviesModule.movie_lister) - :param movie_lister: Movie lister instance - :type movie_lister: movies.listers.MovieLister - """ - print movie_lister.movies_directed_by('Francis Lawrence') - print movie_lister.movies_directed_by('Patricia Riggen') - print movie_lister.movies_directed_by('JJ Abrams') - - print movie_lister.movies_released_in(2015) + init_db = providers.Callable(example.db.init_csv, + movies_data=settings.MOVIES_SAMPLE_DATA, + csv_file_path=settings.MOVIES_CSV_PATH, + delimiter=',') if __name__ == '__main__': - main() + CsvApplication.init_db() + CsvApplication.main() diff --git a/examples/miniapps/movie_lister/app_db.py b/examples/miniapps/movie_lister/app_db.py index 9c762fe8..8f15e524 100644 --- a/examples/miniapps/movie_lister/app_db.py +++ b/examples/miniapps/movie_lister/app_db.py @@ -11,18 +11,20 @@ sqlite movies database. import sqlite3 -import dependency_injector.containers as containers -import dependency_injector.providers as providers -import dependency_injector.injections as injections - import movies import movies.finders +import example.main +import example.db + import settings +import dependency_injector.containers as containers +import dependency_injector.providers as providers -class ApplicationModule(containers.DeclarativeContainer): - """IoC container of application component providers.""" + +class ResourcesModule(containers.DeclarativeContainer): + """IoC container of application resource providers.""" database = providers.Singleton(sqlite3.connect, settings.MOVIES_DB_PATH) @@ -32,26 +34,21 @@ class MyMoviesModule(containers.DeclarativeContainer): """IoC container for overriding movies module component providers.""" movie_finder = providers.Factory(movies.finders.SqliteMovieFinder, - database=ApplicationModule.database, + database=ResourcesModule.database, **movies.MoviesModule.movie_finder.kwargs) -@injections.inject(movies.MoviesModule.movie_lister) -def main(movie_lister): - """Main function. +class DbApplication(containers.DeclarativeContainer): + """IoC container of database application component providers.""" - This program prints info about all movies that were directed by different - persons and then prints all movies that were released in 2015. + main = providers.Callable(example.main.main, + movie_lister=movies.MoviesModule.movie_lister) - :param movie_lister: Movie lister instance - :type movie_lister: movies.listers.MovieLister - """ - print movie_lister.movies_directed_by('Francis Lawrence') - print movie_lister.movies_directed_by('Patricia Riggen') - print movie_lister.movies_directed_by('JJ Abrams') - - print movie_lister.movies_released_in(2015) + init_db = providers.Callable(example.db.init_sqlite, + movies_data=settings.MOVIES_SAMPLE_DATA, + database=ResourcesModule.database) if __name__ == '__main__': - main() + DbApplication.init_db() + DbApplication.main() diff --git a/examples/miniapps/movie_lister/app_db_csv.py b/examples/miniapps/movie_lister/app_db_csv.py index a8b40eed..b7461430 100644 --- a/examples/miniapps/movie_lister/app_db_csv.py +++ b/examples/miniapps/movie_lister/app_db_csv.py @@ -11,18 +11,20 @@ sqlite movies database and csv file movies database. import sqlite3 -import dependency_injector.containers as containers -import dependency_injector.providers as providers -import dependency_injector.injections as injections - import movies import movies.finders +import example.main +import example.db + import settings +import dependency_injector.containers as containers +import dependency_injector.providers as providers -class ApplicationModule(containers.DeclarativeContainer): - """IoC container of application component providers.""" + +class ResourcesModule(containers.DeclarativeContainer): + """IoC container of application resource providers.""" database = providers.Singleton(sqlite3.connect, settings.MOVIES_DB_PATH) @@ -32,7 +34,7 @@ class DbMoviesModule(movies.MoviesModule): """IoC container for overriding movies module component providers.""" movie_finder = providers.Factory(movies.finders.SqliteMovieFinder, - database=ApplicationModule.database, + database=ResourcesModule.database, **movies.MoviesModule.movie_finder.kwargs) @@ -41,31 +43,36 @@ class CsvMoviesModule(movies.MoviesModule): """IoC container for overriding movies module component providers.""" movie_finder = providers.Factory(movies.finders.CsvMovieFinder, - csv_file=settings.MOVIES_CSV_PATH, - delimeter=',', + csv_file_path=settings.MOVIES_CSV_PATH, + delimiter=',', **movies.MoviesModule.movie_finder.kwargs) -@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. +class DbApplication(containers.DeclarativeContainer): + """IoC container of database application component providers.""" - This program prints info about all movies that were directed by different - persons and then prints all movies that were released in 2015. + main = providers.Callable(example.main.main, + movie_lister=DbMoviesModule.movie_lister) - :param db_movie_lister: Movie lister, configured to work with database - :type db_movie_lister: movies.listers.MovieLister + init_db = providers.Callable(example.db.init_sqlite, + movies_data=settings.MOVIES_SAMPLE_DATA, + database=ResourcesModule.database) - :param csv_movie_lister: Movie lister, configured to work with csv file - :type csv_movie_lister: movies.listers.MovieLister - """ - for movie_lister in (db_movie_lister, csv_movie_lister): - print movie_lister.movies_directed_by('Francis Lawrence') - print movie_lister.movies_directed_by('Patricia Riggen') - print movie_lister.movies_directed_by('JJ Abrams') - print movie_lister.movies_released_in(2015) +class CsvApplication(containers.DeclarativeContainer): + """IoC container of csv application component providers.""" + + main = providers.Callable(example.main.main, + movie_lister=CsvMoviesModule.movie_lister) + + init_db = providers.Callable(example.db.init_csv, + movies_data=settings.MOVIES_SAMPLE_DATA, + csv_file_path=settings.MOVIES_CSV_PATH, + delimiter=',') if __name__ == '__main__': - main() + DbApplication.init_db() + DbApplication.main() + + CsvApplication.init_db() + CsvApplication.main() diff --git a/examples/miniapps/movie_lister/create_db.py b/examples/miniapps/movie_lister/create_db.py deleted file mode 100644 index 5482afdc..00000000 --- a/examples/miniapps/movie_lister/create_db.py +++ /dev/null @@ -1,33 +0,0 @@ -"""Script for initializing movie databases.""" - -import os -import csv -import sqlite3 -import shutil - -from settings import DATA_DIR -from settings import MOVIES_CSV_PATH -from settings import MOVIES_DB_PATH - - -MOVIES = (('The Hunger Games: Mockingjay - Part 2', 2015, 'Francis Lawrence'), - ('The 33', 2015, 'Patricia Riggen'), - ('Star Wars: Episode VII - The Force Awakens', 2015, 'JJ Abrams')) - - -if __name__ == '__main__': - # (Re)create data directory: - if os.path.exists(DATA_DIR): - shutil.rmtree(DATA_DIR) - os.makedirs(DATA_DIR) - - # Initialize sqlite database: - connection = sqlite3.connect(MOVIES_DB_PATH) - with connection: - connection.execute('CREATE TABLE movies ' - '(name text, year int, director text)') - connection.executemany('INSERT INTO movies VALUES (?,?,?)', MOVIES) - - # Initialize csv database: - with open(MOVIES_CSV_PATH, 'w') as csv_file: - csv.writer(csv_file).writerows(MOVIES) diff --git a/examples/miniapps/movie_lister/data/.gitignore b/examples/miniapps/movie_lister/data/.gitignore new file mode 100644 index 00000000..bc208b8f --- /dev/null +++ b/examples/miniapps/movie_lister/data/.gitignore @@ -0,0 +1,5 @@ +# Ignore everything in this directory +* + +# Except this file: +!.gitignore diff --git a/examples/miniapps/movie_lister/example/__init__.py b/examples/miniapps/movie_lister/example/__init__.py new file mode 100644 index 00000000..bfa99aa2 --- /dev/null +++ b/examples/miniapps/movie_lister/example/__init__.py @@ -0,0 +1 @@ +"""Example top-level package.""" diff --git a/examples/miniapps/movie_lister/example/db.py b/examples/miniapps/movie_lister/example/db.py new file mode 100644 index 00000000..d519fb03 --- /dev/null +++ b/examples/miniapps/movie_lister/example/db.py @@ -0,0 +1,35 @@ +"""Example database module.""" + +import csv + + +def init_sqlite(movies_data, database): + """Initialize sqlite3 movies database. + + :param movies_data: Data about movies + :type movies_data: tuple[tuple] + + :param database: Connection to sqlite database with movies data + :type database: sqlite3.Connection + """ + with database: + database.execute('CREATE TABLE IF NOT EXISTS movies ' + '(name text, year int, director text)') + database.execute('DELETE FROM movies') + database.executemany('INSERT INTO movies VALUES (?,?,?)', movies_data) + + +def init_csv(movies_data, csv_file_path, delimiter): + """Initialize csv movies database. + + :param movies_data: Data about movies + :type movies_data: tuple[tuple] + + :param csv_file_path: Path to csv file with movies data + :type csv_file_path: str + + :param delimiter: Csv file's delimiter + :type delimiter: str + """ + with open(csv_file_path, 'w') as csv_file: + csv.writer(csv_file, delimiter=delimiter).writerows(movies_data) diff --git a/examples/miniapps/movie_lister/example/main.py b/examples/miniapps/movie_lister/example/main.py new file mode 100644 index 00000000..26a51f63 --- /dev/null +++ b/examples/miniapps/movie_lister/example/main.py @@ -0,0 +1,17 @@ +"""Example main module.""" + + +def main(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 movie_lister: Movie lister instance + :type movie_lister: movies.listers.MovieLister + """ + print(movie_lister.movies_directed_by('Francis Lawrence')) + print(movie_lister.movies_directed_by('Patricia Riggen')) + print(movie_lister.movies_directed_by('JJ Abrams')) + + print(movie_lister.movies_released_in(2015)) diff --git a/examples/miniapps/movie_lister/movies/__init__.py b/examples/miniapps/movie_lister/movies/__init__.py index 2f6bd185..45930056 100644 --- a/examples/miniapps/movie_lister/movies/__init__.py +++ b/examples/miniapps/movie_lister/movies/__init__.py @@ -12,13 +12,13 @@ concrete finder implementation in terms of library configuration. Each of ``MoviesModule`` providers could be overridden. """ -import dependency_injector.containers as containers -import dependency_injector.providers as providers - import movies.finders import movies.listers import movies.models +import dependency_injector.containers as containers +import dependency_injector.providers as providers + class MoviesModule(containers.DeclarativeContainer): """IoC container of movies module component providers.""" diff --git a/examples/miniapps/movie_lister/movies/finders.py b/examples/miniapps/movie_lister/movies/finders.py index 9d3cbc27..b9b8f42a 100644 --- a/examples/miniapps/movie_lister/movies/finders.py +++ b/examples/miniapps/movie_lister/movies/finders.py @@ -33,20 +33,20 @@ class MovieFinder(object): class CsvMovieFinder(MovieFinder): """Movie finder that fetches movies data from csv file.""" - def __init__(self, movie_model, csv_file, delimeter): + def __init__(self, movie_model, csv_file_path, delimiter): """Initializer. :param movie_model: Movie model's factory :type movie_model: movies.models.Movie - :param csv_file: Path to csv file with movies data - :type csv_file: str + :param csv_file_path: Path to csv file with movies data + :type csv_file_path: str - :param delimeter: Csv file's delimeter - :type delimeter: str + :param delimiter: Csv file's delimiter + :type delimiter: str """ - self._csv_file = csv_file - self._delimeter = delimeter + self._csv_file_path = csv_file_path + self._delimiter = delimiter super(CsvMovieFinder, self).__init__(movie_model) def find_all(self): @@ -55,9 +55,9 @@ class CsvMovieFinder(MovieFinder): :rtype: list[movies.models.Movie] :return: List of movie instances. """ - with open(self._csv_file) as csv_file: - reader = csv.reader(csv_file, delimiter=self._delimeter) - return [self._movie_model(*row) for row in reader] + with open(self._csv_file_path) as csv_file: + csv_reader = csv.reader(csv_file, delimiter=self._delimiter) + return [self._movie_model(*row) for row in csv_reader] class SqliteMovieFinder(MovieFinder): diff --git a/examples/miniapps/movie_lister/settings.py b/examples/miniapps/movie_lister/settings.py index f318b9e1..0a9916f8 100644 --- a/examples/miniapps/movie_lister/settings.py +++ b/examples/miniapps/movie_lister/settings.py @@ -7,8 +7,11 @@ import os DATA_DIR = os.path.abspath(os.path.dirname(__file__) + '/data') - MOVIES_CSV_PATH = DATA_DIR + '/movies.csv' -MOVIES_CSV_DELIMETER = ',' - MOVIES_DB_PATH = DATA_DIR + '/movies.db' + +MOVIES_SAMPLE_DATA = ( + ('The Hunger Games: Mockingjay - Part 2', 2015, 'Francis Lawrence'), + ('The 33', 2015, 'Patricia Riggen'), + ('Star Wars: Episode VII - The Force Awakens', 2015, 'JJ Abrams'), +)