Lichen

lib/__builtins__/tuple.py

775:c8ba74a474eb
2017-03-25 Paul Boddie Fixed method name.
     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         self.__data__ = tuple_init(size)    39     40         if args is None:    41             size = 0    42         else:    43             size = args.__len__()    44             list_setsize(self.__data__, size)    45     46             # Populate the tuple.    47     48             i = 0    49             while i < size:    50                 list_setelement(self.__data__, i, args[i])    51                 i += 1    52     53     def __hash__(self):    54     55         "Return a hashable value for the tuple."    56     57         return self._hashvalue(hash)    58     59     def __getslice__(self, start, end=None, step=1):    60     61         """    62         Return a slice starting from 'start', with the optional 'end' and    63         'step'.    64         """    65     66         return tuple(get_using(sequence.__getslice__, self)(start, end, step))    67     68     def __len__(self):    69     70         "Return the length of the tuple."    71     72         return list_len(self.__data__)    73     74     def __add__(self, other): pass    75     76     def __str__(self):    77     78         "Return a string representation."    79     80         return self._str("(", ")")    81     82     __repr__ = __str__    83     84     def __bool__(self):    85     86         "Tuples are true if non-empty."    87     88         return self.__len__() != 0    89     90     def __iter__(self):    91     92         "Return an iterator."    93     94         return itemiterator(self)    95     96     # Special implementation methods.    97     98     def __get_single_item__(self, index):    99    100         "Return the item at the normalised (positive) 'index'."   101    102         self._check_index(index)   103         return list_element(self.__data__, index)   104    105     def __set_single_item__(self, index, value):   106    107         "Set at the normalised (positive) 'index' the given 'value'."   108    109         raise TypeError   110    111 # vim: tabstop=4 expandtab shiftwidth=4