Skip to main content

Python SDK for AveniECA

Project description

avenieca-python

Python SDK for publishing state signals to the AveniECA suite.

pip install avenieca-python

Usage

Stream continuously to a topic

import os
import numpy as np
from avenieca.utils import Config
from avenieca.utils import Signal
from avenieca.producers import Stream

# Define a handler that returns a signal dict like  
# the sample in avenieca.utils.signal

def handler():
    Signal["valence"] = 10
    Signal["state"] = np.array([0.2, 0.3, 0.8])
    return Signal

# Initialize Kafka configuration for the Stream
Config["bootstrap_servers"] = os.environ["KAFKA_URL"]
Config["topic"] = "left_wheel" #digital twin subscriber-topic

#Initialize the Stream object with a sync_rate 
# (the rate at which to publish signals).
stream = Stream(config=Config, sync_rate=1)
stream.publish(handler)

Publish one signal as an event

import os
import numpy as np
from avenieca.utils import Config
from avenieca.utils import Signal
from avenieca.producers import Event

# Initialize Kafka configuration for the Event
Config["bootstrap_servers"] = os.environ["KAFKA_URL"]
Config["topic"] = "left_wheel" #digital twin subscriber-topic

# Define the signal
Signal["valence"] = 9
Signal["state"] = np.array([0.2, 0.3, 0.8])

event = Event(config=Config)
event.publish(Signal)

Consume from kafka topic

import os
import numpy as np
from avenieca.utils import Config
from avenieca.utils.signal import get_state_as_list, get_state_as_array
from avenieca.consumer import Consumer

# Initialize Kafka configuration for the Event
Config["bootstrap_servers"] = os.environ["KAFKA_URL"]
Config["topic"] = "left_wheel" #digital twin subscriber-topic

# Define a handler to process incoming messages
def handler(data):
    valence = data["valence"]
    state = data["state"]
    assert valence == 10
    assert state == "[0.2, 0.3, 0.8]"

client = Consumer(config=Config)
client.consume(handler, True) # pass in handler

# You can use util functions in your handler to 
# convert the state signal from byte string to 
# np.ndarray or python list
def handler(data):
    assert data["valence"] == 10
    assert data["state"] == "[0.2, 0.3, 0.8]"
    get_state_as_list(data)
    assert data["state"] == [0.2, 0.3, 0.8]

def handler(data):
    assert data["valence"] == 10
    assert data["state"] == "[0.2, 0.3, 0.8]"
    get_state_as_array(data)
    assert True, np.array_equal(data["state"], np.array([0.2, 0.3, 0.8]))

Tests

python -m pytest test/

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

avenieca-python-0.1.2.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

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

avenieca_python-0.1.2-py3-none-any.whl (18.3 kB view details)

Uploaded Python 3

File details

Details for the file avenieca-python-0.1.2.tar.gz.

File metadata

  • Download URL: avenieca-python-0.1.2.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for avenieca-python-0.1.2.tar.gz
Algorithm Hash digest
SHA256 a9d8f9d035ca9ffab67f604ddb477dbdaea2441019b6b3a2b91f158d387a5b72
MD5 1b6f1c5382a34c299aef4930e9b95bd5
BLAKE2b-256 6abe07f7661322090a8de392f58f74ed2e5d6a1a633c33341e78fc52d7cb89a5

See more details on using hashes here.

File details

Details for the file avenieca_python-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for avenieca_python-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0231c461c29d91f5ad630ae81200c1abc1ad12b2c80e91f24d2548696bb36ec9
MD5 6ee72dee459de6d245bbe72a65cc04b0
BLAKE2b-256 53edb772c276c0b5814395dcd4bd6e6d467d2048e24e576d214be624a482f986

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