Lichen

lib/__builtins__/tuple.py

794:7dd8e4815848
2017-03-31 Paul Boddie Introduced a multiple outcome result which can be used to provide a choice of return values to supply the initialiser and alias details.
     1 #!/usr/bin/env python     2      3 """     4 Tuple objects.     5      6 Copyright (C) 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk>     7      8 This program is free software; you can redistribute it and/or modify it under     9 the terms of the GNU General Public License as published by the Free Software    10 Foundation; either version 3 of the License, or (at your option) any later    11 version.    12     13 This program is distributed in the hope that it will be useful, but WITHOUT    14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS    15 FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more    16 details.    17     18 You should have received a copy of the GNU General Public License along with    19 this program.  If not, see <http://www.gnu.org/licenses/>.    20 """    21     22 from __builtins__.iteration.iterator import itemiterator    23 from __builtins__.sequence import hashable, sequence    24 from native import tuple_init, \    25                    list_element, list_len, list_setsize, list_setelement    26     27 class tuple(sequence, hashable):    28     29     "Implementation of tuple."    30     31     def __init__(self, args=None):    32     33         "Initialise the tuple."    34     35         # Reserve an attribute for a fragment reference along with some space    36         # for elements.    37     38         if args is None:    39             size = 0    40         else:    41             size = args.__len__()    42     43         self.__data__ = tuple_init(size)    44     45         if size:    46             list_setsize(self.__data__, size)    47     48             # Populate the tuple.    49     50             i = 0    51             while i < size:    52                 list_setelement(self.__data__, i, args[i])    53                 i += 1    54     55     def __hash__(self):    56     57         "Return a hashable value for the tuple."    58     59         return self._hashvalue(hash)    60     61     def __getslice__(self, start, end=None, step=1):    62     63         """    64         Return a slice starting from 'start', with the optional 'end' and    65         'step'.    66         """    67     68         return tuple(get_using(sequence.__getslice__, self)(start, end, step))    69     70     def __len__(self):    71     72         "Return the length of the tuple."    73     74         return list_len(self.__data__)    75     76     def __add__(self, other): pass    77     78     def __str__(self):    79     80         "Return a string representation."    81     82         return self._str("(", ")")    83     84     __repr__ = __str__    85     86     def __bool__(self):    87     88         "Tuples are true if non-empty."    89     90         return self.__len__() != 0    91     92     def __iter__(self):    93     94         "Return an iterator."    95     96         return itemiterator(self)    97     98     # Special implementation methods.    99    100     def __get_single_item__(self, index):   101    102         "Return the item at the normalised (positive) 'index'."   103    104         self._check_index(index)   105         return list_element(self.__data__, index)   106    107     def __set_single_item__(self, index, value):   108    109         "Set at the normalised (positive) 'index' the given 'value'."   110    111         raise TypeError   112    113 # vim: tabstop=4 expandtab shiftwidth=4