Skip to content

Commit

Permalink
Update CLI application (movie lister) tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
rmk135 committed Sep 29, 2021
1 parent 2d006d5 commit d3d2e70
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 54 deletions.
22 changes: 8 additions & 14 deletions docs/tutorials/cli.rst
Expand Up @@ -575,13 +575,11 @@ Let's inject the ``lister`` into the ``main()`` function.
Edit ``__main__.py``:

.. code-block:: python
:emphasize-lines: 3-7,11-12,19
:emphasize-lines: 3-5,9-10,17
"""Main module."""
import sys
from dependency_injector.wiring import inject, Provide
from dependency_injector.wiring import Provide, inject
from .listers import MovieLister
from .containers import Container
Expand All @@ -595,7 +593,7 @@ Edit ``__main__.py``:
if __name__ == "__main__":
container = Container()
container.config.from_yaml("config.yml")
container.wire(modules=[sys.modules[__name__]])
container.wire(modules=[__name__])
main()
Expand All @@ -607,13 +605,11 @@ Francis Lawrence and movies released in 2016.
Edit ``__main__.py``:

.. code-block:: python
:emphasize-lines: 13-19
:emphasize-lines: 11-17
"""Main module."""
import sys
from dependency_injector.wiring import inject, Provide
from dependency_injector.wiring import Provide, inject
from .listers import MovieLister
from .containers import Container
Expand All @@ -633,7 +629,7 @@ Edit ``__main__.py``:
if __name__ == "__main__":
container = Container()
container.config.from_yaml("config.yml")
container.wire(modules=[sys.modules[__name__]])
container.wire(modules=[__name__])
main()
Expand Down Expand Up @@ -863,13 +859,11 @@ Now we need to read the value of the ``config.finder.type`` option from the envi
Edit ``__main__.py``:

.. code-block:: python
:emphasize-lines: 25
:emphasize-lines: 23
"""Main module."""
import sys
from dependency_injector.wiring import inject, Provide
from dependency_injector.wiring import Provide, inject
from .listers import MovieLister
from .containers import Container
Expand Down
24 changes: 12 additions & 12 deletions examples/miniapps/movie-lister/data/fixtures.py
Expand Up @@ -6,19 +6,19 @@


SAMPLE_DATA = [
('The Hunger Games: Mockingjay - Part 2', 2015, 'Francis Lawrence'),
('Rogue One: A Star Wars Story', 2016, 'Gareth Edwards'),
('The Jungle Book', 2016, 'Jon Favreau'),
("The Hunger Games: Mockingjay - Part 2", 2015, "Francis Lawrence"),
("Rogue One: A Star Wars Story", 2016, "Gareth Edwards"),
("The Jungle Book", 2016, "Jon Favreau"),
]

FILE = pathlib.Path(__file__)
DIR = FILE.parent
CSV_FILE = DIR / 'movies.csv'
SQLITE_FILE = DIR / 'movies.db'
CSV_FILE = DIR / "movies.csv"
SQLITE_FILE = DIR / "movies.db"


def create_csv(movies_data, path):
with open(path, 'w') as opened_file:
with open(path, "w") as opened_file:
writer = csv.writer(opened_file)
for row in movies_data:
writer.writerow(row)
Expand All @@ -27,18 +27,18 @@ def create_csv(movies_data, path):
def create_sqlite(movies_data, path):
with sqlite3.connect(path) as db:
db.execute(
'CREATE TABLE IF NOT EXISTS movies '
'(title text, year int, director text)'
"CREATE TABLE IF NOT EXISTS movies "
"(title text, year int, director text)"
)
db.execute('DELETE FROM movies')
db.executemany('INSERT INTO movies VALUES (?,?,?)', movies_data)
db.execute("DELETE FROM movies")
db.executemany("INSERT INTO movies VALUES (?,?,?)", movies_data)


def main():
create_csv(SAMPLE_DATA, CSV_FILE)
create_sqlite(SAMPLE_DATA, SQLITE_FILE)
print('OK')
print("OK")


if __name__ == '__main__':
if __name__ == "__main__":
main()
22 changes: 10 additions & 12 deletions examples/miniapps/movie-lister/movies/__main__.py
@@ -1,28 +1,26 @@
"""Main module."""

import sys

from dependency_injector.wiring import inject, Provide
from dependency_injector.wiring import Provide, inject

from .listers import MovieLister
from .containers import Container


@inject
def main(lister: MovieLister = Provide[Container.lister]) -> None:
print('Francis Lawrence movies:')
for movie in lister.movies_directed_by('Francis Lawrence'):
print('\t-', movie)
print("Francis Lawrence movies:")
for movie in lister.movies_directed_by("Francis Lawrence"):
print("\t-", movie)

print('2016 movies:')
print("2016 movies:")
for movie in lister.movies_released_in(2016):
print('\t-', movie)
print("\t-", movie)


if __name__ == '__main__':
if __name__ == "__main__":
container = Container()
container.config.from_yaml('config.yml')
container.config.finder.type.from_env('MOVIE_FINDER_TYPE')
container.wire(modules=[sys.modules[__name__]])
container.config.from_yaml("config.yml")
container.config.finder.type.from_env("MOVIE_FINDER_TYPE")
container.wire(modules=[__name__])

main()
2 changes: 1 addition & 1 deletion examples/miniapps/movie-lister/movies/entities.py
Expand Up @@ -9,7 +9,7 @@ def __init__(self, title: str, year: int, director: str):
self.director = str(director)

def __repr__(self):
return '{0}(title={1}, year={2}, director={3})'.format(
return "{0}(title={1}, year={2}, director={3})".format(
self.__class__.__name__,
repr(self.title),
repr(self.year),
Expand Down
2 changes: 1 addition & 1 deletion examples/miniapps/movie-lister/movies/finders.py
Expand Up @@ -46,5 +46,5 @@ def __init__(

def find_all(self) -> List[Movie]:
with self._database as db:
rows = db.execute('SELECT title, year, director FROM movies')
rows = db.execute("SELECT title, year, director FROM movies")
return [self._movie_factory(*row) for row in rows]
28 changes: 14 additions & 14 deletions examples/miniapps/movie-lister/movies/tests.py
Expand Up @@ -11,14 +11,14 @@
def container():
container = Container()
container.config.from_dict({
'finder': {
'type': 'csv',
'csv': {
'path': '/fake-movies.csv',
'delimiter': ',',
"finder": {
"type": "csv",
"csv": {
"path": "/fake-movies.csv",
"delimiter": ",",
},
'sqlite': {
'path': '/fake-movies.db',
"sqlite": {
"path": "/fake-movies.db",
},
},
})
Expand All @@ -28,28 +28,28 @@ def container():
def test_movies_directed_by(container):
finder_mock = mock.Mock()
finder_mock.find_all.return_value = [
container.movie('The 33', 2015, 'Patricia Riggen'),
container.movie('The Jungle Book', 2016, 'Jon Favreau'),
container.movie("The 33", 2015, "Patricia Riggen"),
container.movie("The Jungle Book", 2016, "Jon Favreau"),
]

with container.finder.override(finder_mock):
lister = container.lister()
movies = lister.movies_directed_by('Jon Favreau')
movies = lister.movies_directed_by("Jon Favreau")

assert len(movies) == 1
assert movies[0].title == 'The Jungle Book'
assert movies[0].title == "The Jungle Book"


def test_movies_released_in(container):
finder_mock = mock.Mock()
finder_mock.find_all.return_value = [
container.movie('The 33', 2015, 'Patricia Riggen'),
container.movie('The Jungle Book', 2016, 'Jon Favreau'),
container.movie("The 33", 2015, "Patricia Riggen"),
container.movie("The Jungle Book", 2016, "Jon Favreau"),
]

with container.finder.override(finder_mock):
lister = container.lister()
movies = lister.movies_released_in(2015)

assert len(movies) == 1
assert movies[0].title == 'The 33'
assert movies[0].title == "The 33"

0 comments on commit d3d2e70

Please sign in to comment.