Skip to content

Commit 5b85dc9

Browse files
committed
Separate tutorials into separate functions, add logging.
1 parent 732b303 commit 5b85dc9

5 files changed

Lines changed: 28 additions & 7 deletions

File tree

asyncio_tutorial/__init__.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,25 @@ def init_script():
1313
Run source code found in Hackersandslackers asyncio tutorials.
1414
1515
1. Intro to Async Python: https://hackersandslackers.com/python-concurrency-asyncio/
16-
2. Intro to Async Python: https://hackersandslackers.com/async-requests-with-aiohttp/
16+
2. Async HTTP Requests with Aiohttp & Aiofiles: https://hackersandslackers.com/async-requests-with-aiohttp/
1717
"""
18+
tutorial_part_one()
19+
time.sleep(3)
20+
tutorial_part_two()
21+
22+
23+
def tutorial_part_one():
24+
"""Run `Intro to Async Python` script."""
1825
start_time = time.perf_counter()
1926
asyncio.run(asyncio_intro_tutorial(start_time))
27+
LOGGER.warning(
28+
f"Tutorial Part I completed. Starting part II in 3 seconds... \
29+
\n--------------------------------------"
30+
)
31+
32+
33+
def tutorial_part_two():
34+
"""Run `Async HTTP Requests with Aiohttp & Aiofiles` script."""
35+
start_time = time.perf_counter()
2036
asyncio.run(aiohttp_aiofiles_tutorial(start_time))
37+
LOGGER.warning(f"Tutorial Part II completed. Thanks for playing!")

asyncio_tutorial/logger.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@ def formatter(log: dict) -> str:
1212
1313
:returns: str
1414
"""
15+
if log["level"].name == "INFO":
16+
return (
17+
"<fg #aad1f7>{time:HH:mm:ss A}</fg #aad1f7> | "
18+
"<fg #cfe2f3>{level}</fg #cfe2f3>: "
19+
"<light-white>{message}</light-white> \n"
20+
)
1521
if log["level"].name == "SUCCESS":
1622
return (
1723
"<fg #aad1f7>{time:HH:mm:ss A}</fg #aad1f7> | "
18-
"<light-green>{level}</light-green>: "
24+
"<fg #85de83>{level}</fg #85de83>: "
1925
"<light-white>{message}</light-white> \n"
2026
)
2127
if log["level"].name == "WARNING":

asyncio_tutorial/part_II_aiohttp_aiofiles/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from .tasks import create_tasks
1414

1515

16-
async def aiohttp_aiofiles_tutorial(start_time):
16+
async def aiohttp_aiofiles_tutorial(start_time: float):
1717
"""
1818
Open async HTTP session & execute created tasks.
1919

asyncio_tutorial/part_II_aiohttp_aiofiles/futures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ def loop_completed(result: str):
2323
2424
:param str result: Result of the loop & its execution time.
2525
"""
26-
LOGGER.success(result)
26+
LOGGER.success(f"{result}\n--------------------------------------")

asyncio_tutorial/part_I_asyncio_intro/futures.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,4 @@ def loop_completed(result: str):
2323
2424
:param str result: Result of the loop & its execution time.
2525
"""
26-
LOGGER.success(
27-
f"{result}\n--------------------------------------"
28-
)
26+
LOGGER.success(f"{result}\n--------------------------------------")

0 commit comments

Comments
 (0)