This folder contains the sample code for the tutorial Serialize Your Data With Python published on Real Python.
- Setup
- Usage
Create and activate a new virtual environment
Linux and MacOS:
python3 -m venv .venv/
source .venv/bin/activate
git-bash on Windows:
python3 -m venv .venv/
source .venv/Scripts/activate
Install the required third-party dependencies:
python3 -m pip install -r requirements.txt
cd python-objects/standard-python/
python3 pickle_demo.py
python3 marshal_demo.py
python3 shelve_demo.py
python3 dbm_demo.py
cd python-objects/customize-pickle/
python3 main.py
cd python-objects/json-encode/
python main.py
jsonpickle and PyYAML:
cd python-objects/foreign-formats/
python jsonpickle_demo.py
python pyyaml_demo.py
cd executable-code/pickle-importable/
python main.py
cd executable-code/code-objects/
python dill_demo.py
cd executable-code/digital-signature/
python main.py
Start the web server:
cd http-payload/flask-rest-api/
flask --app main --debug run
Navigate to the "users" resource in your web browser: http://127.0.0.1:5000/users
Send an HTTP GET request to retrieve all users:
curl -s http://127.0.0.1:5000/users | jq
[
{
"name": "Alice",
"id": "512a956f-165a-429f-9ec8-83d859843072",
"created_at": "2023-11-13T12:29:18.664574"
},
{
"name": "Bob",
"id": "fb52a80f-8982-46be-bcdd-605932d8ef03",
"created_at": "2023-11-13T12:29:18.664593"
}
]
Send an HTTP POST request to add a new user:
curl -s -X POST http://127.0.0.1:5000/users \
-H 'Content-Type: application/json' \
--data '{"name": "Frank"}' | jq
{
"name": "Frank",
"id": "f6d3cae7-f86a-4bc8-8d05-2fb65e8c6f3b",
"created_at": "2023-11-13T12:31:21.602389"
}
Navigate to the folder:
cd http-payload/django-rest-api/
Apply the migrations if necessary:
python manage.py migrate
Start the Django development web server:
python manage.py runserver
Navigate to the "users" resource in your web browser: http://127.0.0.1:8000/users/
You can use the web interface generated by Django REST Framework to send a POST request to add a new user, for example:
{"name": "Frank"}
Start the web server:
cd http-payload/fastapi-rest-api/
uvicorn main:app --reload
Navigate to the "users" resource in your web browser: http://127.0.0.1:8000/users
Send an HTTP GET request to retrieve all users:
curl -s http://127.0.0.1:8000/users | jq
[
{
"name": "Alice",
"id": "512a956f-165a-429f-9ec8-83d859843072",
"created_at": "2023-11-13T12:29:18.664574"
},
{
"name": "Bob",
"id": "fb52a80f-8982-46be-bcdd-605932d8ef03",
"created_at": "2023-11-13T12:29:18.664593"
}
]
Send an HTTP POST request to add a new user:
curl -s -X POST http://127.0.0.1:8000/users \
-H 'Content-Type: application/json' \
--data '{"name": "Frank"}' | jq
{
"name": "Frank",
"id": "f6d3cae7-f86a-4bc8-8d05-2fb65e8c6f3b",
"created_at": "2023-11-13T12:31:21.602389"
}
Start the FastAPI server:
cd http-payload/fastapi-rest-api/
uvicorn main:app --reload
Run the REST API consumer:
cd http-payload/pydantic-demo/
python main.py
cd hierarchical-data/
python bson_demo.py
python yaml_demo.py
cd tabular-data/csv-demo/
python main.py
cd tabular-data/parquet-demo/
python main.py
cd schema-based/avro-demo/
python main.py
Install the protoc
compiler on debian:
sudo apt install protobuf-compiler
Generate Python code from IDL:
cd schema-based/protocol-buffers-demo/
protoc --python_out=. --pyi_out=. users.proto
Run the demo:
python main.py