1 #!/usr/bin/env python 2 3 """ 4 List 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__.iterator import listiterator 23 from __builtins__.sequence import sequence 24 import native 25 26 class list(sequence): 27 28 "Implementation of list." 29 30 def __init__(self, args=None): 31 32 "Initialise the list." 33 34 # Reserve an attribute for a fragment reference along with some space 35 # for elements. 36 37 self.__data__ = native._list_init(len(args)) 38 39 if args is not None: 40 self.extend(args) 41 42 def __contains__(self, value): pass 43 def __setitem__(self, index, value): pass 44 def __delitem__(self, index): pass 45 def __setslice__(self, start, end, slice): pass 46 def __delslice__(self, start, end): pass 47 48 def append(self, value): 49 50 "Append 'value' to the list." 51 52 native._list_append(self, value) 53 54 def insert(self, i, value): pass 55 56 def extend(self, iterable): 57 58 "Extend the list with the contents of 'iterable'." 59 60 for i in iterable: 61 self.append(i) 62 63 def pop(self): pass 64 def reverse(self): pass 65 def sort(self, cmp=None, key=None, reverse=0): pass 66 67 def __len__(self): 68 69 "Return the length of the list." 70 71 return native._list_len(self) 72 73 def __add__(self, other): pass 74 75 def __iadd__(self, other): 76 77 "Concatenate 'other' to the list." 78 79 if isinstance(other, list): 80 native._list_concat(self, other) 81 else: 82 self.extend(other) 83 return self 84 85 def __str__(self): 86 87 "Return a string representation of the list." 88 89 b = buffer() 90 i = 0 91 l = self.__len__() 92 first = True 93 94 # NOTE: Should really show quoted forms of the items. 95 96 b.append("[") 97 while i < l: 98 if first: 99 first = False 100 else: 101 b.append(", ") 102 b.append(repr(self.__get_single_item__(i))) 103 i += 1 104 b.append("]") 105 106 return str(b) 107 108 __repr__ = __str__ 109 110 def __bool__(self): 111 112 "Lists are true if non-empty." 113 114 return native._list_nonempty(self) 115 116 def __iter__(self): 117 118 "Return an iterator." 119 120 return listiterator(self) 121 122 # Special implementation methods. 123 124 def __get_single_item__(self, index): 125 126 "Return the item at the normalised (positive) 'index'." 127 128 if index >= len(self): 129 raise IndexError(index) 130 131 return native._list_element(self, index) 132 133 # vim: tabstop=4 expandtab shiftwidth=4