1.1 --- a/lib/builtins.py Mon Sep 27 23:30:55 2010 +0200
1.2 +++ b/lib/builtins.py Mon Nov 01 01:22:44 2010 +0100
1.3 @@ -173,12 +173,20 @@
1.4 self._elements = None
1.5
1.6 def __getitem__(self, index):
1.7 - # Note usage.
1.8 - IndexError
1.9 +
1.10 + "Return the item or slice specified by 'index'."
1.11 +
1.12 + return _getitem(self, index)
1.13
1.14 def __contains__(self, value): pass
1.15 def __setitem__(self, index, value): pass
1.16 - def __getslice__(self, start, end=None): pass
1.17 +
1.18 + def __getslice__(self, start, end=None):
1.19 +
1.20 + "Return a slice starting from 'start', with the optional 'end'."
1.21 +
1.22 + return _getslice(self, start, end)
1.23 +
1.24 def __setslice__(self, start, end, slice): pass
1.25 def append(self, value): pass
1.26
1.27 @@ -202,6 +210,10 @@
1.28
1.29 return listiterator(self)
1.30
1.31 + # Special implementation methods.
1.32 +
1.33 + def __get_single_item__(self, index): pass
1.34 +
1.35 class listiterator(object):
1.36
1.37 "Implementation of listiterator."
1.38 @@ -270,10 +282,17 @@
1.39 def __init__(self, args): pass
1.40
1.41 def __getitem__(self, index):
1.42 - # Note usage.
1.43 - IndexError
1.44 +
1.45 + "Return the item or slice specified by 'index'."
1.46 +
1.47 + return _getitem(self, index)
1.48
1.49 - def __getslice__(self, start, end=None): pass
1.50 + def __getslice__(self, start, end=None):
1.51 +
1.52 + "Return a slice starting from 'start', with the optional 'end'."
1.53 +
1.54 + return tuple(_getslice(self, start, end))
1.55 +
1.56 def __len__(self): pass
1.57 def __add__(self, other): pass
1.58 def __str__(self): pass
1.59 @@ -285,6 +304,10 @@
1.60
1.61 return listiterator(self)
1.62
1.63 + # Special implementation methods.
1.64 +
1.65 + def __get_single_item__(self, index): pass
1.66 +
1.67 class unicode(basestring):
1.68 pass
1.69
1.70 @@ -435,7 +458,10 @@
1.71 either a class or a tuple of classes.
1.72 """
1.73
1.74 + # NOTE: CPython insists on tuples, but any sequence might be considered
1.75 + # NOTE: acceptable.
1.76 # NOTE: tuple.__class__ is tuple in micropython!
1.77 +
1.78 if cls_or_tuple is not tuple and cls_or_tuple.__class__ is tuple:
1.79 for cls in cls_or_tuple:
1.80 if _isinstance(obj, cls):
1.81 @@ -444,8 +470,6 @@
1.82 else:
1.83 return _isinstance(obj, cls_or_tuple)
1.84
1.85 -def _isinstance(obj, cls): pass
1.86 -
1.87 def issubclass(obj, cls_or_tuple): pass
1.88
1.89 def iter(collection):
1.90 @@ -511,6 +535,81 @@
1.91 def vars(obj=None): pass
1.92 def zip(*args): pass
1.93
1.94 +# Utility functions.
1.95 +
1.96 +def _get_absolute_index(index, length):
1.97 +
1.98 + """
1.99 + Return the absolute index for 'index' given a collection having the
1.100 + specified 'length'.
1.101 + """
1.102 +
1.103 + if index < 0:
1.104 + return length + index
1.105 + else:
1.106 + return index
1.107 +
1.108 +def _normalise_index(index, length):
1.109 +
1.110 + "Normalise 'index' for a collection having the specified 'length'."
1.111 +
1.112 + return _min(length, _max(0, _get_absolute_index(index, length)))
1.113 +
1.114 +def _max(x, y):
1.115 +
1.116 + "Return the maximum of 'x' and 'y'."
1.117 +
1.118 + if x >= y:
1.119 + return x
1.120 + else:
1.121 + return y
1.122 +
1.123 +def _min(x, y):
1.124 +
1.125 + "Return the minimum of 'x' and 'y'."
1.126 +
1.127 + if x <= y:
1.128 + return x
1.129 + else:
1.130 + return y
1.131 +
1.132 +def _getitem(seq, index):
1.133 +
1.134 + "Return the item or slice specified by 'index'."
1.135 +
1.136 + if isinstance(index, int):
1.137 + return seq.__get_single_item__(index)
1.138 + elif isinstance(index, slice):
1.139 + return seq.__getslice__(index.start, index.end)
1.140 + else:
1.141 + raise TypeError
1.142 +
1.143 +def _getslice(seq, start, end=None):
1.144 +
1.145 + "Return a slice starting from 'start', with the optional 'end'."
1.146 +
1.147 + length = len(seq)
1.148 +
1.149 + if start is None:
1.150 + start = 0
1.151 + else:
1.152 + start = _normalise_index(start, length)
1.153 +
1.154 + if end is None:
1.155 + end = length
1.156 + else:
1.157 + end = _normalise_index(end, length)
1.158 +
1.159 + result = []
1.160 + while start < end:
1.161 + result.append(seq.__get_single_item__(start))
1.162 + start += 1
1.163 + return result
1.164 +
1.165 +# Special implementation functions.
1.166 +
1.167 +def _isinstance(obj, cls): pass
1.168 +
1.169 # Reference some names to ensure their existence. This should be everything
1.170 # mentioned in a get_builtin or load_builtin call. Instances from this module
1.171 # should be predefined constants.