/python-serialize

python-serialize : Sample code for some Python Serialization methods

Primary LanguagePython

Serialize Your Data With Python

This folder contains the sample code for the tutorial Serialize Your Data With Python published on Real Python.

Table of Contents

Setup

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

Usage

Python Objects

Standard Python

cd python-objects/standard-python/
python3 pickle_demo.py
python3 marshal_demo.py
python3 shelve_demo.py
python3 dbm_demo.py

Customize Pickle

cd python-objects/customize-pickle/
python3 main.py

JSON Encode

cd python-objects/json-encode/
python main.py

Foreign Formats

jsonpickle and PyYAML:

cd python-objects/foreign-formats/
python jsonpickle_demo.py
python pyyaml_demo.py

Executable Code

Pickle-Importable Code

cd executable-code/pickle-importable/
python main.py

Code Objects

cd executable-code/code-objects/
python dill_demo.py

Digital Signature

cd executable-code/digital-signature/
python main.py

HTTP Payload

Flask

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"
}

Django REST Framework

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"}

FastAPI

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"
}

Pydantic

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

Hierarchical Data

XML, YAML, JSON, BSON

cd hierarchical-data/
python bson_demo.py
python yaml_demo.py

Tabular Data

CSV

cd tabular-data/csv-demo/
python main.py

Apache Parquet

cd tabular-data/parquet-demo/
python main.py

Schema-Based Formats

Apache Avro

cd schema-based/avro-demo/
python main.py

Protocol Buffers (Protobuf)

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