Skip to content

Commit 6c4c2a0

Browse files
committed
Merge branch 'master' of https://github.com/chathika/nl4py into master
2 parents c5a9699 + ba35b4c commit 6c4c2a0

File tree

11 files changed

+26
-17
lines changed

11 files changed

+26
-17
lines changed
-37 Bytes
Binary file not shown.
-56 Bytes
Binary file not shown.
-37 Bytes
Binary file not shown.
-56 Bytes
Binary file not shown.

β€Žsrc/client/NL4Py/CHANGES.txtβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ v0.5.10, 17th February 2020 -- awaitScheduledReporterResults fix: fixed bug that
3030
v0.5.11, 21st April 2020 -- Mac OS library misindentification fix and accommodation for levelspace extension
3131
v0.7.0, 04th October 2020 -- PEP compliance for API, run_experiment
3232
v0.8.0, 13th April 2020 -- API fixes; run_experiment return pandas dataframe
33-
v0.8.1, 2nd June 2021 -- Safe conversion of reporter results into Python objects when non-eval'uable objects are returned.
33+
v0.8.1, 2nd June 2021 -- Safe conversion of reporter results into Python objects when non-eval'uable objects are returned.
34+
v0.9.0, 8th June 2021 -- Safe conversion of schedule_reporters results into Python objects when non-eval'uable objects are returned, fix to workspace disposal

β€Žsrc/client/NL4Py/nl4py/NetLogoHeadlessWorkspace.pyβ€Ž

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
You should have received a copy of the GNU General Public License
1515
along with this program. If not, see <http://www.gnu.org/licenses/>.'''
1616

17-
import os
18-
import socket
19-
from typing import List, Any, Union
17+
from typing import List, Any, Union, Dict
2018

2119
from py4j.protocol import Py4JNetworkError
2220
from py4j.java_gateway import JavaGateway, GatewayParameters, is_instance_of
@@ -76,12 +74,7 @@ def report(self, reporter : str) -> str:
7674
reporter on its HeadlessWorkspace object
7775
'''
7876
result = self.hwc.report(reporter.encode()).decode(encoding='UTF-8')
79-
# Try to convert result to Python objects
80-
try:
81-
result = eval(result)
82-
except SyntaxError:
83-
pass
84-
return result
77+
return self._normalize(result)
8578

8679
def schedule_reporters(self, reporters : List[str], start_at_tick : int = 0, interval_ticks : int = 1,
8780
stop_at_tick : int = -1, go_command : str = 'go') -> List[str]:
@@ -92,15 +85,30 @@ def schedule_reporters(self, reporters : List[str], start_at_tick : int = 0, int
9285
for idx, reporter in enumerate(reporters):
9386
reporter_array.append(str(reporter).encode())
9487
ticks_reporters_results = self.hwc.scheduleReportersAndRun(reporter_array,start_at_tick,
95-
interval_ticks,stop_at_tick,go_command)
88+
interval_ticks,stop_at_tick,go_command)
9689
out_ticks_reporter_results = []
9790
for reporters_results in ticks_reporters_results:
9891
out_reporter_results = []
9992
for result in reporters_results:
100-
out_reporter_results.append(result)
93+
out_reporter_results.append(self._normalize(result))
10194
out_ticks_reporter_results.append(out_reporter_results)
10295
return out_ticks_reporter_results
103-
96+
97+
def _normalize(self, result : str) -> Union[str, int, float, List, Dict]:
98+
'''
99+
Tries to normalize result into Python constructs if possible
100+
'''
101+
try:
102+
eval_result = eval(result)
103+
t = type(eval_result)
104+
if (t == str) or (t == int) or (t == float) or (t == list) or (t == dict):
105+
result = eval_result
106+
else:
107+
result = str(result)
108+
except SyntaxError:
109+
pass
110+
return float(result)
111+
104112
def refresh(self):
105113
'''
106114
Sends a signal to the server to tell the respective controller to create a
@@ -186,7 +194,7 @@ def deleteWorkspace(self):
186194
'''
187195
Kills the Workspace and its controller on the server
188196
'''
189-
self.hwc.removeControllerFromStore()
197+
self.hwc.disposeWorkspace()
190198
self.gateway.close()
191199

192200
@deprecated('Alias left for backward compatibility. Use open_model() since version 1.0.0.')
Binary file not shown.
Binary file not shown.

β€Žsrc/client/NL4Py/setup.pyβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name='NL4Py',
5-
version='0.8.1',
5+
version='0.9.0',
66
author='Chathika Gunaratne',
77
author_email='[email protected]',
88
packages=['nl4py', 'nl4py.test'],

β€Žsrc/server/HeadlessWorkspaceController.javaβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public SearchSpace getParamList(String path) {
151151
return ss;
152152
}
153153

154-
protected void disposeWorkspace(){
154+
public void disposeWorkspace(){
155155
this.closeModel();
156156
try{
157157
ws.dispose();

0 commit comments

Comments
Β (0)