forked from DynamoDS/PythonNet3Engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonLibraryTests.cs
More file actions
270 lines (242 loc) · 7.56 KB
/
PythonLibraryTests.cs
File metadata and controls
270 lines (242 loc) · 7.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
using DSPythonNet3;
using System.Collections;
namespace DSPythonNet3Tests
{
public class PythonLibraryTests
{
[Test]
public void TestSciPyAvailable()
{
string code = @"
from scipy import special
OUT = special.round(3.3333333)
";
var empty = new ArrayList();
var result = DSPythonNet3Evaluator.EvaluatePythonScript(code, empty, empty);
Assert.That(result, Is.EqualTo(3.0));
}
[Test]
public void TestContourPyAvailable()
{
string code = @"
import numpy as np
import contourpy
x = np.linspace(-2, 2, 20)
y = np.linspace(-2, 2, 20)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) * np.cos(Y)
contour_generator = contourpy.contour_generator(x, y, Z)
lines = contour_generator.lines(0.0)
OUT = len(lines)
";
var empty = new ArrayList();
var result = DSPythonNet3Evaluator.EvaluatePythonScript(code, empty, empty);
Assert.That(result, Is.EqualTo(3));
}
[Test]
public void TestCyclerAvailable()
{
string code = @"
from cycler import cycler
color_cycle = cycler(color=['r', 'g', 'b'])
OUT = len(color_cycle)
";
var empty = new ArrayList();
var result = DSPythonNet3Evaluator.EvaluatePythonScript(code, empty, empty);
Assert.That(result, Is.EqualTo(3));
}
[Test]
public void TestEt_xmlfileAvailable()
{
string code = @"
from io import BytesIO
from xml.etree.ElementTree import Element
from et_xmlfile import xmlfile
out = BytesIO()
with xmlfile(out) as xf:
el = Element(""root"")
xf.write(el)
OUT = out.getvalue() == b""<root />""
";
var empty = new ArrayList();
var result = DSPythonNet3Evaluator.EvaluatePythonScript(code, empty, empty);
Assert.That(result, Is.EqualTo(true));
}
[Test]
public void TestFonttoolsAvailable()
{
string code = @"
from fontTools.pens import svgPathPen
pen = svgPathPen.SVGPathPen(None)
pen.moveTo((0, 0))
pen.lineTo((1, 1))
OUT = pen.getCommands()
";
var empty = new ArrayList();
var result = DSPythonNet3Evaluator.EvaluatePythonScript(code, empty, empty);
Assert.That(result, Is.EqualTo("M0 0 1 1"));
}
[Test]
public void TestKiwisolverAvailable()
{
string code = @"
import kiwisolver as kiwi
solver = kiwi.Solver()
x = kiwi.Variable('x')
solver.addConstraint(3 * x + 5 == 14)
solver.updateVariables()
OUT = x.value()
";
var empty = new ArrayList();
var result = DSPythonNet3Evaluator.EvaluatePythonScript(code, empty, empty);
Assert.That(result, Is.EqualTo(3));
}
[Test]
public void TestMatplotlibAvailable()
{
string code = @"
import matplotlib as mpl
cmap = mpl.colormaps['plasma']
OUT = cmap.name
";
var empty = new ArrayList();
var result = DSPythonNet3Evaluator.EvaluatePythonScript(code, empty, empty);
Assert.That(result, Is.EqualTo("plasma"));
}
[Test]
public void TestPillowAvailable()
{
string code = @"
from PIL import Image, ImageDraw
image = Image.new('RGB', (100, 100), 'white')
draw = ImageDraw.Draw(image)
draw.rectangle((25, 25, 100, 100), fill='red')
OUT = image.getpixel((50, 50))[0]
";
var empty = new ArrayList();
var result = DSPythonNet3Evaluator.EvaluatePythonScript(code, empty, empty);
Assert.That(result, Is.EqualTo(255));
}
[Test]
public void TestPyParsingAvailable()
{
string code = @"
import pyparsing as pp
parser = pp.Word(pp.alphas) + ',' + pp.Word(pp.alphas)
parsed = parser.parse_string('Hello, World')
OUT = len(parsed)
";
var empty = new ArrayList();
var result = DSPythonNet3Evaluator.EvaluatePythonScript(code, empty, empty);
Assert.That(result, Is.EqualTo(3));
}
[Test]
public void TestPythonDateutilAvailable()
{
string code = @"
from dateutil.parser import parse
date_string = 'Today is January 3, 2047 at 8:21:00AM'
parsed_date = parse(date_string, fuzzy=True)
OUT = parsed_date.day
";
var empty = new ArrayList();
var result = DSPythonNet3Evaluator.EvaluatePythonScript(code, empty, empty);
Assert.That(result, Is.EqualTo(3));
}
[Test]
public void TestPandasAvailable()
{
string code = @"
import pandas as pd
ser = pd.Series(range(5), index=list('abcde'))
OUT = ser['d'].item()
";
var empty = new ArrayList();
var result = DSPythonNet3Evaluator.EvaluatePythonScript(code, empty, empty);
Assert.That(result, Is.EqualTo(3));
}
[Test]
public void TestOpenpyxlAvailable()
{
string code = @"
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
treeData = [['A', 'B', 'C'], [1, 2, 3]]
for row in treeData:
ws.append(row)
OUT = ws['C2'].value
";
var empty = new ArrayList();
var result = DSPythonNet3Evaluator.EvaluatePythonScript(code, empty, empty);
Assert.That(result, Is.EqualTo(3));
}
[Test]
public void TestShapelyAvailable()
{
string code = @"
from shapely.geometry import Point
# Circle-like buffer (radius=1) -> area ~= pi; round to 2dp for stability
area = Point(0,0).buffer(1.0).area
OUT = round(area, 2)
";
var empty = new ArrayList();
var result = DSPythonNet3Evaluator.EvaluatePythonScript(code, empty, empty);
Assert.That(result, Is.EqualTo(3.14));
}
[Test]
public void TestAlphaShapeAvailable()
{
string code = @"
import alphashape
from shapely.geometry import Point
pts = [(0,0), (1,0), (1,1), (0,1), (0.5,0.5)]
alpha = 1.5
poly = alphashape.alphashape(pts, alpha)
OUT = poly.is_valid and poly.area > 0
";
var empty = new ArrayList();
var result = DSPythonNet3Evaluator.EvaluatePythonScript(code, empty, empty);
Assert.That(result, Is.EqualTo(true));
}
[Test]
public void TestScikitLearnAvailable()
{
string code = @"
from sklearn.cluster import KMeans
import numpy as np
X = np.array([[0,0],[0,1],[9,9],[9,8]], dtype=float)
km = KMeans(n_clusters=2, n_init=5, random_state=0).fit(X)
OUT = len(set(km.labels_))
";
var empty = new ArrayList();
var result = DSPythonNet3Evaluator.EvaluatePythonScript(code, empty, empty);
Assert.That(result, Is.EqualTo(2));
}
[Test]
public void TestIfcopenshellAvailable()
{
string code = @"
import ifcopenshell
# Simple smoke: module import + version attribute exists
OUT = hasattr(ifcopenshell, '__version__')
";
var empty = new ArrayList();
var result = DSPythonNet3Evaluator.EvaluatePythonScript(code, empty, empty);
Assert.That(result, Is.EqualTo(true));
}
[Test]
public void TestTabulateAvailable()
{
string code = @"
from tabulate import tabulate
tbl = tabulate([[1,'a'],[2,'b']], headers=['n','c'], tablefmt='plain')
# Expect two data rows -> 2 newline characters (plain format produces 2 lines)
OUT = ('1' in tbl) and ('2' in tbl) and ('a' in tbl) and ('b' in tbl)
";
var empty = new ArrayList();
var result = DSPythonNet3Evaluator.EvaluatePythonScript(code, empty, empty);
Assert.That(result, Is.EqualTo(true));
}
}
}