Skip to content

Commit bbd1854

Browse files
authored
Merge pull request #209 from CESNET/pytrap_setarrayfromdict
pytrap: add capability to set arrays from dict
2 parents b5c0896 + 3f51166 commit bbd1854

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

pytrap/src/unirecmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1523,7 +1523,7 @@ UnirecTemplate_setFromDict(pytrap_unirectemplate *self, PyObject *dict, int skip
15231523
Py_DECREF(idkey);
15241524
return NULL;
15251525
}
1526-
} else if (PyLong_Check(v)) {
1526+
} else {
15271527
if (UnirecTemplate_set_local(self, self->data, id, v) == NULL) {
15281528
Py_DECREF(idkey);
15291529
return NULL;

pytrap/test/unirectemplate_unittest.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,3 +839,41 @@ def runTest(self):
839839
elapsed_time = time.process_time() - startt
840840
print(f"Elapsed time for {messages} messages is: {elapsed_time}")
841841

842+
class ArrayFromDict(unittest.TestCase):
843+
def runTest(self):
844+
import pytrap
845+
rec = pytrap.UnirecTemplate("int8* PPI_PKT_DIRECTIONS,uint8* PPI_PKT_FLAGS,uint16* PPI_PKT_LENGTHS,uint32* DBI_BRST_BYTES,time* DBI_BRST_TIME_START,ipaddr SRC_IP,time TIME")
846+
rec.createMessage(10000)
847+
# prepare dict
848+
data = {
849+
"SRC_IP": pytrap.UnirecIPAddr("10.0.0.1"),
850+
"TIME": pytrap.UnirecTime(1669885132, 853),
851+
"PPI_PKT_DIRECTIONS": [
852+
1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
853+
-1, -1, -1, -1, -1, -1, -1, 1, -1, -1
854+
],
855+
"PPI_PKT_FLAGS": [
856+
24, 16, 24, 16, 24, 24, 24, 24, 24, 24, 24, 24, 16, 24, 16, 24, 16, 24, 16,
857+
24, 16, 24, 16, 24, 16, 24, 16, 24, 24, 16
858+
],
859+
"PPI_PKT_LENGTHS": [
860+
517, 1400, 1400, 1400, 282, 74, 98, 416, 978, 31, 590, 1400, 1400, 1400,
861+
1400, 1400, 1400, 1400, 1400, 1400, 1400, 1400, 1400, 1400, 1400, 1400,
862+
1400, 31, 1400, 1400
863+
],
864+
"DBI_BRST_BYTES": [
865+
71274
866+
],
867+
"DBI_BRST_TIME_START": [
868+
pytrap.UnirecTime(1669885132, 853),
869+
pytrap.UnirecTime(1669985132, 853)
870+
]
871+
}
872+
873+
# set it into UnirecTemplate
874+
rec.setFromDict(data)
875+
# get the content
876+
stored = rec.getDict()
877+
# compare it
878+
self.assertEqual(stored, data)
879+

0 commit comments

Comments
 (0)