mirror of https://github.com/Nonannet/copapy.git
read_value function extended to work with vectors
This commit is contained in:
parent
ad78c4089c
commit
89e8efb864
|
|
@ -1,4 +1,4 @@
|
||||||
from typing import Iterable, overload
|
from typing import Iterable, overload, TypeVar, Any
|
||||||
from . import _binwrite as binw
|
from . import _binwrite as binw
|
||||||
from coparun_module import coparun, read_data_mem
|
from coparun_module import coparun, read_data_mem
|
||||||
import struct
|
import struct
|
||||||
|
|
@ -6,6 +6,8 @@ from ._basic_types import stencil_db_from_package
|
||||||
from ._basic_types import variable, Net, Node, Write, NumLike
|
from ._basic_types import variable, Net, Node, Write, NumLike
|
||||||
from ._compiler import compile_to_dag
|
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:
|
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"
|
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}"
|
assert isinstance(net, Net), f"The folowing element is not a Net: {net}"
|
||||||
nodes.append(Write(net))
|
nodes.append(Write(net))
|
||||||
else:
|
else:
|
||||||
|
assert isinstance(s, Net), f"The folowing element is not a Net: {s}"
|
||||||
nodes.append(Write(s))
|
nodes.append(Write(s))
|
||||||
|
|
||||||
dw, self._variables = compile_to_dag(nodes, self.sdb)
|
dw, self._variables = compile_to_dag(nodes, self.sdb)
|
||||||
|
|
@ -56,12 +59,12 @@ class Target():
|
||||||
assert coparun(dw.get_data()) > 0
|
assert coparun(dw.get_data()) > 0
|
||||||
|
|
||||||
@overload
|
@overload
|
||||||
def read_value(self, net: variable[float]) -> float: ...
|
def read_value(self, net: variable[T]) -> T: ...
|
||||||
@overload
|
|
||||||
def read_value(self, net: variable[int]) -> int: ...
|
|
||||||
@overload
|
@overload
|
||||||
def read_value(self, net: NumLike) -> float | int | bool: ...
|
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.
|
"""Reads the value of a variable.
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
|
|
@ -70,6 +73,13 @@ class Target():
|
||||||
Returns:
|
Returns:
|
||||||
Value of the variable
|
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 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."
|
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]
|
addr, lengths, var_type = self._variables[net]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue