Reference
python-bus examples
The runnable examples of the python-bus repository, with what each one shows.
The python-bus repository has runnable examples for the bus. Each example is one file about one idea. It runs against an in-process server or a fake device, so no hardware is needed.
To run an example, clone the repository and call its module:
hatch -e dev run python -m examples.tcp_connection
Start with tcp_connection, then timeouts, notifications, read_stream, discovery, status and concurrency.
Connections
| Example | Shows |
|---|---|
tcp_connection | Send one command over TCP and print the answer. |
udp_connection | Talk UDP to a device, over the same bus as everything else. |
http_request | Talk HTTP to a device, over the same bus as everything else. |
binary_framing | Talk to a device whose messages are neither text nor delimited. |
custom_connection | Add a connection type of your own with a ConnectionFactory. |
multiple_devices | Talk to 2 devices over one bus. |
status | Follow the state of a connection while it works and while it cannot be reached. |
Commands
| Example | Shows |
|---|---|
complex_command | Use one command class with different request and response types. |
command_builder | Build commands from request and response dataclasses with CommandBuilder. |
void_command | Send a command the device does not answer, and one it does. |
timeouts | Give a command only so much time, and see what happens when it is not enough. |
late_answer | See what happens to the command that follows one the device did not answer in time. |
Parallel commands and streams
| Example | Shows |
|---|---|
concurrency | Send 3 commands at once over a single connection. |
channel_pool | Send 3 commands at once over a pool of 3 connections. |
read_stream | Receive many answers to a single request. |
write_stream | Send a series of values over one connection. |
notifications | Receive messages a device sends without being asked. |
Device selection
| Example | Shows |
|---|---|
discovery | Find the right device among the attached ones, without naming it. |
which_device | Leave the device out of the config, and see why that alone is refused. |
Simulation
| Example | Shows |
|---|---|
simulation_mode | Answer a command without a device, with simulate and simulated=True. |
simulation_state | Keep values between simulated commands in bus.state. |
Last updated