Skip to content

Commit 83940f8

Browse files
authored
355 convert long num arrays (#358)
addresses #355
1 parent aa76710 commit 83940f8

File tree

2 files changed

+96
-3
lines changed

2 files changed

+96
-3
lines changed

pycyphal/application/register/_value.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,21 @@ def _strictify(s: RelaxedValue) -> Value:
350350
if all(isinstance(x, bool) for x in s):
351351
return _strictify(Bit(s))
352352
if all(isinstance(x, (int, bool)) for x in s):
353-
return _strictify(Natural64(s)) if all(x >= 0 for x in s) else _strictify(Integer64(s))
354-
if all(isinstance(x, (float, int, bool)) for x in s):
355-
return _strictify(Real64(s))
353+
if len(s) <= 32:
354+
return _strictify(Natural64(s)) if all(x >= 0 for x in s) else _strictify(Integer64(s))
355+
if len(s) <= 64:
356+
return _strictify(Natural32(s)) if all(x >= 0 for x in s) else _strictify(Integer32(s))
357+
if len(s) <= 128:
358+
return _strictify(Natural16(s)) if all(x >= 0 for x in s) else _strictify(Integer16(s))
359+
if len(s) <= 256:
360+
return _strictify(Natural8(s)) if all(x >= 0 for x in s) else _strictify(Integer8(s))
361+
elif all(isinstance(x, (float, int, bool)) for x in s):
362+
if len(s) <= 32:
363+
return _strictify(Real64(s))
364+
if len(s) <= 64:
365+
return _strictify(Real32(s))
366+
if len(s) <= 128:
367+
return _strictify(Real16(s))
356368

357369
raise ValueConversionError(f"Don't know how to convert {s!r} into {Value}") # pragma: no cover
358370

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Copyright (c) 2025 OpenCyphal
2+
# This software is distributed under the terms of the MIT License.
3+
# Author: Huong Pham <[email protected]>
4+
from typing import List
5+
6+
7+
def _unittest_strictify_bool() -> None:
8+
# noinspection PyProtectedMember
9+
from pycyphal.application.register._value import _strictify
10+
11+
s = [True, False]
12+
n = _strictify(s).bit
13+
assert n is not None
14+
v = n.value
15+
assert (s == v).all() # type: ignore[attr-defined]
16+
17+
18+
def _unittest_strictify_u64() -> None:
19+
# noinspection PyProtectedMember
20+
from pycyphal.application.register._value import _strictify
21+
22+
s = [x * 1000000 for x in range(30)]
23+
n = _strictify(s).natural64
24+
assert n is not None
25+
v = n.value
26+
assert (s == v).all() # type: ignore[attr-defined]
27+
28+
29+
def _unittest_strictify_u32() -> None:
30+
# noinspection PyProtectedMember
31+
from pycyphal.application.register._value import _strictify
32+
33+
s = [x * 1000000 for x in range(60)]
34+
n = _strictify(s).natural32
35+
assert n is not None
36+
v = n.value
37+
assert (s == v).all() # type: ignore[attr-defined]
38+
39+
40+
def _unittest_strictify_u16() -> None:
41+
# noinspection PyProtectedMember
42+
from pycyphal.application.register._value import _strictify
43+
44+
s = [x * 100 for x in range(80)]
45+
n = _strictify(s).natural16
46+
assert n is not None
47+
v = n.value
48+
assert (s == v).all() # type: ignore[attr-defined]
49+
50+
51+
def _unittest_strictify_i64() -> None:
52+
# noinspection PyProtectedMember
53+
from pycyphal.application.register._value import _strictify
54+
55+
s = [-x * 1000000 for x in range(30)]
56+
n = _strictify(s).integer64
57+
assert n is not None
58+
v = n.value
59+
assert (s == v).all() # type: ignore[attr-defined]
60+
61+
62+
def _unittest_strictify_i32() -> None:
63+
# noinspection PyProtectedMember
64+
from pycyphal.application.register._value import _strictify
65+
66+
s = [-x * 1000000 for x in range(60)]
67+
n = _strictify(s).integer32
68+
assert n is not None
69+
v = n.value
70+
assert (s == v).all() # type: ignore[attr-defined]
71+
72+
73+
def _unittest_strictify_i16() -> None:
74+
# noinspection PyProtectedMember
75+
from pycyphal.application.register._value import _strictify
76+
77+
s = [-x * 100 for x in range(80)]
78+
n = _strictify(s).integer16
79+
assert n is not None
80+
v = n.value
81+
assert (s == v).all() # type: ignore[attr-defined]

0 commit comments

Comments
 (0)