iterator.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # Copyright David Abrahams 2004. Distributed under the Boost
  2. # Software License, Version 1.0. (See accompanying
  3. # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. from __future__ import print_function
  5. '''
  6. >>> from iterator_ext import *
  7. >>> from input_iterator import *
  8. >>> x = list_int()
  9. >>> x.push_back(1)
  10. >>> x.back()
  11. 1
  12. >>> x.push_back(3)
  13. >>> x.push_back(5)
  14. >>> for y in x:
  15. ... print(y)
  16. 1
  17. 3
  18. 5
  19. >>> z = range(x)
  20. >>> for y in z:
  21. ... print(y)
  22. 1
  23. 3
  24. 5
  25. Range2 wraps a transform_iterator which doubles the elements it
  26. traverses. This proves we can wrap input iterators
  27. >>> z2 = range2(x)
  28. >>> for y in z2:
  29. ... print(y)
  30. 2
  31. 6
  32. 10
  33. >>> l2 = two_lists()
  34. >>> for y in l2.primes:
  35. ... print(y)
  36. 2
  37. 3
  38. 5
  39. 7
  40. 11
  41. 13
  42. >>> for y in l2.evens:
  43. ... print(y)
  44. 2
  45. 4
  46. 6
  47. 8
  48. 10
  49. 12
  50. >>> ll = list_list()
  51. >>> ll.push_back(x)
  52. >>> x.push_back(7)
  53. >>> ll.push_back(x)
  54. >>> for a in ll: #doctest: +NORMALIZE_WHITESPACE
  55. ... for b in a:
  56. ... print(b, end='')
  57. ... print('')
  58. ...
  59. 1 3 5
  60. 1 3 5 7
  61. '''
  62. def run(args = None):
  63. import sys
  64. import doctest
  65. if args is not None:
  66. sys.argv = args
  67. return doctest.testmod(sys.modules.get(__name__))
  68. if __name__ == '__main__':
  69. print("running...")
  70. import sys
  71. status = run()[0]
  72. if (status == 0): print("Done.")
  73. sys.exit(status)