paul@6 | 1 | #!/usr/bin/env python |
paul@6 | 2 | |
paul@6 | 3 | """ |
paul@6 | 4 | List 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@290 | 22 | from __builtins__.iterator import itemiterator |
paul@224 | 23 | from __builtins__.sequence import sequence |
paul@140 | 24 | import native |
paul@6 | 25 | |
paul@224 | 26 | class list(sequence): |
paul@6 | 27 | |
paul@6 | 28 | "Implementation of list." |
paul@6 | 29 | |
paul@6 | 30 | def __init__(self, args=None): |
paul@6 | 31 | |
paul@6 | 32 | "Initialise the list." |
paul@6 | 33 | |
paul@206 | 34 | # Reserve an attribute for a fragment reference along with some space |
paul@206 | 35 | # for elements. |
paul@159 | 36 | |
paul@227 | 37 | self.__data__ = native._list_init(args is not None and len(args) or 0) |
paul@159 | 38 | |
paul@6 | 39 | if args is not None: |
paul@6 | 40 | self.extend(args) |
paul@6 | 41 | |
paul@6 | 42 | def __delitem__(self, index): pass |
paul@6 | 43 | def __setslice__(self, start, end, slice): pass |
paul@6 | 44 | def __delslice__(self, start, end): pass |
paul@206 | 45 | |
paul@206 | 46 | def append(self, value): |
paul@206 | 47 | |
paul@206 | 48 | "Append 'value' to the list." |
paul@206 | 49 | |
paul@206 | 50 | native._list_append(self, value) |
paul@206 | 51 | |
paul@6 | 52 | def insert(self, i, value): pass |
paul@6 | 53 | |
paul@6 | 54 | def extend(self, iterable): |
paul@6 | 55 | |
paul@6 | 56 | "Extend the list with the contents of 'iterable'." |
paul@6 | 57 | |
paul@6 | 58 | for i in iterable: |
paul@6 | 59 | self.append(i) |
paul@6 | 60 | |
paul@6 | 61 | def pop(self): pass |
paul@6 | 62 | def reverse(self): pass |
paul@6 | 63 | def sort(self, cmp=None, key=None, reverse=0): pass |
paul@140 | 64 | |
paul@140 | 65 | def __len__(self): |
paul@140 | 66 | |
paul@140 | 67 | "Return the length of the list." |
paul@140 | 68 | |
paul@140 | 69 | return native._list_len(self) |
paul@140 | 70 | |
paul@6 | 71 | def __add__(self, other): pass |
paul@206 | 72 | |
paul@206 | 73 | def __iadd__(self, other): |
paul@206 | 74 | |
paul@206 | 75 | "Concatenate 'other' to the list." |
paul@206 | 76 | |
paul@206 | 77 | if isinstance(other, list): |
paul@206 | 78 | native._list_concat(self, other) |
paul@206 | 79 | else: |
paul@206 | 80 | self.extend(other) |
paul@206 | 81 | return self |
paul@198 | 82 | |
paul@198 | 83 | def __str__(self): |
paul@198 | 84 | |
paul@227 | 85 | "Return a string representation." |
paul@198 | 86 | |
paul@227 | 87 | return self._str("[", "]") |
paul@6 | 88 | |
paul@222 | 89 | __repr__ = __str__ |
paul@222 | 90 | |
paul@6 | 91 | def __bool__(self): |
paul@6 | 92 | |
paul@6 | 93 | "Lists are true if non-empty." |
paul@6 | 94 | |
paul@140 | 95 | return native._list_nonempty(self) |
paul@6 | 96 | |
paul@6 | 97 | def __iter__(self): |
paul@6 | 98 | |
paul@6 | 99 | "Return an iterator." |
paul@6 | 100 | |
paul@290 | 101 | return itemiterator(self) |
paul@6 | 102 | |
paul@6 | 103 | # Special implementation methods. |
paul@6 | 104 | |
paul@140 | 105 | def __get_single_item__(self, index): |
paul@140 | 106 | |
paul@194 | 107 | "Return the item at the normalised (positive) 'index'." |
paul@194 | 108 | |
paul@265 | 109 | self._check_index(index) |
paul@140 | 110 | return native._list_element(self, index) |
paul@6 | 111 | |
paul@227 | 112 | def __set_single_item__(self, index, value): |
paul@227 | 113 | |
paul@227 | 114 | "Set at the normalised (positive) 'index' the given 'value'." |
paul@227 | 115 | |
paul@265 | 116 | self._check_index(index) |
paul@227 | 117 | return native._list_setelement(self, index, value) |
paul@227 | 118 | |
paul@6 | 119 | # vim: tabstop=4 expandtab shiftwidth=4 |