You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

125 lines
4.1 KiB

  1. """
  2. A more or less complete user-defined wrapper around tuple objects.
  3. Adapted version of the standard library's UserList.
  4. Taken from Stefan Schwarzer's ftputil library, available at
  5. <http://www.ndh.net/home/sschwarzer/python/python_software.html>,
  6. and used under this license:
  7. Copyright (C) 1999, Stefan Schwarzer
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are
  11. met:
  12. - Redistributions of source code must retain the above copyright
  13. notice, this list of conditions and the following disclaimer.
  14. - Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in the
  16. documentation and/or other materials provided with the distribution.
  17. - Neither the name of the above author nor the names of the
  18. contributors to the software may be used to endorse or promote
  19. products derived from this software without specific prior written
  20. permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
  25. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  26. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. """
  33. # $Id$
  34. # XXX tuple instances (in Python 2.2) contain also:
  35. # __class__, __delattr__, __getattribute__, __hash__, __new__,
  36. # __reduce__, __setattr__, __str__
  37. # What about these?
  38. class UserTuple:
  39. def __init__(self, inittuple=None):
  40. self.data = ()
  41. if inittuple is not None:
  42. # XXX should this accept an arbitrary sequence?
  43. if isinstance(inittuple, tuple):
  44. self.data = inittuple
  45. elif isinstance(inittuple, UserTuple):
  46. # this results in
  47. # self.data is inittuple.data
  48. # but that's ok for tuples because they are
  49. # immutable. (Builtin tuples behave the same.)
  50. self.data = inittuple.data[:]
  51. else:
  52. # the same applies here; (t is tuple(t)) == 1
  53. self.data = tuple(inittuple)
  54. def __repr__(self):
  55. return repr(self.data)
  56. def __lt__(self, other):
  57. return self.data < self.__cast(other)
  58. def __le__(self, other):
  59. return self.data <= self.__cast(other)
  60. def __eq__(self, other):
  61. return self.data == self.__cast(other)
  62. def __ne__(self, other):
  63. return self.data != self.__cast(other)
  64. def __gt__(self, other):
  65. return self.data > self.__cast(other)
  66. def __ge__(self, other):
  67. return self.data >= self.__cast(other)
  68. def __cast(self, other):
  69. if isinstance(other, UserTuple):
  70. return other.data
  71. else:
  72. return other
  73. def __cmp__(self, other):
  74. return (self.data > self.__cast(other)) - (self.data < self.__cast(other))
  75. def __contains__(self, item):
  76. return item in self.data
  77. def __len__(self):
  78. return len(self.data)
  79. def __getitem__(self, i):
  80. return self.data[i]
  81. def __getslice__(self, i, j):
  82. i = max(i, 0)
  83. j = max(j, 0)
  84. return self.__class__(self.data[i:j])
  85. def __add__(self, other):
  86. if isinstance(other, UserTuple):
  87. return self.__class__(self.data + other.data)
  88. elif isinstance(other, type(self.data)):
  89. return self.__class__(self.data + other)
  90. else:
  91. return self.__class__(self.data + tuple(other))
  92. # dir( () ) contains no __radd__ (at least in Python 2.2)
  93. def __mul__(self, n):
  94. return self.__class__(self.data * n)
  95. __rmul__ = __mul__