Skip to main content

A Python library for managing spx server.

Project description

spx-python — Quick‑Start Guide

Lightweight Python wrapper for the SPX server — model, simulate and test devices through the SPX REST API.
Designed for effortless use in local development, CI pipelines, and unit‑testing suites.


Table of Contents

  1. Requirements
  2. Installation
  3. Running an SPX server
  4. Connecting from Python
  5. Common operations (step‑by‑step)
  6. Full‑system example
  7. Using the client in unit tests
  8. CI integration (GitHub Actions)
  9. FAQ
  10. License

Requirements

Requirement Notes
Python ≥ 3.7 Officially tested on 3.9 – 3.12
Docker & Docker Compose To launch spx‑server locally
SPX_PRODUCT_KEY Export in your shell or set as a CI secret

Installation

pip install spx-python                         # or add to poetry/requirements.txt

If you develop the package locally:

poetry add --dev spx-python pytest pytest-cov coverage

Running an SPX server

1. Create docker‑compose.yml (minimal):

services:
  spx-server:
    image: simplephysx/spx-server:latest
    ports: ["8000:8000"]
    environment:
      SPX_PRODUCT_KEY: ${SPX_PRODUCT_KEY}
    healthcheck:
      test: ["CMD-SHELL", "curl -f http://localhost:8000/ || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 5

2. Start it:

export SPX_PRODUCT_KEY="YOUR_REAL_KEY"
docker compose up -d         # server listens on :8000

Connecting from Python

import os, spx_python
from spx_python.client import SpxClient

client: SpxClient = spx_python.init(
    address     = "http://localhost:8000",   # default
    product_key = os.environ["SPX_PRODUCT_KEY"],
)

client now represents the root of the SPX system and behaves like a dictionary plus convenience helpers.


Common operations (step‑by‑step)

1 · Inspect an empty system

print(client.keys())      # ['models', 'instances', 'timer', 'polling']

2 · Add a model

client["models"]["TemperatureSensor"] = {
    "attributes": {"temperature": 25.0, "heating_power": 0.0}
}

3 · Create an instance

client["instances"]["sensor"] = "TemperatureSensor"
sensor = client["instances"]["sensor"]

4 · Read / write an attribute

temp_attr = sensor["attributes"]["temperature"]
print(temp_attr.internal_value)      # 25.0
temp_attr.internal_value = 30.0      # update

5 · Delete things

del client["instances"]["sensor"]
del client["models"]["TemperatureSensor"]

Full‑system example

The unit‑tests (tests/test_full_spx_python.py) show a PID loop built from three models (TemperatureSensor, PowerSupply, PIDController).
Load the YAML, create instances, wire connections, then run prepare() and run() to watch values propagate.


Using the client in unit tests

def test_attribute_roundtrip():
    key = os.environ["SPX_PRODUCT_KEY"]
    w   = spx_python.init(product_key=key)

    w["models"]["Foo"] = {"attributes": {"x": 1}}
    w["instances"]["foo1"] = "Foo"
    inst = w["instances"]["foo1"]

    inst["attributes"]["x"].internal_value = 42
    assert inst["attributes"]["x"].internal_value == 42

CI integration (GitHub Actions)

env:
  SPX_PRODUCT_KEY: ${{ secrets.SPX_PRODUCT_KEY }}

steps:
  - uses: actions/checkout@v3
  - uses: actions/setup-python@v4
    with: { python-version: "3.10" }

  - run: |
      python -m pip install poetry
      poetry install

  - run: docker compose up -d        # start server
  - run: |                           # wait until healthy
      for i in {1..10}; do
        curl -fs http://localhost:8000/ && break
        echo "waiting…"; sleep 5
      done
  - run: |                           # run unit tests + coverage
      poetry run python -m unittest discover -s tests
  - if: always()
    run: docker compose down

FAQ

Question Answer
Why not use str|None type‑hints? Optional[str] keeps compatibility with Python 3.7 – 3.8.

License

SPX‑Python is released under the MIT License – see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spx_python-0.1.0rc7.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

spx_python-0.1.0rc7-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file spx_python-0.1.0rc7.tar.gz.

File metadata

  • Download URL: spx_python-0.1.0rc7.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for spx_python-0.1.0rc7.tar.gz
Algorithm Hash digest
SHA256 e19ae3d7d7f64474e898264ba711d015fcb5374293bf0b9f8d2074988e47ee19
MD5 6e2018c14371ef0067444f79f56bf2cc
BLAKE2b-256 aea3898ad9f1e3362711599894dbb954bec2b77fe1252cb3e005aa71b1278244

See more details on using hashes here.

File details

Details for the file spx_python-0.1.0rc7-py3-none-any.whl.

File metadata

  • Download URL: spx_python-0.1.0rc7-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for spx_python-0.1.0rc7-py3-none-any.whl
Algorithm Hash digest
SHA256 7d193bb6c7ac41706b05fed2eee07dc7fe7407407e10474da1c636e17abd2a22
MD5 bf4c807ad488c956474449240a7f1622
BLAKE2b-256 f2da6a3b76ec50d600d8a67534ba2baddf23fe9a3c6aa30a18cdd06027beea5b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page