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