Lichen

lib/__builtins__/tuple.py

580:e703b981b9b1
2017-02-13 Paul Boddie Eliminated redundant struct usage. method-wrapper-for-context
     1 #!/usr/bin/env python     2      3 """     4 Tuple objects.     5      6 Copyright (C) 2015, 2016 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 list_element, list_init, list_len, list_setsize, \    25                    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         size = args is not None and len(args) or 0    39         self.__data__ = list_init(size)    40         list_setsize(self.__data__, size)    41     42         # Populate the tuple.    43     44         if args is not None:    45             i = 0    46             for arg in args:    47                 list_setelement(self.__data__, i, arg)    48                 i += 1    49     50     def __hash__(self):    51     52         "Return a hashable value for the tuple."    53     54         return self._hashvalue(hash)    55     56     def __getslice__(self, start, end=None, step=1):    57     58         """    59         Return a slice starting from 'start', with the optional 'end' and    60         'step'.    61         """    62     63         return tuple(get_using(sequence.__getslice__, self)(start, end, step))    64     65     def __len__(self):    66     67         "Return the length of the tuple."    68     69         return list_len(self.__data__)    70     71     def __add__(self, other): pass    72     73     def __str__(self):    74     75         "Return a string representation."    76     77         return self._str("(", ")")    78     79     __repr__ = __str__    80     81     def __bool__(self):    82     83         "Tuples are true if non-empty."    84     85         return self.__len__() != 0    86     87     def __iter__(self):    88     89         "Return an iterator."    90     91         return itemiterator(self)    92     93     # Special implementation methods.    94     95     def __get_single_item__(self, index):    96     97         "Return the item at the normalised (positive) 'index'."    98     99         self._check_index(index)   100         return list_element(self.__data__, index)   101    102     def __set_single_item__(self, index, value):   103    104         "Set at the normalised (positive) 'index' the given 'value'."   105    106         raise TypeError(self)   107    108 # vim: tabstop=4 expandtab shiftwidth=4