How to use to_python method in Kiwi

Best Python code snippet using Kiwi_python

test_vector_api.py

Source:test_vector_api.py Github

copy

Full Screen

...4def copy(src, result, *args, dest=None):5 if dest is None:6 dest = h.Vector()7 dest.copy(src, *args)8 assert dest.to_python() == result9def vwrite_type(src, vtype):10 f = h.File()11 fname = "vwrite.{}.tmp".format(str(vtype))12 f.wopen(fname)13 src.c().vwrite(f, vtype)14 f.close()15 f.ropen(fname)16 vr = h.Vector(vtype)17 vr.vread(f)18 f.close()19 f.unlink()20 assert src.to_python() == vr.to_python()21def test_vector_api():22 """23 Construction24 """25 vi = h.Vector((1, 2, 3))26 assert vi.to_python() == [1.0, 2.0, 3.0]27 assert vi.get(1) == 2.028 vi.set(1, 2.1)29 assert vi.to_python() == [1.0, 2.1, 3.0]30 del vi31 v = h.Vector(np.array([5, 1, 6], "d"))32 assert v.to_python() == [5.0, 1.0, 6.0]33 v.clear()34 assert v.size() == 035 del v36 v = h.Vector(3)37 assert v.to_python() == [0.0, 0.0, 0.0]38 del v39 v = h.Vector(3, 1)40 assert v.to_python() == [1.0, 1.0, 1.0]41 del v42 assert h.Vector().from_python((1, 2, 3)).to_python() == [1.0, 2.0, 3.0]43 v = h.Vector()44 v.append(3, 3)45 v.append(2)46 v.append(1)47 v.append(5)48 """49 Vector size & capacity50 """51 assert v.to_python() == [3.0, 3.0, 2.0, 1.0, 5.0]52 assert v.size() == 553 v.buffer_size(v.size())54 assert v.buffer_size() >= v.size()55 v.buffer_size(6)56 assert v.buffer_size() >= 657 assert v.to_python() == [3.0, 3.0, 2.0, 1.0, 5.0]58 assert v.eq(v.c())59 """60 Calculations61 """62 assert v.median() == 3.063 assert v.mean() == 2.864 assert v.mean(1, 3) == 2.065 assert np.allclose(v.stdev(), 1.4832396974191326)66 assert np.allclose(v.stdev(0, 3), 0.9574271077563381)67 assert np.allclose(v.stderr(), 0.6633249580710799)68 assert np.allclose(v.stderr(1, 3), 0.5773502691896258)69 assert v.sum() == 14.070 assert v.sum(1, 3) == 6.071 assert np.allclose(72 v.sumgauss(-1, 1, 0.5, 1).to_python(),73 [74 0.05869048145253869,75 0.14879136924715222,76 0.30482687457572216,77 0.5166555071584352,78 ],79 )80 assert np.allclose(81 v.sumgauss(-1, 1, 0.5, 1, h.Vector((1, 3, 2, 5, 4))).to_python(),82 [83 0.2793538745964073,84 0.6861357408871805,85 1.3355688961479038,86 2.0895389620919826,87 ],88 )89 assert np.allclose(90 v.cl().smhist(v.cl(), 1, 3, 2, 1).to_python(),91 [0.9060003240550064, 0.9598574603424295, 0.5071918738793386],92 )93 assert np.allclose(94 v.cl().smhist(v.cl(), 1, 3, 2, 1, h.Vector((1, 3, 2, 5, 4))).to_python(),95 [3.009095149765841, 2.1896697532507994, 1.8126697992388372],96 )97 assert v.sumsq() == 48.098 assert v.sumsq(2, 4) == 30.099 assert v.var() == 2.2100 assert v.var(2, 3) == 0.5101 assert v.min() == 1.0102 assert v.min(0, 2) == 2.0103 assert h.Vector().min() == 0.0104 assert v.min_ind() == 3105 assert v.min_ind(0, 2) == 2106 assert h.Vector().min_ind() == -1.0107 assert v.max() == 5.0108 assert v.max(0, 2) == 3.0109 assert h.Vector().max() == 0.0110 assert v.max_ind() == 4111 assert v.max_ind(0, 2) == 0112 assert h.Vector().max_ind() == -1.0113 assert v.dot(h.Vector((1, 2, 3, 4, 5))) == 44.0114 assert np.allclose(v.mag(), 6.928203230275509)115 assert v.c().reverse().meansqerr(v) == 3.2116 assert v.c().reverse().meansqerr(v, h.Vector((1, 2, 5, 4, 3))) == 8.0117 assert (118 v.c().trigavg(h.Vector((1, 2, 3, 4, 5)), h.Vector((1, 2, 3, 4, 5)), 1, 2) == 2119 )120 """121 Copying122 """123 # vdest.copy(vsrc)124 copy(v, [3.0, 3.0, 2.0, 1.0, 5.0])125 # vdest.copy(vsrc, dest_start)126 copy(v, [0.0, 0.0, 3.0, 3.0, 2.0, 1.0, 5.0], 2)127 # vdest.copy(vsrc, src_start, src_end)128 copy(v, [2.0, 1.0], 2, 3)129 copy(v, [2.0, 1.0, 5.0], 2, -1) # -1 for actual end130 # vdest.copy(vsrc, dest_start, src_start, src_end)131 copy(v, [0.0, 2.0, 1.0], 1, 2, 3)132 # vdest.copy(vsrc, dest_start, src_start, src_end, dest_inc, src_inc)133 copy(v, [0.0, 3.0, 1.0], 1, 1, -1, 1, 2)134 # vdest.copy(vsrc, vsrcdestindex)135 copy(v, [0.0, 3.0, 2.0], h.Vector((1, 2)), dest=h.Vector(3, 0.0))136 # vdest.copy(vsrc, vsrcindex, vdestindex)copy(v, [3.0, 3.0, 2.0, 1.0, 5.0])137 copy(v, [3.0, 2.0, 0.0], h.Vector((1, 2)), h.Vector((0, 1)), dest=h.Vector(3, 0.0))138 copy(v, [3.0], h.Vector((1, 2)), h.Vector((0, 1)), dest=h.Vector(1, 0.0))139 assert v.c().to_python() == v.to_python()140 assert v.c(1).to_python() == [3.0, 2.0, 1.0, 5.0]141 assert v.c(1, 3).to_python() == [3.0, 2.0, 1.0]142 assert v.at().to_python() == v.to_python()143 assert v.at(1).to_python() == [3.0, 2.0, 1.0, 5.0]144 assert v.at(1, 3).to_python() == [3.0, 2.0, 1.0]145 """146 Data morphing and operations147 """148 assert v.resize(4).size() == 4149 assert v.fill(1.0).to_python() == [1.0, 1.0, 1.0, 1.0]150 assert v.fill(1.1, 1, 2).to_python() == [1.0, 1.1, 1.1, 1.0]151 # obj = vsrcdest.indgen()152 assert v.indgen().to_python() == [0.0, 1.0, 2.0, 3.0]153 # obj = vsrcdest.indgen(stepsize)154 assert v.indgen(2).to_python() == [0.0, 2.0, 4.0, 6.0]155 # obj = vsrcdest.indgen(start,stepsize)156 assert v.indgen(2, 5).to_python() == [2.0, 7.0, 12.0, 17.0]157 # obj = vsrcdest.indgen(start,stop,stepsize)158 assert v.indgen(1, 20, 5).to_python() == [1.0, 6.0, 11.0, 16.0]159 assert v.append(h.Vector(1, 17.0), 18.0, 19.0).to_python() == [160 1.0,161 6.0,162 11.0,163 16.0,164 17.0,165 18.0,166 19.0,167 ]168 assert v.insrt(1, 3.0).to_python() == [1.0, 3.0, 6.0, 11.0, 16.0, 17.0, 18.0, 19.0]169 assert v.insrt(3, h.Vector(1, 9.0)).to_python() == [170 1.0,171 3.0,172 6.0,173 9.0,174 11.0,175 16.0,176 17.0,177 18.0,178 19.0,179 ]180 assert v.remove(4).to_python() == [1.0, 3.0, 6.0, 9.0, 16.0, 17.0, 18.0, 19.0]181 assert v.remove(1, 5).to_python() == [1.0, 18.0, 19.0]182 h("double px[3]")183 h.px[0] = 5184 h.px[2] = 2185 assert v.from_double(3, h._ref_px[0]).to_python() == [5.0, 0.0, 2.0]186 a = np.array([5, 1, 6], "d")187 assert v.from_double(3, npyref(a, 0)).to_python() == [5.0, 1.0, 6.0]188 v.indgen(1, 30, 5)189 assert v.to_python() == [1.0, 6.0, 11.0, 16.0, 21.0, 26.0]190 assert v.contains(6.0)191 assert not v.contains(7.0)192 assert h.Vector().where(v, ">=", 10).to_python() == [11.0, 16.0, 21.0, 26.0]193 assert h.Vector().where(v, "<=", 11).to_python() == [1.0, 6.0, 11.0]194 assert h.Vector().where(v, "!=", 11).to_python() == [1.0, 6.0, 16.0, 21.0, 26.0]195 assert h.Vector().where(v, "==", 11).to_python() == [11.0]196 assert h.Vector().where(v, "<", 11).to_python() == [1.0, 6.0]197 assert h.Vector().where(v, ">", 11).to_python() == [16.0, 21.0, 26.0]198 assert h.Vector().where(v, "[)", 9, 21).to_python() == [11.0, 16.0]199 assert h.Vector().where(v, "[]", 9, 21).to_python() == [11.0, 16.0, 21.0]200 assert h.Vector().where(v, "(]", 11, 21).to_python() == [16.0, 21.0]201 assert h.Vector().where(v, "()", 11, 21).to_python() == [16.0]202 assert v.where(">", 1.0).to_python() == [6.0, 11.0, 16.0, 21.0, 26.0]203 assert v.where("[)", 6.0, 26.0).to_python() == [6.0, 11.0, 16.0, 21.0]204 assert v.indwhere(">", 11.0) == 2205 assert v.indwhere("<", 11.0) == 0206 assert v.indwhere("!=", 11.0) == 0207 assert v.indwhere(">=", 11.0) == 1208 assert v.indwhere("<=", 11.0) == 0209 assert v.indwhere("[)", 11.1, 16.0) == -1210 assert v.indwhere("[)", 11.0, 16.0) == 1211 assert v.indwhere("(]", 11.0, 16.0) == 2212 assert v.indwhere("[]", 11.0, 16.0) == 1213 assert v.indwhere("()", 16.0, 11.0) == -1214 assert h.Vector().indvwhere(v, "()", 11, 21).to_python() == [2.0]215 assert h.Vector().indvwhere(v, "==", 11).to_python() == [1.0]216 assert h.Vector().indvwhere(v, "[]", 1, 17).to_python() == [0.0, 1.0, 2.0]217 assert h.Vector().indvwhere(v, "(]", 1, 16).to_python() == [0.0, 1.0, 2.0]218 assert h.Vector().indvwhere(v, "[)", 1, 16).to_python() == [0.0, 1.0]219 assert h.Vector().indvwhere(v, "!=", 11).to_python() == [0.0, 2.0, 3.0]220 assert h.Vector().indvwhere(v, "<", 11).to_python() == [0.0]221 assert h.Vector().indvwhere(v, "<=", 11).to_python() == [0.0, 1.0]222 assert h.Vector().indvwhere(v, ">", 16).to_python() == [3.0]223 assert h.Vector().indvwhere(v, ">=", 16).to_python() == [2.0, 3.0]224 assert v.histogram(1.0, 20.0, 10).to_python() == [0.0, 1.0, 2.0]225 assert h.Vector().hist(v, 1.0, 2.0, 10).to_python() == [1.0, 2.0]226 assert v.ind(h.Vector((1, 3))).to_python() == [11.0, 21.0]227 assert h.Vector().spikebin(v.c(), 12.0).to_python() == [0.0, 0.0, 1.0, 0.0]228 """229 Vector metadata230 """231 assert v.label() == ""232 v.label("v")233 assert v.label() == "v"234 assert v.cl().label() == "v"235 v.label("v2")236 assert v.label() == "v2"237 """238 Transformations239 """240 v = h.Vector((3, 2, 15, 16))241 assert np.allclose(242 v.c().apply("sin").to_python(),243 [244 0.1411200080598672,245 0.9092974268256817,246 0.6502878401571169,247 -0.2879033166650653,248 ],249 )250 assert np.allclose(251 v.c().apply("sin", 1, 2).to_python(),252 [3.0, 0.9092974268256817, 0.6502878401571169, 16.0],253 )254 h("func sq(){return $1*$1}")255 assert np.allclose(v.c().apply("sq").to_python(), [9.0, 4.0, 225.0, 256.0])256 assert v.reduce("sq", 100) == 594.0257 assert h.Vector().deriv(v, 0.1).to_python() == [-10.0, 60.0, 70.0, 10.0]258 assert h.Vector().deriv(v, 1, 1).to_python() == [-1.0, 13.0, 1.0]259 assert h.Vector().deriv(v, 1, 2).to_python() == [-1.0, 6.0, 7.0, 1.0]260 assert np.allclose(261 v.c().interpolate(v.c(), v.c().apply("sqrt")).to_python(),262 [10.384365150689874, 5.097168242109362, 16.0, 16.0],263 )264 assert np.allclose(265 v.c()266 .interpolate(v.c(), v.c().apply("sqrt"), h.Vector((1, 2, 3, 4)))267 .to_python(),268 [2.644951165437683, 2.2382437109314894, 4.0, 4.0],269 )270 assert h.Vector().integral(v).to_python() == [3.0, 5.0, 20.0, 36.0]271 assert np.allclose(272 h.Vector().integral(v, 0.1).to_python(), [3.0, 3.2, 4.7, 6.300000000000001]273 )274 assert v.c().medfltr().to_python() == [3.0, 3.0, 3.0, 3.0]275 assert v.c().medfltr(h.Vector((1, 2, 3, 4))).to_python() == [2.0, 2.0, 2.0, 2.0]276 assert v.c().sort().to_python() == [2.0, 3.0, 15.0, 16.0]277 assert v.sortindex().to_python() == [1.0, 0.0, 2.0, 3.0]278 assert v.c().reverse().to_python() == [16.0, 15.0, 2.0, 3.0]279 assert v.c().rotate(3).to_python() == [2.0, 15.0, 16.0, 3.0]280 assert v.c().rotate(3, 0).to_python() == [0.0, 0.0, 0.0, 3.0]281 assert h.Vector().rebin(v, 2).to_python() == [5.0, 31.0]282 assert v.c().rebin(2).to_python() == [5.0, 31.0]283 assert h.Vector().pow(v, 2).to_python() == [9.0, 4.0, 225.0, 256.0]284 assert np.allclose(v.c().pow(v, 0).to_python(), [1.0, 1.0, 1.0, 1.0])285 assert np.allclose(286 v.c().pow(v, 0.5).to_python(),287 [1.7320508075688772, 1.4142135623730951, 3.872983346207417, 4.0],288 )289 assert np.allclose(290 v.c().pow(v, -1).to_python(),291 [0.3333333333333333, 0.5, 0.06666666666666667, 0.0625],292 )293 assert np.allclose(v.c().pow(v, 1).to_python(), [3.0, 2.0, 15.0, 16.0])294 assert np.allclose(v.c().pow(v, 3).to_python(), [27.0, 8.0, 3375.0, 4096.0])295 assert v.c().pow(2).to_python() == [9.0, 4.0, 225.0, 256.0]296 assert np.allclose(297 h.Vector().sqrt(v).to_python(),298 [1.7320508075688772, 1.4142135623730951, 3.872983346207417, 4.0],299 )300 assert np.allclose(301 v.c().sqrt().to_python(),302 [1.7320508075688772, 1.4142135623730951, 3.872983346207417, 4.0],303 )304 assert np.allclose(305 h.Vector().log(v).to_python(),306 [1.0986122886681098, 0.6931471805599453, 2.70805020110221, 2.772588722239781],307 )308 assert np.allclose(309 v.c().log().to_python(),310 [1.0986122886681098, 0.6931471805599453, 2.70805020110221, 2.772588722239781],311 )312 assert np.allclose(313 h.Vector().log10(v).to_python(),314 [315 0.47712125471966244,316 0.3010299956639812,317 1.1760912590556813,318 1.2041199826559248,319 ],320 )321 assert np.allclose(322 v.c().log10().to_python(),323 [324 0.47712125471966244,325 0.3010299956639812,326 1.1760912590556813,327 1.2041199826559248,328 ],329 )330 assert np.allclose(331 h.Vector().tanh(v).to_python(),332 [333 0.9950547536867305,334 0.9640275800758169,335 0.9999999999998128,336 0.9999999999999747,337 ],338 )339 assert np.allclose(340 v.c().tanh().to_python(),341 [342 0.9950547536867305,343 0.9640275800758169,344 0.9999999999998128,345 0.9999999999999747,346 ],347 )348 assert h.Vector([1.2312414, 3.1231, 5.49554, 6.5000000001]).floor().to_python() == [349 1.0,350 3.0,351 5.0,352 6.0,353 ]354 assert h.Vector([-1.0, -3.0, -5.0, -6.0]).abs().to_python() == [1.0, 3.0, 5.0, 6.0]355 assert v.c().add(h.Vector((1.1, 2.2, 3.3, 4.4))).to_python() == [356 4.1,357 4.2,358 18.3,359 20.4,360 ]361 assert v.c().add(1.3).to_python() == [4.3, 3.3, 16.3, 17.3]362 assert v.c().sub(h.Vector((1.1, 2, 3.3, 4))).to_python() == [1.9, 0.0, 11.7, 12.0]363 assert v.c().sub(1.3).to_python() == [1.7, 0.7, 13.7, 14.7]364 assert v.c().mul(h.Vector((1.5, 2, 3, 4))).to_python() == [4.5, 4.0, 45.0, 64.0]365 assert v.c().mul(2.5).to_python() == [7.5, 5.0, 37.5, 40.0]366 assert v.c().div(h.Vector((1.5, 2, 3, 4))).to_python() == [2.0, 1.0, 5.0, 4.0]367 assert v.c().div(2.5).to_python() == [1.2, 0.8, 6.0, 6.4]368 vs = v.c()369 assert np.allclose(vs.scale(2, 5), 0.21428571428571427)370 assert np.allclose(371 vs.to_python(), [2.2142857142857144, 2.0, 4.785714285714286, 5.0]372 )373 assert np.allclose(374 v.c().sin(1, 1).to_python(),375 [0.8414709848078965, 0.844849172063764, 0.8481940061209319, 0.8515053549310787],376 )377 assert np.allclose(378 v.c().sin(1, 1, 2).to_python(),379 [380 0.8414709848078965,381 0.8481940061209319,382 0.8547830877678237,383 0.8612371892561972,384 ],385 )386 """387 Fourier388 """389 assert v.to_python() == [3.0, 2.0, 15.0, 16.0]390 assert h.Vector(v.size()).correl(v).to_python() == [494.0, 324.0, 154.0, 324.0]391 assert h.Vector(v.size()).convlv(v, v.c().reverse()).to_python() == [392 294.0,393 122.0,394 318.0,395 490.0,396 ]397 assert np.allclose(398 h.Vector(v.size()).convlv(v, v.c().reverse(), -1).to_python(),399 [305.9999866504336, 306.0, 306.0000133495664, 306.0],400 )401 assert v.c().spctrm(v).to_python() == [60.625, 2.0, 15.0, 16.0]402 assert h.Vector(v.size()).filter(v, v.c().reverse()).to_python() == [403 308.0,404 -66.0,405 376.0,406 750.0,407 ]408 assert h.Vector(v.size()).fft(v, -1).to_python() == [17.5, 16.5, -12.5, -15.5]409 assert v.c().fft(-1).to_python() == h.Vector(v.size()).fft(v, -1).to_python()410 """411 I/O412 """413 assert v.to_python() == [3.0, 2.0, 15.0, 16.0]414 f = h.File()415 f.wopen("temp.tmp")416 v.vwrite(f)417 f.close()418 assert v.to_python() == [3.0, 2.0, 15.0, 16.0]419 vr = h.Vector()420 f.ropen("temp.tmp")421 vr.vread(f)422 assert vr.to_python() == v.to_python()423 f.close()424 f.unlink()425 f.wopen("temp.tmp")426 f.printf("%d %d %d %d\n", 3, 2, 15, 16)427 f.close()428 f.ropen("temp.tmp")429 vr.resize(0)430 vr.scanf(f)431 assert vr.to_python() == v.to_python()432 f.seek(0)433 vr2 = h.Vector(4)434 vr2.scanf(f)435 assert vr.to_python() == vr2.to_python()436 f.seek(0)437 vr.resize(0)438 vr.scanf(f, 1)439 assert vr.to_python() == [3.0]440 vr.scanf(f, 1)441 assert vr.to_python() == [2.0]442 vr.resize(0)443 f.seek(0)444 vr.scantil(f, 15.0)445 assert vr.to_python() == [3.0, 2.0]446 f.close()447 f.unlink()448 # Columns449 f.wopen("col.tmp")450 f.printf("%d %d %d %d\n", 3, 2, 15, 16)451 f.printf("%d %d %d %d\n", 6, 9, 7, 21)452 f.printf("%d %d %d %d\n", 1, 4, 5, 22)453 f.printf("%d %d %d %d\n", 3, 8, 14, 23)454 f.close()455 f.ropen("col.tmp")456 vc = h.Vector()457 vc.scanf(f, 3, 2, 4)458 assert vc.to_python() == [2.0, 9.0, 4.0]459 vc.scanf(f, 3, 2, 4)460 assert vc.to_python() == [8.0]461 f.close()462 f.ropen("col.tmp")463 vc = h.Vector()464 vc.scanf(f, 3, 4)465 assert vc.to_python() == [15.0, 7.0, 5.0, 14.0]466 f.seek(0)467 vc.scantil(f, 5.0, 3, 4)468 assert vc.to_python() == [15.0, 7.0]469 vc.printf() # code cov470 vc.printf("%8.4f ")471 vc.printf("%8.4f ", 0, 1)472 f.close()473 f.unlink()474 # Vwrite types475 vwrite_type(h.Vector([1, 2, 3, 4]), 1)476 vwrite_type(h.Vector([4, 3, 2, 1]), 2)477 vwrite_type(h.Vector([4, 5, 6, 7]), 3)478 vwrite_type(h.Vector([7, 8, 9, 10]), 4)479 vwrite_type(h.Vector([0, 1, 2, 33]), 5)480 """481 Random 482 """483 vrand = h.Vector((1, 2, 3))484 r = h.Random()485 r.poisson(12)486 assert vrand.cl().setrand(r).to_python() == [10.0, 16.0, 11.0]487 assert vrand.cl().setrand(r, 1, 2).to_python() == [1.0, 9.0, 18.0]488 assert vrand.cl().addrand(r).to_python() == [9.0, 9.0, 16.0]489 assert vrand.cl().addrand(r, 0, 1).to_python() == [13.0, 16.0, 3.0]490 """491 Misc 492 """493 assert h.Vector().inf(h.Vector((3, 2, 4)), 2, 3, 4, 5, 6, 7).to_python() == [494 4.0,495 5.2,496 4.56,497 ]498 assert h.Vector().resample(h.Vector((3, 2, 4, 6, 7)), 2).to_python() == [499 3.0,500 3.0,501 2.0,502 2.0,503 4.0,504 4.0,505 6.0,506 6.0,507 7.0,508 7.0,509 ]510 assert h.Vector().psth(h.Vector((3, 2, 4, 6, 7, 6, 7, 8)), 1, 2, 3).to_python() == [511 1500.0,512 1500.0,513 2000.0,514 3000.0,515 3500.0,516 3000.0,517 3500.0,518 4000.0,519 ]520 h("func fun () { return ($1 - 0.5) * 2 + ($2 - 0.5) * 2 }")521 dvec = h.Vector(2)522 fvec = h.Vector(2)523 fvec.fill(1)524 ivec = h.Vector(2)525 ivec.indgen()526 a = h.ref(2)527 b = h.ref(1)528 error = dvec.fit(fvec, "fun", ivec, a, b)529 assert np.allclose([error], [1.0005759999999997])530 assert np.allclose(fvec.to_python(), [-0.976, 1.024])531 assert ivec.to_python() == [0.0, 1.0]532 assert dvec.to_python() == [0.0, 0.0]533 aftau = np.array([5, 1, 6, 8], "d")534 error = dvec.fit(535 fvec,536 "exp2",537 ivec,538 npyref(aftau, 0),539 npyref(aftau, 1),540 npyref(aftau, 2),541 npyref(aftau, 3),542 )543 assert np.allclose([error], [8.442756842706686])544 error = dvec.fit(545 fvec,546 "exp1",...

