From 89e8efb86479cfb65d912850bb1331a19bd7ccf1 Mon Sep 17 00:00:00 2001 From: Nicolas Kruse Date: Thu, 27 Nov 2025 17:20:07 +0100 Subject: [PATCH] read_value function extended to work with vectors --- src/copapy/_target.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/copapy/_target.py b/src/copapy/_target.py index 5e15912..6039ce5 100644 --- a/src/copapy/_target.py +++ b/src/copapy/_target.py @@ -1,4 +1,4 @@ -from typing import Iterable, overload +from typing import Iterable, overload, TypeVar, Any from . import _binwrite as binw from coparun_module import coparun, read_data_mem import struct @@ -6,6 +6,8 @@ from ._basic_types import stencil_db_from_package from ._basic_types import variable, Net, Node, Write, NumLike from ._compiler import compile_to_dag +T = TypeVar("T", int, float) + def add_read_command(dw: binw.data_writer, variables: dict[Net, tuple[int, int, str]], net: Net) -> None: assert net in variables, f"Variable {net} not found in data writer variables" @@ -41,6 +43,7 @@ class Target(): assert isinstance(net, Net), f"The folowing element is not a Net: {net}" nodes.append(Write(net)) else: + assert isinstance(s, Net), f"The folowing element is not a Net: {s}" nodes.append(Write(s)) dw, self._variables = compile_to_dag(nodes, self.sdb) @@ -56,12 +59,12 @@ class Target(): assert coparun(dw.get_data()) > 0 @overload - def read_value(self, net: variable[float]) -> float: ... - @overload - def read_value(self, net: variable[int]) -> int: ... + def read_value(self, net: variable[T]) -> T: ... @overload def read_value(self, net: NumLike) -> float | int | bool: ... - def read_value(self, net: NumLike) -> float | int | bool: + @overload + def read_value(self, net: Iterable[T | variable[T]]) -> list[T]: ... + def read_value(self, net: NumLike | variable[T] | Iterable[T | variable[T]]) -> Any: """Reads the value of a variable. Arguments: @@ -70,6 +73,13 @@ class Target(): Returns: Value of the variable """ + if isinstance(net, Iterable): + return [self.read_value(ni) if isinstance(ni, variable) else ni for ni in net] + + if isinstance(net, float | int): + print(f"Warning: value is not a copypy value") + return net + assert isinstance(net, Net), "Variable must be a copapy variable object" assert net in self._variables, f"Variable {net} not found. It might not have been compiled for the target." addr, lengths, var_type = self._variables[net]