Lichen

lib/__builtins__/list.py

206:1a64c775111a
2016-11-22 Paul Boddie Fixed fragment allocation in the native functions and added some list and buffer operations, extending the buffer class. Eventually, buffer and list may be combined in some way.
     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 _getitem, _getslice    24 import native    25     26 class list(object):    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 __getitem__(self, index):    43     44         "Return the item or slice specified by 'index'."    45     46         return _getitem(self, index)    47     48     def __contains__(self, value): pass    49     def __setitem__(self, index, value): pass    50     def __delitem__(self, index): pass    51     52     def __getslice__(self, start, end=None):    53     54         "Return a slice starting from 'start', with the optional 'end'."    55     56         return _getslice(self, start, end)    57     58     def __setslice__(self, start, end, slice): pass    59     def __delslice__(self, start, end): pass    60     61     def append(self, value):    62     63         "Append 'value' to the list."    64     65         native._list_append(self, value)    66     67     def insert(self, i, value): pass    68     69     def extend(self, iterable):    70     71         "Extend the list with the contents of 'iterable'."    72     73         for i in iterable:    74             self.append(i)    75     76     def pop(self): pass    77     def reverse(self): pass    78     def sort(self, cmp=None, key=None, reverse=0): pass    79     80     def __len__(self):    81     82         "Return the length of the list."    83     84         return native._list_len(self)    85     86     def __add__(self, other): pass    87     88     def __iadd__(self, other):    89     90         "Concatenate 'other' to the list."    91     92         if isinstance(other, list):    93             native._list_concat(self, other)    94         else:    95             self.extend(other)    96         return self    97     98     def __str__(self):    99    100         "Return a string representation of the list."   101    102         b = buffer()   103         i = 0   104         l = self.__len__()   105         first = True   106    107         # NOTE: Should really show quoted forms of the items.   108    109         b.append("[")   110         while i < l:   111             if first:   112                 first = False   113             else:   114                 b.append(", ")   115             b.append(self.__get_single_item__(i))   116         b.append("]")   117    118         return str(b)   119    120     def __bool__(self):   121    122         "Lists are true if non-empty."   123    124         return native._list_nonempty(self)   125    126     def __iter__(self):   127    128         "Return an iterator."   129    130         return listiterator(self)   131    132     # Special implementation methods.   133    134     def __get_single_item__(self, index):   135    136         "Return the item at the normalised (positive) 'index'."   137    138         if index >= len(self):   139             raise IndexError(index)   140    141         return native._list_element(self, index)   142    143 # vim: tabstop=4 expandtab shiftwidth=4