Lichen

lib/__builtins__/list.py

227:3a4ba5788f0f
2016-11-23 Paul Boddie Fixed list and tuple initialisation via their initialisers. Introduced generic string representation support for sequences. Removed superfluous tuple native functions. Added element-setting and size-setting native functions for fragments. Added __setitem__ support for sequences. Added initialisation and serialisation tests for lists and tuples.
     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(args is not None and len(args) or 0)    38     39         if args is not None:    40             self.extend(args)    41     42     def __contains__(self, value): pass    43     def __delitem__(self, index): pass    44     def __setslice__(self, start, end, slice): pass    45     def __delslice__(self, start, end): pass    46     47     def append(self, value):    48     49         "Append 'value' to the list."    50     51         native._list_append(self, value)    52     53     def insert(self, i, value): pass    54     55     def extend(self, iterable):    56     57         "Extend the list with the contents of 'iterable'."    58     59         for i in iterable:    60             self.append(i)    61     62     def pop(self): pass    63     def reverse(self): pass    64     def sort(self, cmp=None, key=None, reverse=0): pass    65     66     def __len__(self):    67     68         "Return the length of the list."    69     70         return native._list_len(self)    71     72     def __add__(self, other): pass    73     74     def __iadd__(self, other):    75     76         "Concatenate 'other' to the list."    77     78         if isinstance(other, list):    79             native._list_concat(self, other)    80         else:    81             self.extend(other)    82         return self    83     84     def __str__(self):    85     86         "Return a string representation."    87     88         return self._str("[", "]")    89     90     __repr__ = __str__    91     92     def __bool__(self):    93     94         "Lists are true if non-empty."    95     96         return native._list_nonempty(self)    97     98     def __iter__(self):    99    100         "Return an iterator."   101    102         return listiterator(self)   103    104     # Special implementation methods.   105    106     def __get_single_item__(self, index):   107    108         "Return the item at the normalised (positive) 'index'."   109    110         if index >= len(self):   111             raise IndexError(index)   112    113         return native._list_element(self, index)   114    115     def __set_single_item__(self, index, value):   116    117         "Set at the normalised (positive) 'index' the given 'value'."   118    119         if index >= len(self):   120             raise IndexError(index)   121    122         return native._list_setelement(self, index, value)   123    124 # vim: tabstop=4 expandtab shiftwidth=4