dtexample.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. """Simple example using doctests.
  2. This file just contains doctests both using plain python and IPython prompts.
  3. All tests should be loaded by nose.
  4. """
  5. import os
  6. def pyfunc():
  7. """Some pure python tests...
  8. >>> pyfunc()
  9. 'pyfunc'
  10. >>> import os
  11. >>> 2+3
  12. 5
  13. >>> for i in range(3):
  14. ... print(i, end=' ')
  15. ... print(i+1, end=' ')
  16. ...
  17. 0 1 1 2 2 3
  18. """
  19. return 'pyfunc'
  20. def ipfunc():
  21. """Some ipython tests...
  22. In [1]: import os
  23. In [3]: 2+3
  24. Out[3]: 5
  25. In [26]: for i in range(3):
  26. ....: print(i, end=' ')
  27. ....: print(i+1, end=' ')
  28. ....:
  29. 0 1 1 2 2 3
  30. It's OK to use '_' for the last result, but do NOT try to use IPython's
  31. numbered history of _NN outputs, since those won't exist under the
  32. doctest environment:
  33. In [7]: 'hi'
  34. Out[7]: 'hi'
  35. In [8]: print(repr(_))
  36. 'hi'
  37. In [7]: 3+4
  38. Out[7]: 7
  39. In [8]: _+3
  40. Out[8]: 10
  41. In [9]: ipfunc()
  42. Out[9]: 'ipfunc'
  43. """
  44. return "ipfunc"
  45. def ipos():
  46. """Examples that access the operating system work:
  47. In [1]: !echo hello
  48. hello
  49. In [2]: !echo hello > /tmp/foo_iptest
  50. In [3]: !cat /tmp/foo_iptest
  51. hello
  52. In [4]: rm -f /tmp/foo_iptest
  53. """
  54. pass
  55. ipos.__skip_doctest__ = os.name == "nt"
  56. def ranfunc():
  57. """A function with some random output.
  58. Normal examples are verified as usual:
  59. >>> 1+3
  60. 4
  61. But if you put '# random' in the output, it is ignored:
  62. >>> 1+3
  63. junk goes here... # random
  64. >>> 1+2
  65. again, anything goes #random
  66. if multiline, the random mark is only needed once.
  67. >>> 1+2
  68. You can also put the random marker at the end:
  69. # random
  70. >>> 1+2
  71. # random
  72. .. or at the beginning.
  73. More correct input is properly verified:
  74. >>> ranfunc()
  75. 'ranfunc'
  76. """
  77. return 'ranfunc'
  78. def random_all():
  79. """A function where we ignore the output of ALL examples.
  80. Examples:
  81. # all-random
  82. This mark tells the testing machinery that all subsequent examples should
  83. be treated as random (ignoring their output). They are still executed,
  84. so if a they raise an error, it will be detected as such, but their
  85. output is completely ignored.
  86. >>> 1+3
  87. junk goes here...
  88. >>> 1+3
  89. klasdfj;
  90. >>> 1+2
  91. again, anything goes
  92. blah...
  93. """
  94. pass
  95. def iprand():
  96. """Some ipython tests with random output.
  97. In [7]: 3+4
  98. Out[7]: 7
  99. In [8]: print('hello')
  100. world # random
  101. In [9]: iprand()
  102. Out[9]: 'iprand'
  103. """
  104. return 'iprand'
  105. def iprand_all():
  106. """Some ipython tests with fully random output.
  107. # all-random
  108. In [7]: 1
  109. Out[7]: 99
  110. In [8]: print('hello')
  111. world
  112. In [9]: iprand_all()
  113. Out[9]: 'junk'
  114. """
  115. return 'iprand_all'