python client

This commit is contained in:
BENEDEK László 2024-06-01 02:50:53 +02:00
parent 4fac1bd6d4
commit 4bdd751d75
8 changed files with 165 additions and 110 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
build
dist
graph.egg-info
.cache
__pycache__

View File

@ -18,6 +18,7 @@ include_directories("${CMAKE_SOURCE_DIR}/inc")
option(ENABLE_PLUGINS "Enable plugins" ON)
option(ENABLE_SERVER "Enable server" ON)
option(ENABLE_PYTHON_CLIENT "Enable Python client" ON)
# build plugins
if(ENABLE_PLUGINS)
@ -28,3 +29,8 @@ endif()
if(ENABLE_SERVER)
add_subdirectory("${CMAKE_SOURCE_DIR}/src/graph/server")
endif()
# build python package
if(ENABLE_PYTHON_CLIENT)
add_subdirectory("${CMAKE_SOURCE_DIR}/src/python")
endif()

29
src/python/CMakeLists.txt Normal file
View File

@ -0,0 +1,29 @@
if(NOT DEFINED PYTHON_EXE)
set(PYTHON_EXE "python")
endif()
option(INSTALL_PYTHON_CLIENT "Install the Python client package")
file(GLOB_RECURSE PYTHON_SOURCES "${CMAKE_SOURCE_DIR}/src/python/*.py")
add_custom_command(
OUTPUT "${CMAKE_BINARY_DIR}/python_package.stamp"
COMMAND "${CMAKE_COMMAND}" -E env "${PYTHON_EXE}" ./setup.py sdist
COMMAND "${CMAKE_COMMAND}" -E touch "${CMAKE_BINARY_DIR}/python_package.stamp"
DEPENDS "${PYTHON_SOURCES}"
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/src/python"
COMMENT "Building Python client package"
)
add_custom_target(build_python_package ALL
DEPENDS ${CMAKE_BINARY_DIR}/python_package.stamp
)
if(INSTALL_PYTHON_CLIENT)
add_custom_target(install_python_package ALL
COMMAND "${CMAKE_COMMAND}" -E env "${PYTHON_EXE}" -m pip install "${CMAKE_SOURCE_DIR}/src/python/dist/graph*"
COMMENT "Insatlling Python client package"
)
add_dependencies(install_python_package build_python_package)
endif()

0
src/python/__init__.py Normal file
View File

51
src/python/graph.py Normal file
View File

@ -0,0 +1,51 @@
from requests import session
from numpy import array
class Graph:
def __init__(self, series: str, chart: int = 0, address: str = "127.0.0.1", port: int = 8080):
self.base = f"http://{address}:{port}/add"
self.chart = chart
self.series = series
def __enter__(self):
self.session = session()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.session.close()
def list(self, points: list):
"""
Send a list of points to the graph.
Parameters:
points: eg. [(0.1, 13.5), (0.2, 14.9)]
"""
self.session.post(
self.base,
json={
"chart": self.chart,
"series": self.series,
"points": [ {"x": i[0], "y": i[1]} for i in points ]
}
)
def lists(self, x: list, y: list):
self.list(zip(x,y))
def point(self, point: tuple[float, float]):
self.session.get(f"{self.base}?chart={self.chart}&series={self.series}&x={point[0]}&y={point[1]}")
def __iadd__(self, data):
if (isinstance(data, list)):
self.list(data)
return self
elif (isinstance(data, tuple)):
if (isinstance(data[0], list)):
self.lists(data[0], data[1])
return self
elif (isinstance(data[0], float)):
self.point(data)
return self
raise Exception("unknown data format")

13
src/python/setup.py Normal file
View File

@ -0,0 +1,13 @@
from setuptools import setup
setup(
name="graph",
version="0.0.1",
description="dowerx/graph client for Python",
packages=["."],
install_requires=["requests"],
author="BENEDEK László",
author_email="lacbenedek@gmail.com",
keywords=["graph", "http", "client"],
url="https://git.tek.govt.hu/dowerx/graph"
)

64
test/client_example.py Normal file
View File

@ -0,0 +1,64 @@
from graph import Graph
from math import sin, cos
from time import sleep
import numpy as np
from argparse import ArgumentParser
def function(args, func):
x = 0.0
while True:
with Graph(series=args.series, chart=args.chart, address=args.address, port=args.port) as g:
try:
x += 0.01
g += (x, func(x))
sleep(0.01)
except KeyboardInterrupt:
break
def sine_array(args):
start = 0
with Graph(series=args.series, chart=args.chart, address=args.address, port=args.port) as g:
while True:
try:
x = np.linspace(start, start+0.5, args.num)
y = np.sin(x)
start += 0.5
g += (x.tolist(), y.tolist())
sleep(args.delay)
except KeyboardInterrupt:
break
def star(args):
with Graph(series=args.series, chart=args.chart, address=args.address, port=args.port) as g:
points = [
(2/4,2/2),
(3/4,0),
(0,1/2),
(4/4,1/2),
(1/4,0),
(2/4,2/2)
]
g += points
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("-t", action="store", dest="test", help="test type", type=str, choices=["sin", "cos", "array", "star"], required=False, default="sin")
parser.add_argument("-n", action="store", dest="num", help="sin_array size/iteration", type=int, required=False, default=100)
parser.add_argument("-d", action="store", dest="delay", help="delay between iteration", type=float, required=False, default=0.25)
parser.add_argument("-c", action="store", dest="chart", help="chart id", type=int, required=True)
parser.add_argument("-s", action="store", dest="series", help="series name", type=str, required=True)
parser.add_argument("-p", action="store", dest="port", help="server port", type=int, required=False, default=8080)
parser.add_argument("-a", action="store", dest="address", help="server address", type=str, required=False, default="127.0.0.1")
args = parser.parse_args()
if args.test == "sin":
function(args, sin)
if args.test == "cos":
function(args, cos)
elif args.test == "array":
sine_array(args)
elif args.test == "star":
star(args)

View File

@ -1,110 +0,0 @@
#!/bin/env python3
import requests
from math import sin, cos
from time import sleep
import numpy as np
from argparse import ArgumentParser
# import pyaudio
def sine(args):
x = 0.0
while True:
try:
requests.get(f"http://{args.address}:{args.port}/add?chart={args.chart}&series={args.series}&x={x}&y={sin(x)}")
x += 0.01
sleep(0.01)
except KeyboardInterrupt:
break
def cosine(args):
x = 0.0
while True:
try:
requests.get(f"http://{args.address}:{args.port}/add?chart={args.chart}&series={args.series}&x={x}&y={cos(x)}")
x += 0.01
sleep(0.01)
except KeyboardInterrupt:
break
def sine_array(args):
start = 0
while True:
try:
x = np.linspace(start, start+0.5, args.num)
y = np.sin(x)
start += 0.5
data = {
"chart": args.chart,
"series": args.series,
"points": [ {"x": i[0], "y":i[1]} for i in zip(x,y) ]
}
r = requests.post(f"http://{args.address}:{args.port}/add", json=data)
sleep(args.delay)
except KeyboardInterrupt:
break
def star(args):
points = [
(2/4,2/2),
(3/4,0),
(0,1/2),
(4/4,1/2),
(1/4,0),
(2/4,2/2)
]
for point in points:
requests.get(f"http://{args.address}:{args.port}/add?chart={args.chart}&series={args.series}&x={point[0]}&y={point[1]}")
# def audio():
# p = pyaudio.PyAudio()
# stream = p.open(
# input=True,
# input_device_index=0,
# format=pyaudio.paInt16,
# channels=1,
# rate=44100,
# frames_per_buffer=1024
# )
# stream.start_stream()
# while True:
# try:
# data = stream.read(1024, False)
# requests.post(
# "http://0.0.0.0:8080/add",
# json={
# "chart": 0,
# "series": "audio",
# "points": [ {"x": i, "y": v} for (i, v) in enumerate(data) ]
# },
# headers={"Connection":"close"})
# except KeyboardInterrupt:
# break
# stream.stop_stream()
# stream.close()
# p.terminate()
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("-t", action="store", dest="test", help="test type", type=str, choices=["sine", "cosine", "sine_array", "star"], required=False, default="sine")
parser.add_argument("-n", action="store", dest="num", help="sin_array size/iteration", type=int, required=False, default=100)
parser.add_argument("-d", action="store", dest="delay", help="delay between iteration", type=float, required=False, default=0.25)
parser.add_argument("-c", action="store", dest="chart", help="chart id", type=int, required=True)
parser.add_argument("-s", action="store", dest="series", help="series name", type=str, required=True)
parser.add_argument("-p", action="store", dest="port", help="server port", type=int, required=False, default=8080)
parser.add_argument("-a", action="store", dest="address", help="server address", type=str, required=False, default="127.0.0.1")
args = parser.parse_args()
if args.test == "sine":
sine(args)
if args.test == "cosine":
cosine(args)
elif args.test == "sine_array":
sine_array(args)
elif args.test == "star":
star(args)