|
6 | 6 |
|
7 | 7 | import gnuplotlib as gp |
8 | 8 |
|
| 9 | + |
| 10 | +# some simple infrastructure |
| 11 | +def print_red(x): |
| 12 | + """print the message in red""" |
| 13 | + sys.stdout.write("\x1b[31m" + x + "\x1b[0m\n") |
| 14 | +def print_green(x): |
| 15 | + """Print the message in green""" |
| 16 | + sys.stdout.write("\x1b[32m" + x + "\x1b[0m\n") |
| 17 | +def check_expected_error(what, f): |
| 18 | + sys.stderr.write(what + '\n') |
| 19 | + sys.stderr.write("=================================\n") |
| 20 | + try: |
| 21 | + f() |
| 22 | + except gp.GnuplotlibError as e: |
| 23 | + print_green("OK! Got err I was supposed to get:\n[[[[[[[\n{}\n]]]]]]]".format(e)) |
| 24 | + except Exception as e: |
| 25 | + print_red("ERROR! Got some other error I was NOT supposed to get: {}".format(e)) |
| 26 | + else: |
| 27 | + print_red("ERROR! An error was supposed to be reported but it was not") |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | + |
9 | 32 | # data I use for 2D testing |
10 | 33 | x = np.arange(21) - 10 |
11 | 34 |
|
|
22 | 45 |
|
23 | 46 |
|
24 | 47 |
|
25 | | - |
26 | | - |
27 | | - |
28 | 48 | ################################# |
29 | 49 | # Now the demos! |
30 | 50 | ################################# |
|
345 | 365 |
|
346 | 366 | sys.stderr.write("\n\n\n") |
347 | 367 | sys.stderr.write("==== Testing error detection ====\n") |
348 | | -sys.stderr.write('I should complain about an invalid "with":\n') |
349 | | -sys.stderr.write("=================================\n") |
350 | | -try: |
351 | | - gp.plot(np.arange(5), _with = 'bogusstyle') |
352 | | -except gp.GnuplotlibError as e: |
353 | | - print("OK! Got err I was supposed to get:\n[[[[[[[\n{}\n]]]]]]]\n".format(e)) |
354 | | -except: |
355 | | - print("ERROR! Got some other error I was NOT supposed to get\n") |
356 | | -else: |
357 | | - print("ERROR! An error was supposed to be reported but it was not\n") |
358 | | - |
359 | | -sys.stderr.write('gnuplotlib can detect I/O hangs. Here I ask for a delay, so I should detect this and quit after a few seconds...\n') |
360 | | -sys.stderr.write("=================================\n") |
361 | | -try: |
362 | | - gp.plot( np.arange(5), cmds = 'pause 20' ) |
363 | | -except gp.GnuplotlibError as e: |
364 | | - print("OK! Got err I was supposed to get:\n[[[[[[[\n{}\n]]]]]]]\n".format(e)) |
365 | | -except Exception as e: |
366 | | - print("ERROR! Got some other error I was NOT supposed to get: {}\n".format(e)) |
367 | | -else: |
368 | | - print("ERROR! An error was supposed to be reported but it was not\n") |
| 368 | + |
| 369 | +check_expected_error('I should complain about an invalid "with"', |
| 370 | + lambda: gp.plot(np.arange(5), _with = 'bogusstyle')) |
| 371 | + |
| 372 | +check_expected_error('Error detection in multiplots', |
| 373 | + lambda: gp.plot( (x**2,), |
| 374 | + (-x, x**3), |
| 375 | + ( rho, |
| 376 | + 1./np.cos(rho) + a*np.cos(rho), # broadcasted. dim=(7,1000) |
| 377 | + |
| 378 | + dict( _with = 'lines', |
| 379 | + set = 'poflar', |
| 380 | + square = True, |
| 381 | + yrange = [-5,5], |
| 382 | + legend = a.ravel())), |
| 383 | + (x_3d, y_3d, z_3d, |
| 384 | + dict( _with = 'points', |
| 385 | + title = 'sphere', |
| 386 | + square = True, |
| 387 | + legend = 'sphere', |
| 388 | + _3d = True)), |
| 389 | + wait=1, |
| 390 | + multiplot='title "basic multiplot" layout 2,2', ) ) |
| 391 | + |
| 392 | +check_expected_error('gnuplotlib can detect I/O hangs. Here I ask for a delay, so I should detect this and quit after a few seconds...', |
| 393 | + lambda: gp.plot( np.arange(5), cmds = 'pause 20' )) |
0 commit comments