Full Screen

Full Screen

expressions.py

Source:expressions.py Github

copy

Full Screen

...32 if isinstance(expr, Expression):33 if isinstance(expr, ScriptOp) and not isinstance(expr.script, text_type):34 return expr.script35 else:36 return compile_expression(expr.to_python())37 if expr != None and not isinstance(expr, (Mapping, list)) and hasattr(expr, "__call__"):38 return expr39 return compile_expression(jx_expression(expr).to_python())40@extend(Variable)41def to_python(self, not_null=False, boolean=False, many=False):42 path = split_field(self.var)43 agg = "row"44 if not path:45 return agg46 elif path[0] in ["row", "rownum"]:47 # MAGIC VARIABLES48 agg = path[0]49 path = path[1:]50 if len(path) == 0:51 return agg52 elif path[0] == "rows":53 if len(path) == 1:54 return "rows"55 elif path[1] in ["first", "last"]:56 agg = "rows." + path[1] + "()"57 path = path[2:]58 else:59 Log.error("do not know what {{var}} of `rows` is", var=path[1])60 for p in path[:-1]:61 if not_null:62 agg = agg + ".get(" + convert.value2quote(p) + ")"63 else:64 agg = agg + ".get(" + convert.value2quote(p) + ", EMPTY_DICT)"65 output = agg + ".get(" + convert.value2quote(path[-1]) + ")"66 if many:67 output = "listwrap(" + output + ")"68 return output69@extend(OffsetOp)70def to_python(self, not_null=False, boolean=False, many=False):71 return "row[" + text_type(self.var) + "] if 0<=" + text_type(self.var) + "<len(row) else None"72@extend(RowsOp)73def to_python(self, not_null=False, boolean=False, many=False):74 agg = "rows[rownum+" + self.offset.to_python() + "]"75 path = split_field(json2value(self.var.json))76 if not path:77 return agg78 for p in path[:-1]:79 agg = agg + ".get(" + convert.value2quote(p) + ", EMPTY_DICT)"80 return agg + ".get(" + convert.value2quote(path[-1]) + ")"81@extend(GetOp)82def to_python(self, not_null=False, boolean=False, many=False):83 obj = self.var.to_python()84 code = self.offset.to_python()85 return "listwrap("+obj+")[" + code + "]"86@extend(LastOp)87def to_python(self, not_null=False, boolean=False, many=False):88 term = self.term.to_python()89 return "listwrap(" + term + ").last()"90@extend(SelectOp)91def to_python(self, not_null=False, boolean=False, many=False):92 return (93 "wrap_leaves({" +94 ','.join(95 quote(t['name']) + ":" + t['value'].to_python() for t in self.terms96 ) +97 "})"98 )99@extend(ScriptOp)100def to_python(self, not_null=False, boolean=False, many=False):101 return self.script102@extend(Literal)103def to_python(self, not_null=False, boolean=False, many=False):104 return text_type(repr(unwrap(json2value(self.json))))105@extend(NullOp)106def to_python(self, not_null=False, boolean=False, many=False):107 return "None"108@extend(TrueOp)109def to_python(self, not_null=False, boolean=False, many=False):110 return "True"111@extend(FalseOp)112def to_python(self, not_null=False, boolean=False, many=False):113 return "False"114@extend(DateOp)115def to_python(self, not_null=False, boolean=False, many=False):116 return text_type(Date(self.value).unix)117@extend(TupleOp)118def to_python(self, not_null=False, boolean=False, many=False):119 if len(self.terms) == 0:120 return "tuple()"121 elif len(self.terms) == 1:122 return "(" + self.terms[0].to_python() + ",)"123 else:124 return "(" + (','.join(t.to_python() for t in self.terms)) + ")"125@extend(LeavesOp)126def to_python(self, not_null=False, boolean=False, many=False):127 return "Data(" + self.term.to_python() + ").leaves()"128@extend(BinaryOp)129def to_python(self, not_null=False, boolean=False, many=False):130 return "(" + self.lhs.to_python() + ") " + BinaryOp.operators[self.op] + " (" + self.rhs.to_python() + ")"131@extend(InequalityOp)132def to_python(self, not_null=False, boolean=False, many=False):133 return "(" + self.lhs.to_python() + ") " + InequalityOp.operators[self.op] + " (" + self.rhs.to_python() + ")"134@extend(InOp)135def to_python(self, not_null=False, boolean=False, many=False):136 return self.value.to_python() + " in " + self.superset.to_python(many=True)137@extend(DivOp)138def to_python(self, not_null=False, boolean=False, many=False):139 return "None if (" + self.missing().to_python() + ") else (" + self.lhs.to_python(140 not_null=True) + ") / (" + self.rhs.to_python(not_null=True) + ")"141@extend(FloorOp)142def to_python(self, not_null=False, boolean=False, many=False):143 return "Math.floor(" + self.lhs.to_python() + ", " + self.rhs.to_python() + ")"144@extend(EqOp)145def to_python(self, not_null=False, boolean=False, many=False):146 return "(" + self.rhs.to_python() + ") in listwrap(" + self.lhs.to_python() + ")"147@extend(NeOp)148def to_python(self, not_null=False, boolean=False, many=False):149 lhs = self.lhs.to_python()150 rhs = self.rhs.to_python()151 return "((" + lhs + ") != None and (" + rhs + ") != None and (" + lhs + ") != (" + rhs + "))"152@extend(NotOp)153def to_python(self, not_null=False, boolean=False, many=False):154 return "not (" + self.term.to_python(boolean=True) + ")"155@extend(AndOp)156def to_python(self, not_null=False, boolean=False, many=False):157 if not self.terms:158 return "True"159 else:160 return " and ".join("(" + t.to_python() + ")" for t in self.terms)161@extend(OrOp)162def to_python(self, not_null=False, boolean=False, many=False):163 return " or ".join("(" + t.to_python() + ")" for t in self.terms)164@extend(LengthOp)165def to_python(self, not_null=False, boolean=False, many=False):166 value = self.term.to_python()167 return "len(" + value + ") if (" + value + ") != None else None"168@extend(NumberOp)169def to_python(self, not_null=False, boolean=False, many=False):170 test = self.term.missing().to_python(boolean=True)171 value = self.term.to_python(not_null=True)172 return "float(" + value + ") if (" + test + ") else None"173@extend(StringOp)174def to_python(self, not_null=False, boolean=False, many=False):175 missing = self.term.missing().to_python(boolean=True)176 value = self.term.to_python(not_null=True)177 return "null if (" + missing + ") else text_type(" + value + ")"178@extend(CountOp)179def to_python(self, not_null=False, boolean=False, many=False):180 return "+".join("(0 if (" + t.missing().to_python(boolean=True) + ") else 1)" for t in self.terms)181@extend(MaxOp)182def to_python(self, not_null=False, boolean=False, many=False):183 return "max(["+(','.join(t.to_python() for t in self.terms))+"])"184_python_operators = {185 "add": (" + ", "0"), # (operator, zero-array default value) PAIR186 "sum": (" + ", "0"),187 "mul": (" * ", "1"),188 "mult": (" * ", "1"),189 "multiply": (" * ", "1")190}191@extend(MultiOp)192def to_python(self, not_null=False, boolean=False, many=False):193 if len(self.terms) == 0:194 return self.default.to_python()195 elif self.default is NULL:196 return _python_operators[self.op][0].join("(" + t.to_python() + ")" for t in self.terms)197 else:198 return "coalesce(" + _python_operators[self.op][0].join("(" + t.to_python() + ")" for t in self.terms) + ", " + self.default.to_python() + ")"199@extend(RegExpOp)200def to_python(self, not_null=False, boolean=False, many=False):201 return "re.match(" + quote(json2value(self.pattern.json) + "$") + ", " + self.var.to_python() + ")"202@extend(CoalesceOp)203def to_python(self, not_null=False, boolean=False, many=False):204 return "coalesce(" + (', '.join(t.to_python() for t in self.terms)) + ")"205@extend(MissingOp)206def to_python(self, not_null=False, boolean=False, many=False):207 return self.expr.to_python() + " == None"208@extend(ExistsOp)209def to_python(self, not_null=False, boolean=False, many=False):210 return self.field.to_python() + " != None"211@extend(PrefixOp)212def to_python(self, not_null=False, boolean=False, many=False):213 return "(" + self.field.to_python() + ").startswith(" + self.prefix.to_python() + ")"214@extend(SuffixOp)215def to_python(self, not_null=False, boolean=False, many=False):216 return "(" + self.field.to_python() + ").endswith(" + self.suffix.to_python() + ")"217@extend(ConcatOp)218def to_python(self, not_null=False, boolean=False, many=False):219 v = self.value.to_python()220 l = self.length.to_python()221 return "None if " + v + " == None or " + l + " == None else " + v + "[0:max(0, " + l + ")]"222@extend(NotLeftOp)223def to_python(self, not_null=False, boolean=False, many=False):224 v = self.value.to_python()225 l = self.length.to_python()226 return "None if " + v + " == None or " + l + " == None else " + v + "[max(0, " + l + "):]"227@extend(RightOp)228def to_python(self, not_null=False, boolean=False, many=False):229 v = self.value.to_python()230 l = self.length.to_python()231 return "None if " + v + " == None or " + l + " == None else " + v + "[max(0, len(" + v + ")-(" + l + ")):]"232@extend(NotRightOp)233def to_python(self, not_null=False, boolean=False, many=False):234 v = self.value.to_python()235 l = self.length.to_python()236 return "None if " + v + " == None or " + l + " == None else " + v + "[0:max(0, len(" + v + ")-(" + l + "))]"237@extend(SplitOp)238def to_python(self, not_null=False, boolean=False, many=False):239 return "(" + self.value.to_python() + ").split(" + self.find.to_python() + ")"240@extend(FindOp)241def to_python(self, not_null=False, boolean=False, many=False):242 return "((" + quote(self.substring) + " in " + self.var.to_python() + ") if " + self.var.to_python() + "!=None else False)"243@extend(BetweenOp)244def to_python(self, not_null=False, boolean=False, many=False):245 return self.value.to_python() + " in " + self.superset.to_python(many=True)246@extend(RangeOp)247def to_python(self, not_null=False, boolean=False, many=False):248 return "(" + self.then.to_python(not_null=not_null) + ") if (" + self.when.to_python(249 boolean=True) + ") else (" + self.els_.to_python(not_null=not_null) + ")"250@extend(CaseOp)251def to_python(self, not_null=False, boolean=False, many=False):252 acc = self.whens[-1].to_python()253 for w in reversed(self.whens[0:-1]):254 acc = "(" + w.then.to_python() + ") if (" + w.when.to_python(boolean=True) + ") else (" + acc + ")"255 return acc256@extend(WhenOp)257def to_python(self, not_null=False, boolean=False, many=False):...

Full Screen

Full Screen

test_fields.py

Source:test_fields.py Github

copy

Full Screen

...10 number = long11 return number12 def test_boolean_field(self):13 boolean_field = BooleanField()14 self.assertTrue(boolean_field.to_python('1'))15 self.assertTrue(boolean_field.to_python('True'))16 self.assertTrue(boolean_field.to_python('true'))17 self.assertFalse(boolean_field.to_python('0'))18 self.assertFalse(boolean_field.to_python('False'))19 self.assertFalse(boolean_field.to_python('false'))20 self.assertFalse(boolean_field.to_python(None))21 def test_long_field(self):22 long_field = LongField()23 number = self.__get_long_type()24 self.assertEqual(long_field.to_python('1'), number(1))25 self.assertEqual(long_field.to_python('0'), number(0))26 self.assertEqual(long_field.to_python('-1'), number(-1))27 def test_int_field(self):28 int_field = IntField()29 self.assertEqual(int_field.to_python('1'), int(1))30 self.assertEqual(int_field.to_python('0'), int(0))31 self.assertEqual(int_field.to_python('-1'), int(-1))32 def test_float_field(self):33 float_field = FloatField()34 self.assertEqual(float_field.to_python('1'), float(1))35 self.assertEqual(float_field.to_python('1.1231'), float(1.1231))36 self.assertEqual(float_field.to_python('0'), float(0))37 self.assertEqual(float_field.to_python('-1'), float(-1))38 self.assertEqual(float_field.to_python('-1.1'), float(-1.1))39 def test_string_field(self):40 string_field = StringField()41 texts = ['str1', 'str2', 'str3', '1', '0']42 for text in texts:43 self.assertEqual(string_field.to_python(text), text)44 def test_list_field(self):45 list_field = ListField()46 expected_vals = [1, 2, 3.2, 'test']47 values = list_field.to_python(expected_vals)48 for index, expected_val in enumerate(expected_vals):49 self.assertEqual(values[index], expected_val)50 def test_boolean_list_field(self):51 list_field = ListField(BooleanField())52 input_vals = ['1', '2', '3.2', 'test', '0', 'false']53 expected_vals = [True, True, True, True, False, False]54 values = list_field.to_python(input_vals)55 for index, value in enumerate(values):56 self.assertEqual(value, expected_vals[index])57 self.assertTrue(isinstance(value, bool))58 def test_int_list_field(self):59 list_field = ListField(IntField())60 input_vals = ['1', '2', '3', '0', '-1' , '-32']61 expected_vals = [1, 2, 3, 0, -1, -32]62 values = list_field.to_python(input_vals)63 64 for index, value in enumerate(values):65 self.assertEqual(value, expected_vals[index])66 self.assertTrue(isinstance(value, int))67 def test_long_list_field(self):68 list_field = ListField(LongField())69 number = self.__get_long_type()70 input_vals = ['1', '2', '3', '0', '-1' , '-32']71 expected_vals = [1, 2, 3, 0, -1, -32]72 values = list_field.to_python(input_vals)73 74 for index, value in enumerate(values):75 self.assertEqual(value, expected_vals[index])76 self.assertTrue(isinstance(value, number))77 78 def test_string_list_field(self):79 list_field = ListField(StringField())80 input_vals = ['test1', 'test2', '']81 expected_vals = input_vals82 values = list_field.to_python(input_vals)83 84 for index, value in enumerate(values):85 self.assertEqual(value, expected_vals[index])86 self.assertTrue(isinstance(value, str))87 88if __name__ == '__main__':...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Kiwi automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful