Preparation¶
Before coming to the tutorial, please do the following:
Have Python 3.7 installed in your laptop. Note: It’s important that you’re running Python 3.7 (or above) because we will use some of the new features added in 3.7 in this tutorial.
Install the dependencies, preferably in a virtual environment. For this tutorial, we’ll use
venv
.
Create a new virtual environment using venv:
python3.7 -m venv .venv
Activate the virtual environment. On Unix, Mac OS:
source .venv/bin/activate
On Windows:
.venv\Scripts\activate.bat
Install the dependencies, listed in the requirements.txt file. You can download the file, or clone this repository:
(.venv) python -m pip install -U pip -r requirements.txt
Verify that you correctly have Python 3.7 installed. If you’re able to run the following code, then you’re good to go.
import asyncio
async def main() -> None:
print("Hello ...")
await asyncio.sleep(1)
print("... World!")
# Python 3.7+
asyncio.run(main())
Resources and documentation links¶
Tools and documentations that we’ll use throughout this tutorial.
venv¶
Python venv tutorial documentation.
aiohttp¶
Installation:
python3.7 -m pip install aiohttp
.aiohttp source code
Owner: Andrew Svetlov
f-strings¶
We will use some f-strings during this tutorial.
My talk about f-strings.
Example:
first_name = "bart"
last_name = "simpson"
# old style %-formatting
print("Hello %s %s" % (first_name, last_name))
# str.format
print("Hello {first_name} {last_name}".format(first_name=first_name, last_name=last_name))
# f-string
print(f"Hello {first_name} {last_name}")
asyncio¶
aiohttp is async Python library. Read up the quick intro to asyncio.
type annotations¶
Our code examples use type annotations and checked with mypy
. It is optional.
typing documentation: https://docs.python.org/3/library/typing.html
mypy documentation: https://mypy.readthedocs.io/en/stable/
dataclass¶
The dataclass
module provides a decorator and functions
for automatically adding generated special methods such as __init__()
and __repr__()
to user-defined classes. It was originally described in PEP 557.
Example:
@dataclass
class InventoryItem:
'''Class for keeping track of an item in inventory.'''
name: str
unit_price: float
quantity_on_hand: int = 0
def total_cost(self) -> float:
return self.unit_price * self.quantity_on_hand
aiohttp-jinja2¶
jinja2 template renderer for aiohttp.web.
aiohttp-session¶
click¶
Click is a Python package for creating beautiful command line interfaces in a composable way with as little code as necessary. It’s the “Command Line Interface Creation Kit”.