i found code somewhere checks if there packages installed.
# python program can executed report whether particular # python packages available on system. import os import math import sys def test_numpy(): try: import numpy np except importerror: print("could not import numpy -> numpy failed") return none # simple test = np.arange(0, 100, 1) assert np.sum(a) == sum(a) print("-> numpy ok") def test_scipy(): try: import scipy except importerror: print("could not import 'scipy' -> scipy failed") return none # simple test import scipy.integrate assert abs(scipy.integrate.quad(lambda x: x * x, 0, 6)[0] - 72.0) < 1e-6 print("-> scipy ok") def test_pylab(): """actually testing matplotlib, pylab part of matplotlib.""" try: import pylab except importerror: print("could not import 'matplotlib/pylab' -> failed") return none # creata plot testing purposes xvalues = [i * 0.1 in range(100)] yvalues = [math.sin(x) x in xvalues] pylab.plot(xvalues, yvalues, "-o", label="sin(x)") pylab.legend() pylab.xlabel('x') testfilename='pylab-testfigure.png' # check file not exist yet: if os.path.exists(testfilename): print("skipping plotting file file {} exists already."\ .format(testfilename)) else: # write plot file pylab.savefig(testfilename) # check file exists assert os.path.exists(testfilename) print("-> pylab ok") os.remove(testfilename) def test_sympy(): try: import sympy except importerror: print("could not import 'sympy' -> fail") return none # simple test x = sympy.symbol('x') my_f = x ** 2 assert sympy.diff(my_f,x) == 2 * x print("-> sympy ok") def test_pytest(): try: import pytest except importerror: print("could not import 'pytest' -> fail") return none print("-> pytest ok") if __name__ == "__main__": print("running using python {}".format(sys.version)) print("testing numpy... "), test_numpy() print("testing scipy... "), test_scipy() print("testing matplotlib..."), test_pylab() print("testing sympy... "), test_sympy() print("testing pytest... "), test_pytest()
i don't understand why there tests ?
without tests can assert libraries there no exceptions thrown, right ?
can remove tests , expect correct results regarding package installations ?
like:
def test_numpy(): try: import numpy np except importerror: print("could not import numpy -> numpy failed") return none
also, test supposed work in every kind of python distribution anaconda etc ?
does depend upon version of python using?
thanks lot help.
Comments
Post a Comment