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@9 | 22 | from __builtins__.iterator import listiterator |
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 __contains__(self, value): pass |
paul@6 | 43 | def __delitem__(self, index): pass |
paul@6 | 44 | def __setslice__(self, start, end, slice): pass |
paul@6 | 45 | def __delslice__(self, start, end): pass |
paul@206 | 46 | |
paul@206 | 47 | def append(self, value): |
paul@206 | 48 | |
paul@206 | 49 | "Append 'value' to the list." |
paul@206 | 50 | |
paul@206 | 51 | native._list_append(self, value) |
paul@206 | 52 | |
paul@6 | 53 | def insert(self, i, value): pass |
paul@6 | 54 | |
paul@6 | 55 | def extend(self, iterable): |
paul@6 | 56 | |
paul@6 | 57 | "Extend the list with the contents of 'iterable'." |
paul@6 | 58 | |
paul@6 | 59 | for i in iterable: |
paul@6 | 60 | self.append(i) |
paul@6 | 61 | |
paul@6 | 62 | def pop(self): pass |
paul@6 | 63 | def reverse(self): pass |
paul@6 | 64 | def sort(self, cmp=None, key=None, reverse=0): pass |
paul@140 | 65 | |
paul@140 | 66 | def __len__(self): |
paul@140 | 67 | |
paul@140 | 68 | "Return the length of the list." |
paul@140 | 69 | |
paul@140 | 70 | return native._list_len(self) |
paul@140 | 71 | |
paul@6 | 72 | def __add__(self, other): pass |
paul@206 | 73 | |
paul@206 | 74 | def __iadd__(self, other): |
paul@206 | 75 | |
paul@206 | 76 | "Concatenate 'other' to the list." |
paul@206 | 77 | |
paul@206 | 78 | if isinstance(other, list): |
paul@206 | 79 | native._list_concat(self, other) |
paul@206 | 80 | else: |
paul@206 | 81 | self.extend(other) |
paul@206 | 82 | return self |
paul@198 | 83 | |
paul@198 | 84 | def __str__(self): |
paul@198 | 85 | |
paul@227 | 86 | "Return a string representation." |
paul@198 | 87 | |
paul@227 | 88 | return self._str("[", "]") |
paul@6 | 89 | |
paul@222 | 90 | __repr__ = __str__ |
paul@222 | 91 | |
paul@6 | 92 | def __bool__(self): |
paul@6 | 93 | |
paul@6 | 94 | "Lists are true if non-empty." |
paul@6 | 95 | |
paul@140 | 96 | return native._list_nonempty(self) |
paul@6 | 97 | |
paul@6 | 98 | def __iter__(self): |
paul@6 | 99 | |
paul@6 | 100 | "Return an iterator." |
paul@6 | 101 | |
paul@6 | 102 | return listiterator(self) |
paul@6 | 103 | |
paul@6 | 104 | # Special implementation methods. |
paul@6 | 105 | |
paul@140 | 106 | def __get_single_item__(self, index): |
paul@140 | 107 | |
paul@194 | 108 | "Return the item at the normalised (positive) 'index'." |
paul@194 | 109 | |
paul@194 | 110 | if index >= len(self): |
paul@194 | 111 | raise IndexError(index) |
paul@140 | 112 | |
paul@140 | 113 | return native._list_element(self, index) |
paul@6 | 114 | |
paul@227 | 115 | def __set_single_item__(self, index, value): |
paul@227 | 116 | |
paul@227 | 117 | "Set at the normalised (positive) 'index' the given 'value'." |
paul@227 | 118 | |
paul@227 | 119 | if index >= len(self): |
paul@227 | 120 | raise IndexError(index) |
paul@227 | 121 | |
paul@227 | 122 | return native._list_setelement(self, index, value) |
paul@227 | 123 | |
paul@6 | 124 | # vim: tabstop=4 expandtab shiftwidth=4 |