list.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 list_ext import *
  7. >>> new_list()
  8. []
  9. >>> listify((1,2,3))
  10. [1, 2, 3]
  11. >>> letters = listify_string('hello')
  12. >>> letters
  13. ['h', 'e', 'l', 'l', 'o']
  14. >>> X(22)
  15. X(22)
  16. >>> def identity(x):
  17. ... return x
  18. >>> assert apply_object_list(identity, letters) is letters
  19. 5 is not convertible to a list
  20. >>> try: result = apply_object_list(identity, 5)
  21. ... except TypeError: pass
  22. ... else: print('expected an exception, got', result, 'instead')
  23. >>> assert apply_list_list(identity, letters) is letters
  24. 5 is not convertible to a list as a return value
  25. >>> try: result = apply_list_list(len, letters)
  26. ... except TypeError: pass
  27. ... else: print('expected an exception, got', result, 'instead')
  28. >>> append_object(letters, '.')
  29. >>> letters
  30. ['h', 'e', 'l', 'l', 'o', '.']
  31. tuples do not automatically convert to lists when passed as arguments
  32. >>> try: append_list(letters, (1,2))
  33. ... except TypeError: pass
  34. ... else: print('expected an exception')
  35. >>> append_list(letters, [1,2])
  36. >>> letters
  37. ['h', 'e', 'l', 'l', 'o', '.', [1, 2]]
  38. Check that subclass functions are properly called
  39. >>> class mylist(list):
  40. ... def append(self, o):
  41. ... list.append(self, o)
  42. ... if not hasattr(self, 'nappends'):
  43. ... self.nappends = 1
  44. ... else:
  45. ... self.nappends += 1
  46. ...
  47. >>> l2 = mylist()
  48. >>> append_object(l2, 'hello')
  49. >>> append_object(l2, 'world')
  50. >>> l2
  51. ['hello', 'world']
  52. >>> l2.nappends
  53. 2
  54. >>> def printer(*args):
  55. ... for x in args: print( x,)
  56. ... print('')
  57. ...
  58. >>> y = X(42)
  59. >>> exercise(letters, y, printer) #doctest: +NORMALIZE_WHITESPACE
  60. after append:
  61. ['h', 'e', 'l', 'l', 'o', '.', [1, 2], X(42), 5, X(3)]
  62. number of X(42) instances: 1
  63. number of 5s: 1
  64. after extend:
  65. ['h', 'e', 'l', 'l', 'o', '.', [1, 2], X(42), 5, X(3), 'x', 'y', 'z']
  66. index of X(42) is: 7
  67. index of 'l' is: 2
  68. after inserting 666:
  69. ['h', 'e', 'l', 'l', 666, 'o', '.', [1, 2], X(42), 5, X(3), 'x', 'y', 'z']
  70. inserting with object as index:
  71. ['h', 'e', 'l', 'l', 666, '---', 'o', '.', [1, 2], X(42), 5, X(3), 'x', 'y', 'z']
  72. popping...
  73. ['h', 'e', 'l', 'l', 666, '---', 'o', '.', [1, 2], X(42), 5, X(3), 'x', 'y']
  74. ['h', 'e', 'l', 'l', 666, 'o', '.', [1, 2], X(42), 5, X(3), 'x', 'y']
  75. ['h', 'e', 'l', 'l', 666, 'o', '.', [1, 2], X(42), X(3), 'x', 'y']
  76. removing X(42)
  77. ['h', 'e', 'l', 'l', 666, 'o', '.', [1, 2], X(3), 'x', 'y']
  78. removing 666
  79. ['h', 'e', 'l', 'l', 'o', '.', [1, 2], X(3), 'x', 'y']
  80. reversing...
  81. ['y', 'x', X(3), [1, 2], '.', 'o', 'l', 'l', 'e', 'h']
  82. sorted:
  83. ['.', 'e', 'h', 'l', 'l', 'o', 'x', 'y']
  84. reverse sorted:
  85. ['y', 'x', 'o', 'l', 'l', 'h', 'e', '.']
  86. '''
  87. def run(args = None):
  88. import sys
  89. import doctest
  90. if args is not None:
  91. sys.argv = args
  92. return doctest.testmod(sys.modules.get(__name__))
  93. if __name__ == '__main__':
  94. print("running...")
  95. import sys
  96. status = run()[0]
  97. if (status == 0): print("Done.")
  98. sys.exit(status)