Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. # Python program that can be executed to report whether particular
  2. # python packages are available on the system.
  3.  
  4. import math
  5. import os
  6. import sys
  7.  
  8.  
  9. def test_is_python_35():
  10. major = sys.version_info.major
  11. minor = sys.version_info.minor
  12. if major == 3:
  13. pass
  14. else:
  15. print("You are running Python {}, but we need Python {}.".format(major, 3))
  16. print("Download and install the Anaconda distribution for Python 3.")
  17. print("Stopping here.")
  18.  
  19. # Let's stop here
  20. sys.exit(1)
  21. return None
  22. # assert major == 3, "Stopping here - we need Python 3."
  23.  
  24. if minor >= 5:
  25. print("Testing Python version-> py{}.{} OK".format(major, minor))
  26. else:
  27. print("Warning: You should be running Python 3.5 or newer, " +
  28. "you have Python {}.{}.".format(major, minor))
  29.  
  30.  
  31. def test_numpy():
  32. try:
  33. import numpy as np
  34. except ImportError:
  35. print("Could not import numpy -> numpy failed")
  36. return None
  37. # Simple test
  38. a = np.arange(0, 100, 1)
  39. assert np.sum(a) == sum(a)
  40. print("Testing numpy... -> numpy OK")
  41.  
  42.  
  43. def test_scipy():
  44. try:
  45. import scipy
  46. except ImportError:
  47. print("Could not import 'scipy' -> scipy failed")
  48. return None
  49. # Simple test
  50. import scipy.integrate
  51. assert abs(scipy.integrate.quad(lambda x: x * x, 0, 6)[0] - 72.0) < 1e-6
  52. print("Testing scipy ... -> scipy OK")
  53.  
  54.  
  55. def test_pylab():
  56. """Actually testing matplotlib, as pylab is part of matplotlib."""
  57. try:
  58. import pylab
  59. except ImportError:
  60. print("Could not import 'matplotlib/pylab' -> failed")
  61. return None
  62. # Creata plot for testing purposes
  63. xvalues = [i * 0.1 for i in range(100)]
  64. yvalues = [math.sin(x) for x in xvalues]
  65. pylab.plot(xvalues, yvalues, "-o", label="sin(x)")
  66. pylab.legend()
  67. pylab.xlabel('x')
  68. testfilename='pylab-testfigure.png'
  69.  
  70. # check that file does not exist yet:
  71. if os.path.exists(testfilename):
  72. print("Skipping plotting to file as file {} exists already."\
  73. .format(testfilename))
  74. else:
  75. # Write plot to file
  76. pylab.savefig(testfilename)
  77. # Then check that file exists
  78. assert os.path.exists(testfilename)
  79. print("Testing matplotlib... -> pylab OK")
  80. os.remove(testfilename)
  81.  
  82.  
  83. def test_sympy():
  84. try:
  85. import sympy
  86. except ImportError:
  87. print("Could not import 'sympy' -> fail")
  88. return None
  89. # simple test
  90. x = sympy.Symbol('x')
  91. my_f = x ** 2
  92. assert sympy.diff(my_f,x) == 2 * x
  93. print("Testing sympy -> sympy OK")
  94.  
  95.  
  96. def test_pytest():
  97. try:
  98. import pytest
  99. except ImportError:
  100. print("Could not import 'pytest' -> fail")
  101. return None
  102. print("Testing pytest -> pytest OK")
  103.  
  104.  
  105. if __name__ == "__main__":
  106.  
  107. print("Running using Python {}".format(sys.version))
  108. test_is_python_35()
  109. test_numpy()
  110. test_scipy()
  111. test_pylab()
  112. test_sympy()
  113. test_pytest()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement