Skip to content

Commit 3f0b2d5

Browse files
committed
Merge branch 'development' of github.com:gjbex/Python-for-systems-programming into development
2 parents 0a2b017 + d3298eb commit 3f0b2d5

8 files changed

+373
-240
lines changed

environment.yml

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: python_for_systems_programming
22
channels:
3-
- defaults
3+
- conda-forge
44
dependencies:
55
- paramiko
66
- matplotlib
@@ -10,6 +10,10 @@ dependencies:
1010
- fabric
1111
- numpy
1212
- jupyterlab
13-
pip:
14-
- schedule
15-
prefix: /home/gjb/miniconda3/envs/python_for_systems_programming
13+
- uvicorn
14+
- ca-certificates
15+
- certifi
16+
- openssl
17+
- fastapi
18+
- fire
19+
prefix: /home/gjb/mambaforge/envs/python_for_systems_programming

python_for_systems_programming_linux64_conda_specs.txt

+231-124
Large diffs are not rendered by default.

source-code/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ to create it. There is some material not covered in the presentation as well.
4444
1. `subprocess`: illustrates executing a shell command from a Python script
4545
using the `subprocess` module.
4646
1. `xml-generator`: code to generate a random XML documents.
47+
1. `fastapi`: simple illustrations of using FastAPI for webservices.

source-code/fastapi/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# FastAPI
2+
3+
FastAPI is a nice framework to build web applications and handle http requests.
4+
5+
## What is it?
6+
7+
* `main.py`: very simple hello world application.
8+
* `calculator.py`: implementation of an API that allows to
9+
* push numbers on a stack (push/<number>)
10+
* perform a computation (compute/add or compute/mult)
11+
* `calculations.sh`: shell script to illustrate use of the calculator API.
12+
13+
## How to use it?
14+
15+
### Hello world application
16+
```bash
17+
$ uvicorn main:app --reload
18+
```
19+
20+
### Calculator
21+
```bash
22+
$ uvicorn calculatorapp --reload
23+
$ curl --silent http://127.0.0.1:8000/push/5
24+
$ curl --silent http://127.0.0.1:8000/push/7
25+
$ curl --silent http://127.0.0.1:8000/compute/add
26+
```

source-code/fastapi/calculations.sh

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env bash
2+
3+
# start the server with
4+
# uvivorn calculator::app --reload
5+
#
6+
7+
source ~/.mamba_init.sh
8+
mamba activate python_for_systems_programming
9+
10+
HOST=127.0.0.1:8000
11+
12+
echo "push 5"
13+
curl --silent "http://$HOST/push/5"
14+
echo ""
15+
16+
echo "push 7"
17+
curl --silent "http://$HOST/push/7"
18+
echo ""
19+
20+
echo "perform addition"
21+
curl --silent "http://$HOST/compute/add" | json_pp
22+
echo ""

source-code/fastapi/calculator.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from fastapi import FastAPI
2+
from typing import Union
3+
4+
app = FastAPI()
5+
stack = []
6+
7+
@app.get('/push/{value}')
8+
def push(value: float, q: Union[str, None] = None):
9+
stack.append(value)
10+
11+
@app.get('/compute/{operator}')
12+
def compute(operator: str, q: Union[str, None] = None):
13+
right_operand = stack.pop()
14+
left_operand = stack.pop()
15+
if operator == 'add':
16+
result = left_operand + right_operand
17+
elif operator == 'mult':
18+
result = left_operand*right_operand
19+
return {'result': result}

source-code/fastapi/main.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import Union
2+
from fastapi import FastAPI
3+
4+
app = FastAPI()
5+
6+
7+
@app.get("/")
8+
def read_root():
9+
return {"Hello": "World"}
10+
11+
12+
@app.get("/items/{item_id}")
13+
def read_item(item_id: int, q: Union[str, None] = None):
14+
return {"item_id": item_id, "q": q}
15+
16+
17+
@app.get("/users/{user_id}&{category}")
18+
def read_item(user_id: int, category: str,
19+
q: Union[str, None] = None):
20+
return {"user_id": user_id, "category": category, "q": q}

0 commit comments

Comments
 (0)