Skip to content

Commit 5837ea6

Browse files
authored
Merge pull request #8 from GREENRAT-K405/fix/infinite-loop
fix infinite loop error
2 parents c1cf7a4 + 4f4d230 commit 5837ea6

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

concore.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
from ast import literal_eval
44
import sys
5+
import numpy as np # Added for numpy type conversion
56

67
#if windows, create script to kill this process
78
# because batch files don't provide easy way to know pid of last command
@@ -11,6 +12,24 @@
1112
with open("concorekill.bat","w") as fpid:
1213
fpid.write("taskkill /F /PID "+str(os.getpid())+"\n")
1314

15+
# NumPy Type Conversion Helper
16+
def convert_numpy_to_python(obj):
17+
#Recursively convert numpy types to native Python types.
18+
#This is necessary because literal_eval cannot parse numpy representations
19+
#like np.float64(1.0), but can parse native Python types like 1.0.
20+
if isinstance(obj, np.generic):
21+
# Convert numpy scalar types to Python native types
22+
return obj.item()
23+
elif isinstance(obj, list):
24+
return [convert_numpy_to_python(item) for item in obj]
25+
elif isinstance(obj, tuple):
26+
return tuple(convert_numpy_to_python(item) for item in obj)
27+
elif isinstance(obj, dict):
28+
return {key: convert_numpy_to_python(value) for key, value in obj.items()}
29+
else:
30+
return obj
31+
32+
1433
try:
1534
iport = literal_eval(open("concore.iport").read())
1635
except:
@@ -74,8 +93,10 @@ def write(port, name, val, delta=0):
7493
try:
7594
with open(outpath+str(port)+"/"+name,"w") as outfile:
7695
if isinstance(val,list):
77-
outfile.write(str([simtime+delta]+val))
78-
simtime += delta
96+
val_converted = convert_numpy_to_python(val)
97+
data_to_write = [simtime + delta] + val_converted
98+
outfile.write(str(data_to_write))
99+
simtime += delta
79100
else:
80101
outfile.write(val)
81102
except:

0 commit comments

Comments
 (0)