1.1 --- a/lib/builtins.py Wed Dec 13 00:24:59 2006 +0100
1.2 +++ b/lib/builtins.py Sat Dec 16 00:51:02 2006 +0100
1.3 @@ -54,7 +54,16 @@
1.4 return str()
1.5
1.6 class dict:
1.7 - pass
1.8 + def __init__(self, *args):
1.9 + for key, value in args:
1.10 + self[key] = value
1.11 +
1.12 + def __setitem__(self, key, value):
1.13 + self.key = key
1.14 + self.value = value
1.15 +
1.16 + def __getitem__(self, key):
1.17 + return self.value
1.18
1.19 class file:
1.20 def write(self, s):
1.21 @@ -402,102 +411,49 @@
1.22
1.23 class list:
1.24 def __init__(self, *args):
1.25 - self.next = None
1.26 - self.last = self
1.27 for arg in args:
1.28 self.append(arg)
1.29
1.30 def __getitem__(self, index):
1.31 - i = 0
1.32 - n = self
1.33 - # NOTE: Support negative indices using last.
1.34 - while i < index and n.next is not None:
1.35 - n = n.next
1.36 - i += 1
1.37 - if n.next is not None:
1.38 - return n.value
1.39 + if -len(self) <= index < len(self):
1.40 + return self.value
1.41 else:
1.42 - raise IndexError() # NOTE: Make this compliant with Python!
1.43 + raise IndexError, index
1.44
1.45 def __setitem__(self, index, value):
1.46 - i = 0
1.47 - n = self
1.48 - # NOTE: Support negative indices using last.
1.49 - while i < index and n.next is not None:
1.50 - n = n.next
1.51 - i += 1
1.52 - if n.next is not None:
1.53 - n.value = value
1.54 + if -len(self) <= index < len(self):
1.55 + self.value = value
1.56 else:
1.57 - raise IndexError() # NOTE: Make this compliant with Python!
1.58 + raise IndexError, index
1.59
1.60 def __getslice__(self, start, end=None):
1.61 - slice = []
1.62 - i = 0
1.63 - n = self
1.64 - # NOTE: Support negative indices using last.
1.65 - while (end is None or i < end) and n.next is not None:
1.66 - if i >= start:
1.67 - slice.append(n.value)
1.68 - n = n.next
1.69 - i += 1
1.70 - return slice
1.71 + if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end:
1.72 + return list(self.value)
1.73 + else:
1.74 + raise IndexError, index
1.75
1.76 def __setslice__(self, start, end, slice):
1.77 - i = 0
1.78 - n = self
1.79 - j = 0
1.80 - p = slice
1.81 - # NOTE: Support negative indices using last.
1.82 - # NOTE: Support appending when start >= len(self).
1.83 - while i < end and n is not None and p is not None:
1.84 - if i >= start:
1.85 - n.value = p.value
1.86 - p = p.next
1.87 - j += 1
1.88 - n = n.next
1.89 - i += 1
1.90 + if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end:
1.91 + for value in slice:
1.92 + self.value = value
1.93 + else:
1.94 + raise IndexError, index
1.95
1.96 def append(self, value):
1.97 - n = self.last
1.98 - n.value = value
1.99 - n.next = self.__class__()
1.100 - self.last = n.next
1.101 + self.value = value
1.102
1.103 def __len__(self):
1.104 - i = 0
1.105 - n = self
1.106 - while n.next is not None:
1.107 - n = n.next
1.108 - i += 1
1.109 - return i
1.110 + return int()
1.111
1.112 def __add__(self, other):
1.113 - result = self.__class__()
1.114 - for value in self:
1.115 - result.append(value)
1.116 for value in other:
1.117 - result.append(value)
1.118 - return result
1.119 + self.value = value
1.120 + return self
1.121
1.122 - def __iadd__(self, other):
1.123 - for value in other:
1.124 - self.append(value)
1.125 - return self
1.126 + __iadd__ = __add__
1.127
1.128 def __str__(self):
1.129 - output = ["["]
1.130 - n = self
1.131 - first = 1
1.132 - while n.next is not None:
1.133 - if not first:
1.134 - output.append(", ")
1.135 - else:
1.136 - first = 0
1.137 - output.append(str(n.value))
1.138 - n = n.next
1.139 - output.append("]")
1.140 - return "".join(output)
1.141 + return str()
1.142
1.143 def __iter__(self):
1.144 return listiterator(self)
1.145 @@ -508,13 +464,12 @@
1.146 class listiterator:
1.147 def __init__(self, l):
1.148 self.l = l
1.149 + self.index = 0
1.150
1.151 def next(self):
1.152 - l = self.l
1.153 - next = l.next
1.154 - if next is not None:
1.155 - self.l = next
1.156 - return l.value
1.157 + if self.index < len(self.l):
1.158 + self.index += 1
1.159 + return self.l[self.index]
1.160 else:
1.161 raise StopIteration
1.162
1.163 @@ -683,73 +638,49 @@
1.164
1.165 class tuple:
1.166 def __init__(self, *args):
1.167 - self.next = None
1.168 - self.last = self
1.169 for arg in args:
1.170 self.append(arg)
1.171
1.172 def __getitem__(self, index):
1.173 - i = 0
1.174 - n = self
1.175 - # NOTE: Support negative indices using last.
1.176 - while i < index and n.next is not None:
1.177 - n = n.next
1.178 - i += 1
1.179 - if n.next is not None:
1.180 - return n.value
1.181 + if -len(self) <= index < len(self):
1.182 + return self.value
1.183 else:
1.184 - raise IndexError() # NOTE: Make this compliant with Python!
1.185 + raise IndexError, index
1.186 +
1.187 + def __setitem__(self, index, value):
1.188 + if -len(self) <= index < len(self):
1.189 + self.value = value
1.190 + else:
1.191 + raise IndexError, index
1.192
1.193 def __getslice__(self, start, end=None):
1.194 - # NOTE: Should probably return a tuple.
1.195 - slice = []
1.196 - i = 0
1.197 - n = self
1.198 - # NOTE: Support negative indices using last.
1.199 - while (end is None or i < end) and n.next is not None:
1.200 - if i >= start:
1.201 - slice.append(n.value)
1.202 - n = n.next
1.203 - i += 1
1.204 - return slice
1.205 + if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end:
1.206 + return list(self.value)
1.207 + else:
1.208 + raise IndexError, index
1.209 +
1.210 + def __setslice__(self, start, end, slice):
1.211 + if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end:
1.212 + for value in slice:
1.213 + self.value = value
1.214 + else:
1.215 + raise IndexError, index
1.216
1.217 # NOTE: The append method should be internal at most.
1.218
1.219 def append(self, value):
1.220 - n = self.last
1.221 - n.value = value
1.222 - n.next = self.__class__()
1.223 - self.last = n.next
1.224 + self.value = value
1.225
1.226 def __len__(self):
1.227 - i = 0
1.228 - n = self
1.229 - while n.next is not None:
1.230 - n = n.next
1.231 - i += 1
1.232 - return i
1.233 + return int()
1.234
1.235 def __add__(self, other):
1.236 - result = self.__class__()
1.237 - for value in self:
1.238 - result.append(value)
1.239 for value in other:
1.240 - result.append(value)
1.241 - return result
1.242 + self.value = value
1.243 + return self
1.244
1.245 def __str__(self):
1.246 - output = ["("]
1.247 - n = self
1.248 - first = 1
1.249 - while n.next is not None:
1.250 - if not first:
1.251 - output.append(", ")
1.252 - else:
1.253 - first = 0
1.254 - output.append(str(n.value))
1.255 - n = n.next
1.256 - output.append(")")
1.257 - return "".join(output)
1.258 + return str()
1.259
1.260 def __iter__(self):
1.261 return tupleiterator(self)
1.262 @@ -760,13 +691,12 @@
1.263 class tupleiterator:
1.264 def __init__(self, l):
1.265 self.l = l
1.266 + self.index = 0
1.267
1.268 def next(self):
1.269 - l = self.l
1.270 - next = l.next
1.271 - if next is not None:
1.272 - self.l = next
1.273 - return l.value
1.274 + if self.index < len(self.l):
1.275 + self.index += 1
1.276 + return self.l[self.index]
1.277 else:
1.278 raise StopIteration
1.279
1.280 @@ -791,7 +721,8 @@
1.281 return current
1.282
1.283 class Exception:
1.284 - pass
1.285 + def __init__(self, *args):
1.286 + pass
1.287
1.288 class AttributeError(Exception):
1.289 pass