Skip to main content

A Python API for the balances in the Matter Lab.

Project description

matterlab_balances

matterlab_balances provides drivers for the balances used in the Matter Lab. It includes a serial driver for Sartorius balances and a network client for Mettler Toledo XPR/XSR balances, along with a small shared base interface for balance operations.

Supported Devices

  • Balance: shared abstract base class
  • SartoriusBalance
  • MTXPRBalance
  • MTXPRBalanceDoors: door identifiers for the MT client

Installation

Install the package:

pip install matterlab_balances

For development:

pip install -e .[dev]

The package depends on:

  • matterlab_serial_device
  • requests
  • suds
  • jinja2
  • pprp

Quick Start

Example with a Sartorius balance:

from matterlab_balances import SartoriusBalance

balance = SartoriusBalance(com_port="COM5")
print(balance.weigh(stable=False))
balance.tare(stable=False)

Example with an MT XPR/XSR balance:

from matterlab_balances import MTXPRBalance
from matterlab_balances.mt_balance import WeighingCaptureMode

balance = MTXPRBalance(host="192.168.1.1", port=8002, password="password")
value, unit, stable = balance.get_weight(capture_mode=WeighingCaptureMode.IMMEDIATE)
print(value, unit, stable)
balance.close_session()

Core API

The shared Balance interface exposes:

  • weigh(stable=True)
  • tare(stable=True)

Device-specific classes add their own helpers:

  • SartoriusBalance: direct serial weighing and tare control
  • MTXPRBalance: session handling, get_weight(...), tare(...), zero(...), door control, and dosing-related operations

Testing

Simulation Tests

Simulation tests are the default validation layer and are safe for CI.

One-click run:

python examples/test_sim.py

Direct pytest run:

pytest tests/test_base_balance.py tests/test_sartorius_sim.py tests/test_mt_sim.py

Hardware Tests

Hardware tests are opt-in because real balances may not be reachable in every environment.

Available hardware test files:

  • tests/test_sartorius_real.py
  • tests/test_mt_real.py

They are marked with @pytest.mark.real and are skipped unless --run-real is supplied.

Examples:

pytest tests/test_sartorius_real.py --run-real --com-port COM5
pytest tests/test_mt_real.py --run-real --host 192.168.1.1 --port 8002 --password password

You can also provide hardware configuration through environment variables:

  • BALANCE_COM_PORT
  • BALANCE_HOST
  • BALANCE_PORT
  • BALANCE_PASSWORD

Examples

The examples/ directory contains the recommended entry points:

  • examples/test_sim.py: one-click simulation-only test runner
  • examples/real_devices.py: readable examples for Sartorius and MT balances
  • examples/README.md: short guide to the examples directory

Development

Useful commands:

pytest
ruff check .
ruff format .

The default pytest configuration includes retries, timeouts, coverage output, and uses src as the import root for local development.

Package Layout

src/matterlab_balances/
  base_balance.py
  mt_balance.py
  sartorius_balance.py
tests/
examples/

License

This project is distributed under the MIT License. See LICENSE for details.

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

matterlab_balances-1.1.0.tar.gz (48.5 kB view details)

Uploaded Source

Built Distribution

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

matterlab_balances-1.1.0-py3-none-any.whl (44.5 kB view details)

Uploaded Python 3

File details

Details for the file matterlab_balances-1.1.0.tar.gz.

File metadata

  • Download URL: matterlab_balances-1.1.0.tar.gz
  • Upload date:
  • Size: 48.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.13

File hashes

Hashes for matterlab_balances-1.1.0.tar.gz
Algorithm Hash digest
SHA256 3e6851beb8a1cd3d45df83f5923fa0db84a0fbec63ac6d0e0929ed6da44e3d82
MD5 48420af2798d81ee4ef0245f9c7f1004
BLAKE2b-256 34c0c0cb263109538198434d97a2dcf4905d894bd70448f64cf57c8250f77fe8

See more details on using hashes here.

File details

Details for the file matterlab_balances-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for matterlab_balances-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b3f603cfbc8b496799dd70af54a831553177074f280ef87fbed7423a5f8254b2
MD5 9429f797f78107c15916a5c8c92a12ac
BLAKE2b-256 8d2baaea0c5dae55bfd1c3c469f7dc6c6d974430f60e3f8a25593bc2a1342f09

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