Python
Python is the glue layer in the Introduction’s Blockchain + IoT + AI stack: it powers device provisioning, data ingestion, validation, AI pipelines, and API backends that interact with assets and proofs on-chain. This page focuses on the practical skills to move data from ESP32‑class devices to services that the Neurai ecosystem can verify and consume. See the Introduction for context.
What you’ll learn
- Environment setup (venv), packaging basics
- Essentials: types, control flow, functions, modules
- Files, JSON/CSV, HTTP APIs, async IO
- Talking to devices (serial/MQTT) and simple data pipelines
Install and environment
- Install Python 3.10+ for best library support.
- Create a virtual environment and install packages per project.
Language essentials (very short tour)
# variables and types
x = 42
msg = f"Value: {x}"
nums = [1, 2, 3]
point = {"x": 1, "y": 2}
def inc(n: int) -> int:
return n + 1
for n in nums:
print(inc(n))
Files and JSON
from pathlib import Path
import json
path = Path("data.json")
data = {"sensor": "temp", "value": 23.7}
path.write_text(json.dumps(data))
print(json.loads(path.read_text()))
HTTP requests
import requests
r = requests.get("https://httpbin.org/get", timeout=10)
print(r.status_code, r.json())
Async IO (concurrency without threads)
import asyncio
async def work(i):
await asyncio.sleep(0.2)
print("done", i)
async def main():
await asyncio.gather(*(work(i) for i in range(5)))
asyncio.run(main())
Serial and MQTT (IoT basics)
# Serial (pyserial)
import serial
ser = serial.Serial("/dev/ttyUSB0", 115200, timeout=1)
ser.write(b"ping\n")
print(ser.readline())
# MQTT (paho-mqtt)
import paho.mqtt.client as mqtt
client = mqtt.Client()
client.connect("broker.hivemq.com", 1883, 60)
client.publish("demo/sensors/temp", "23.7")
client.disconnect()
Next steps
- Packaging: pyproject.toml, build backends, publishing wheels.
- Testing: pytest, coverage, property‑based tests.
- Data: pandas, numpy; plotting with matplotlib/seaborn.
- APIs: FastAPI for REST; webhooks and background tasks; auth and rate limiting.