paulb@22 | 1 | #!/usr/bin/env python |
paulb@22 | 2 | |
paulb@22 | 3 | """ |
paulb@22 | 4 | Simple built-in classes and functions. |
paulb@22 | 5 | |
paulb@22 | 6 | Copyright (C) 2005, 2006 Paul Boddie <paul@boddie.org.uk> |
paulb@22 | 7 | |
paulb@22 | 8 | This software is free software; you can redistribute it and/or |
paulb@22 | 9 | modify it under the terms of the GNU General Public License as |
paulb@22 | 10 | published by the Free Software Foundation; either version 2 of |
paulb@22 | 11 | the License, or (at your option) any later version. |
paulb@22 | 12 | |
paulb@22 | 13 | This software is distributed in the hope that it will be useful, |
paulb@22 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@22 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paulb@22 | 16 | GNU General Public License for more details. |
paulb@22 | 17 | |
paulb@22 | 18 | You should have received a copy of the GNU General Public |
paulb@22 | 19 | License along with this library; see the file LICENCE.txt |
paulb@22 | 20 | If not, write to the Free Software Foundation, Inc., |
paulb@22 | 21 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@22 | 22 | """ |
paulb@22 | 23 | |
paulb@129 | 24 | class object: |
paulb@130 | 25 | def __bool__(self): |
paulb@130 | 26 | # NOTE: Should really test for __len__ and then resort to False. |
paulb@130 | 27 | # NOTE: See the reference: 3.3.1 Basic customization |
paulb@129 | 28 | return False |
paulb@129 | 29 | |
paulb@145 | 30 | def __iadd__(self, other): |
paulb@145 | 31 | return self.__add__(other) |
paulb@145 | 32 | |
paulb@130 | 33 | class bool: |
paulb@130 | 34 | def __bool__(self): |
paulb@22 | 35 | return self |
paulb@22 | 36 | |
paulb@22 | 37 | def __str__(self): |
paulb@22 | 38 | if self: |
paulb@22 | 39 | return "True" |
paulb@22 | 40 | else: |
paulb@22 | 41 | return "False" |
paulb@22 | 42 | |
paulb@145 | 43 | class buffer: |
paulb@145 | 44 | def __init__(self, size): |
paulb@145 | 45 | if not isinstance(size, int): |
paulb@55 | 46 | raise TypeError |
paulb@22 | 47 | |
paulb@145 | 48 | def append(self, s): |
paulb@145 | 49 | if not isinstance(s, str): |
paulb@145 | 50 | raise TypeError |
paulb@145 | 51 | pass |
paulb@22 | 52 | |
paulb@22 | 53 | def __str__(self): |
paulb@145 | 54 | return str() |
paulb@22 | 55 | |
paulb@145 | 56 | class dict: |
paulb@145 | 57 | pass |
paulb@22 | 58 | |
paulb@145 | 59 | class file: |
paulb@145 | 60 | def write(self, s): |
paulb@145 | 61 | pass |
paulb@22 | 62 | |
paulb@22 | 63 | class float: |
paulb@22 | 64 | def __iadd__(self, other): |
paulb@22 | 65 | if isinstance(other, int): |
paulb@22 | 66 | return float() |
paulb@22 | 67 | elif isinstance(other, long): |
paulb@22 | 68 | return float() |
paulb@22 | 69 | elif isinstance(other, float): |
paulb@22 | 70 | return float() |
paulb@22 | 71 | else: |
paulb@55 | 72 | raise TypeError |
paulb@22 | 73 | |
paulb@22 | 74 | def __isub__(self, other): |
paulb@22 | 75 | if isinstance(other, int): |
paulb@22 | 76 | return float() |
paulb@22 | 77 | elif isinstance(other, long): |
paulb@22 | 78 | return float() |
paulb@22 | 79 | elif isinstance(other, float): |
paulb@22 | 80 | return float() |
paulb@22 | 81 | else: |
paulb@55 | 82 | raise TypeError |
paulb@22 | 83 | |
paulb@22 | 84 | def __add__(self, other): |
paulb@22 | 85 | if isinstance(other, int): |
paulb@22 | 86 | return float() |
paulb@22 | 87 | elif isinstance(other, long): |
paulb@22 | 88 | return float() |
paulb@22 | 89 | elif isinstance(other, float): |
paulb@22 | 90 | return float() |
paulb@22 | 91 | else: |
paulb@55 | 92 | raise TypeError |
paulb@22 | 93 | |
paulb@22 | 94 | def __radd__(self, other): |
paulb@22 | 95 | if isinstance(other, int): |
paulb@22 | 96 | return float() |
paulb@22 | 97 | elif isinstance(other, long): |
paulb@22 | 98 | return float() |
paulb@22 | 99 | elif isinstance(other, float): |
paulb@22 | 100 | return float() |
paulb@22 | 101 | else: |
paulb@55 | 102 | raise TypeError |
paulb@22 | 103 | |
paulb@22 | 104 | def __sub__(self, other): |
paulb@22 | 105 | if isinstance(other, int): |
paulb@22 | 106 | return float() |
paulb@22 | 107 | elif isinstance(other, long): |
paulb@22 | 108 | return float() |
paulb@22 | 109 | elif isinstance(other, float): |
paulb@22 | 110 | return float() |
paulb@22 | 111 | else: |
paulb@55 | 112 | raise TypeError |
paulb@22 | 113 | |
paulb@22 | 114 | def __rsub__(self, other): |
paulb@22 | 115 | if isinstance(other, int): |
paulb@22 | 116 | return float() |
paulb@22 | 117 | elif isinstance(other, long): |
paulb@22 | 118 | return float() |
paulb@22 | 119 | elif isinstance(other, float): |
paulb@22 | 120 | return float() |
paulb@22 | 121 | else: |
paulb@55 | 122 | raise TypeError |
paulb@22 | 123 | |
paulb@22 | 124 | def __mul__(self, other): |
paulb@22 | 125 | if isinstance(other, int): |
paulb@22 | 126 | return float() |
paulb@22 | 127 | elif isinstance(other, long): |
paulb@22 | 128 | return float() |
paulb@22 | 129 | elif isinstance(other, float): |
paulb@22 | 130 | return float() |
paulb@22 | 131 | else: |
paulb@55 | 132 | raise TypeError |
paulb@22 | 133 | |
paulb@22 | 134 | def __rmul__(self, other): |
paulb@22 | 135 | if isinstance(other, int): |
paulb@22 | 136 | return float() |
paulb@22 | 137 | elif isinstance(other, long): |
paulb@22 | 138 | return float() |
paulb@22 | 139 | elif isinstance(other, float): |
paulb@22 | 140 | return float() |
paulb@22 | 141 | else: |
paulb@55 | 142 | raise TypeError |
paulb@22 | 143 | |
paulb@22 | 144 | def __div__(self, other): |
paulb@22 | 145 | if isinstance(other, int): |
paulb@22 | 146 | return float() |
paulb@22 | 147 | elif isinstance(other, long): |
paulb@22 | 148 | return float() |
paulb@22 | 149 | elif isinstance(other, float): |
paulb@22 | 150 | return float() |
paulb@22 | 151 | else: |
paulb@55 | 152 | raise TypeError |
paulb@22 | 153 | |
paulb@22 | 154 | def __rdiv__(self, other): |
paulb@22 | 155 | if isinstance(other, int): |
paulb@22 | 156 | return float() |
paulb@22 | 157 | elif isinstance(other, long): |
paulb@22 | 158 | return float() |
paulb@22 | 159 | elif isinstance(other, float): |
paulb@22 | 160 | return float() |
paulb@22 | 161 | else: |
paulb@55 | 162 | raise TypeError |
paulb@22 | 163 | |
paulb@136 | 164 | def __floordiv__(self, other): |
paulb@136 | 165 | if isinstance(other, int): |
paulb@136 | 166 | return float() |
paulb@136 | 167 | elif isinstance(other, long): |
paulb@136 | 168 | return float() |
paulb@136 | 169 | elif isinstance(other, float): |
paulb@136 | 170 | return float() |
paulb@136 | 171 | else: |
paulb@136 | 172 | raise TypeError |
paulb@136 | 173 | |
paulb@136 | 174 | def __rfloordiv__(self, other): |
paulb@136 | 175 | if isinstance(other, int): |
paulb@136 | 176 | return float() |
paulb@136 | 177 | elif isinstance(other, long): |
paulb@136 | 178 | return float() |
paulb@136 | 179 | elif isinstance(other, float): |
paulb@136 | 180 | return float() |
paulb@136 | 181 | else: |
paulb@136 | 182 | raise TypeError |
paulb@136 | 183 | |
paulb@22 | 184 | def __pow__(self, other): |
paulb@22 | 185 | if isinstance(other, int): |
paulb@22 | 186 | return float() |
paulb@22 | 187 | elif isinstance(other, long): |
paulb@22 | 188 | return float() |
paulb@22 | 189 | elif isinstance(other, float): |
paulb@22 | 190 | return float() |
paulb@22 | 191 | else: |
paulb@55 | 192 | raise TypeError |
paulb@22 | 193 | |
paulb@22 | 194 | def __rpow__(self, other): |
paulb@22 | 195 | if isinstance(other, int): |
paulb@22 | 196 | return float() |
paulb@22 | 197 | elif isinstance(other, long): |
paulb@22 | 198 | return float() |
paulb@22 | 199 | elif isinstance(other, float): |
paulb@22 | 200 | return float() |
paulb@22 | 201 | else: |
paulb@55 | 202 | raise TypeError |
paulb@22 | 203 | |
paulb@22 | 204 | def __lt__(self, other): |
paulb@22 | 205 | if isinstance(other, int): |
paulb@130 | 206 | return bool() |
paulb@22 | 207 | elif isinstance(other, long): |
paulb@130 | 208 | return bool() |
paulb@22 | 209 | elif isinstance(other, float): |
paulb@130 | 210 | return bool() |
paulb@22 | 211 | else: |
paulb@55 | 212 | raise TypeError |
paulb@22 | 213 | |
paulb@22 | 214 | def __gt__(self, other): |
paulb@22 | 215 | if isinstance(other, int): |
paulb@130 | 216 | return bool() |
paulb@22 | 217 | elif isinstance(other, long): |
paulb@130 | 218 | return bool() |
paulb@22 | 219 | elif isinstance(other, float): |
paulb@130 | 220 | return bool() |
paulb@22 | 221 | else: |
paulb@55 | 222 | raise TypeError |
paulb@22 | 223 | |
paulb@22 | 224 | def __le__(self, other): |
paulb@22 | 225 | if isinstance(other, int): |
paulb@130 | 226 | return bool() |
paulb@22 | 227 | elif isinstance(other, long): |
paulb@130 | 228 | return bool() |
paulb@22 | 229 | elif isinstance(other, float): |
paulb@130 | 230 | return bool() |
paulb@22 | 231 | else: |
paulb@55 | 232 | raise TypeError |
paulb@22 | 233 | |
paulb@22 | 234 | def __ge__(self, other): |
paulb@22 | 235 | if isinstance(other, int): |
paulb@130 | 236 | return bool() |
paulb@22 | 237 | elif isinstance(other, long): |
paulb@130 | 238 | return bool() |
paulb@22 | 239 | elif isinstance(other, float): |
paulb@130 | 240 | return bool() |
paulb@22 | 241 | else: |
paulb@55 | 242 | raise TypeError |
paulb@22 | 243 | |
paulb@22 | 244 | def __eq__(self, other): |
paulb@22 | 245 | if isinstance(other, int): |
paulb@130 | 246 | return bool() |
paulb@22 | 247 | elif isinstance(other, long): |
paulb@130 | 248 | return bool() |
paulb@22 | 249 | elif isinstance(other, float): |
paulb@130 | 250 | return bool() |
paulb@22 | 251 | else: |
paulb@55 | 252 | raise TypeError |
paulb@22 | 253 | |
paulb@22 | 254 | def __ne__(self, other): |
paulb@22 | 255 | if isinstance(other, int): |
paulb@130 | 256 | return bool() |
paulb@22 | 257 | elif isinstance(other, long): |
paulb@130 | 258 | return bool() |
paulb@22 | 259 | elif isinstance(other, float): |
paulb@130 | 260 | return bool() |
paulb@22 | 261 | else: |
paulb@55 | 262 | raise TypeError |
paulb@22 | 263 | |
paulb@22 | 264 | def __neg__(self): |
paulb@22 | 265 | return float() |
paulb@22 | 266 | |
paulb@22 | 267 | def __pos__(self): |
paulb@22 | 268 | return self |
paulb@22 | 269 | |
paulb@22 | 270 | def __str__(self): |
paulb@145 | 271 | return str() |
paulb@22 | 272 | |
paulb@130 | 273 | def __bool__(self): |
paulb@22 | 274 | return self != 0 |
paulb@22 | 275 | |
paulb@145 | 276 | class int: |
paulb@145 | 277 | def __iadd__(self, other): |
paulb@145 | 278 | if isinstance(other, int): |
paulb@145 | 279 | return int() |
paulb@145 | 280 | else: |
paulb@145 | 281 | raise TypeError |
paulb@145 | 282 | |
paulb@145 | 283 | def __isub__(self, other): |
paulb@145 | 284 | if isinstance(other, int): |
paulb@145 | 285 | return int() |
paulb@145 | 286 | else: |
paulb@145 | 287 | raise TypeError |
paulb@128 | 288 | |
paulb@22 | 289 | def __add__(self, other): |
paulb@145 | 290 | if isinstance(other, int): |
paulb@145 | 291 | return int() |
paulb@22 | 292 | else: |
paulb@55 | 293 | raise TypeError |
paulb@22 | 294 | |
paulb@22 | 295 | def __radd__(self, other): |
paulb@145 | 296 | if isinstance(other, int): |
paulb@145 | 297 | return int() |
paulb@145 | 298 | else: |
paulb@145 | 299 | raise TypeError |
paulb@145 | 300 | |
paulb@145 | 301 | def __sub__(self, other): |
paulb@145 | 302 | if isinstance(other, int): |
paulb@145 | 303 | return int() |
paulb@145 | 304 | else: |
paulb@145 | 305 | raise TypeError |
paulb@145 | 306 | |
paulb@145 | 307 | def __rsub__(self, other): |
paulb@145 | 308 | if isinstance(other, int): |
paulb@145 | 309 | return int() |
paulb@145 | 310 | else: |
paulb@145 | 311 | raise TypeError |
paulb@145 | 312 | |
paulb@145 | 313 | def __mul__(self, other): |
paulb@145 | 314 | if isinstance(other, int): |
paulb@145 | 315 | return int() |
paulb@145 | 316 | else: |
paulb@145 | 317 | raise TypeError |
paulb@145 | 318 | |
paulb@145 | 319 | def __rmul__(self, other): |
paulb@145 | 320 | if isinstance(other, int): |
paulb@145 | 321 | return int() |
paulb@145 | 322 | else: |
paulb@145 | 323 | raise TypeError |
paulb@145 | 324 | |
paulb@145 | 325 | def __div__(self, other): |
paulb@145 | 326 | if isinstance(other, int): |
paulb@145 | 327 | return int() |
paulb@145 | 328 | else: |
paulb@145 | 329 | raise TypeError |
paulb@145 | 330 | |
paulb@145 | 331 | def __rdiv__(self, other): |
paulb@145 | 332 | if isinstance(other, int): |
paulb@145 | 333 | return int() |
paulb@145 | 334 | else: |
paulb@145 | 335 | raise TypeError |
paulb@145 | 336 | |
paulb@145 | 337 | def __floordiv__(self, other): |
paulb@145 | 338 | if isinstance(other, int): |
paulb@145 | 339 | return int() |
paulb@145 | 340 | else: |
paulb@145 | 341 | raise TypeError |
paulb@145 | 342 | |
paulb@145 | 343 | def __rfloordiv__(self, other): |
paulb@145 | 344 | if isinstance(other, int): |
paulb@145 | 345 | return int() |
paulb@22 | 346 | else: |
paulb@55 | 347 | raise TypeError |
paulb@22 | 348 | |
paulb@145 | 349 | def __pow__(self, other): |
paulb@145 | 350 | if isinstance(other, int): |
paulb@145 | 351 | return int() |
paulb@145 | 352 | else: |
paulb@145 | 353 | raise TypeError |
paulb@145 | 354 | |
paulb@145 | 355 | def __lt__(self, other): |
paulb@145 | 356 | if isinstance(other, int): |
paulb@145 | 357 | return bool() |
paulb@145 | 358 | else: |
paulb@145 | 359 | raise TypeError |
paulb@145 | 360 | |
paulb@145 | 361 | def __gt__(self, other): |
paulb@145 | 362 | if isinstance(other, int): |
paulb@145 | 363 | return bool() |
paulb@145 | 364 | else: |
paulb@145 | 365 | raise TypeError |
paulb@145 | 366 | |
paulb@145 | 367 | def __le__(self, other): |
paulb@145 | 368 | if isinstance(other, int): |
paulb@145 | 369 | return bool() |
paulb@145 | 370 | else: |
paulb@145 | 371 | raise TypeError |
paulb@145 | 372 | |
paulb@145 | 373 | def __ge__(self, other): |
paulb@145 | 374 | if isinstance(other, int): |
paulb@145 | 375 | return bool() |
paulb@145 | 376 | else: |
paulb@145 | 377 | raise TypeError |
paulb@145 | 378 | |
paulb@145 | 379 | def __eq__(self, other): |
paulb@145 | 380 | if isinstance(other, int): |
paulb@145 | 381 | return bool() |
paulb@145 | 382 | else: |
paulb@145 | 383 | raise TypeError |
paulb@145 | 384 | |
paulb@145 | 385 | def __ne__(self, other): |
paulb@145 | 386 | if isinstance(other, int): |
paulb@145 | 387 | return bool() |
paulb@145 | 388 | else: |
paulb@145 | 389 | raise TypeError |
paulb@145 | 390 | |
paulb@145 | 391 | def __neg__(self): |
paulb@22 | 392 | return int() |
paulb@22 | 393 | |
paulb@145 | 394 | def __pos__(self): |
paulb@145 | 395 | return self |
paulb@145 | 396 | |
paulb@22 | 397 | def __str__(self): |
paulb@145 | 398 | return str() |
paulb@22 | 399 | |
paulb@130 | 400 | def __bool__(self): |
paulb@145 | 401 | return self != 0 |
paulb@22 | 402 | |
paulb@22 | 403 | class list: |
paulb@89 | 404 | def __init__(self, *args): |
paulb@22 | 405 | self.next = None |
paulb@22 | 406 | self.last = self |
paulb@89 | 407 | for arg in args: |
paulb@89 | 408 | self.append(arg) |
paulb@22 | 409 | |
paulb@22 | 410 | def __getitem__(self, index): |
paulb@22 | 411 | i = 0 |
paulb@22 | 412 | n = self |
paulb@22 | 413 | # NOTE: Support negative indices using last. |
paulb@22 | 414 | while i < index and n.next is not None: |
paulb@22 | 415 | n = n.next |
paulb@22 | 416 | i += 1 |
paulb@22 | 417 | if n.next is not None: |
paulb@22 | 418 | return n.value |
paulb@22 | 419 | else: |
paulb@22 | 420 | raise IndexError() # NOTE: Make this compliant with Python! |
paulb@22 | 421 | |
paulb@22 | 422 | def __setitem__(self, index, value): |
paulb@22 | 423 | i = 0 |
paulb@22 | 424 | n = self |
paulb@22 | 425 | # NOTE: Support negative indices using last. |
paulb@22 | 426 | while i < index and n.next is not None: |
paulb@22 | 427 | n = n.next |
paulb@22 | 428 | i += 1 |
paulb@22 | 429 | if n.next is not None: |
paulb@22 | 430 | n.value = value |
paulb@22 | 431 | else: |
paulb@22 | 432 | raise IndexError() # NOTE: Make this compliant with Python! |
paulb@22 | 433 | |
paulb@22 | 434 | def __getslice__(self, start, end=None): |
paulb@22 | 435 | slice = [] |
paulb@22 | 436 | i = 0 |
paulb@22 | 437 | n = self |
paulb@22 | 438 | # NOTE: Support negative indices using last. |
paulb@22 | 439 | while (end is None or i < end) and n.next is not None: |
paulb@22 | 440 | if i >= start: |
paulb@22 | 441 | slice.append(n.value) |
paulb@22 | 442 | n = n.next |
paulb@22 | 443 | i += 1 |
paulb@22 | 444 | return slice |
paulb@22 | 445 | |
paulb@22 | 446 | def __setslice__(self, start, end, slice): |
paulb@22 | 447 | i = 0 |
paulb@22 | 448 | n = self |
paulb@22 | 449 | j = 0 |
paulb@22 | 450 | p = slice |
paulb@22 | 451 | # NOTE: Support negative indices using last. |
paulb@22 | 452 | # NOTE: Support appending when start >= len(self). |
paulb@22 | 453 | while i < end and n is not None and p is not None: |
paulb@22 | 454 | if i >= start: |
paulb@22 | 455 | n.value = p.value |
paulb@22 | 456 | p = p.next |
paulb@22 | 457 | j += 1 |
paulb@22 | 458 | n = n.next |
paulb@22 | 459 | i += 1 |
paulb@22 | 460 | |
paulb@22 | 461 | def append(self, value): |
paulb@22 | 462 | n = self.last |
paulb@22 | 463 | n.value = value |
paulb@22 | 464 | n.next = self.__class__() |
paulb@22 | 465 | self.last = n.next |
paulb@22 | 466 | |
paulb@22 | 467 | def __len__(self): |
paulb@22 | 468 | i = 0 |
paulb@22 | 469 | n = self |
paulb@22 | 470 | while n.next is not None: |
paulb@22 | 471 | n = n.next |
paulb@22 | 472 | i += 1 |
paulb@22 | 473 | return i |
paulb@22 | 474 | |
paulb@22 | 475 | def __add__(self, other): |
paulb@22 | 476 | result = self.__class__() |
paulb@22 | 477 | for value in self: |
paulb@22 | 478 | result.append(value) |
paulb@22 | 479 | for value in other: |
paulb@22 | 480 | result.append(value) |
paulb@22 | 481 | return result |
paulb@22 | 482 | |
paulb@97 | 483 | def __iadd__(self, other): |
paulb@97 | 484 | for value in other: |
paulb@97 | 485 | self.append(value) |
paulb@97 | 486 | return self |
paulb@97 | 487 | |
paulb@22 | 488 | def __str__(self): |
paulb@22 | 489 | output = ["["] |
paulb@22 | 490 | n = self |
paulb@22 | 491 | first = 1 |
paulb@22 | 492 | while n.next is not None: |
paulb@22 | 493 | if not first: |
paulb@22 | 494 | output.append(", ") |
paulb@22 | 495 | else: |
paulb@22 | 496 | first = 0 |
paulb@22 | 497 | output.append(str(n.value)) |
paulb@22 | 498 | n = n.next |
paulb@22 | 499 | output.append("]") |
paulb@22 | 500 | return "".join(output) |
paulb@22 | 501 | |
paulb@22 | 502 | def __iter__(self): |
paulb@22 | 503 | return listiterator(self) |
paulb@22 | 504 | |
paulb@130 | 505 | def __bool__(self): |
paulb@22 | 506 | return self.__len__() != 0 |
paulb@22 | 507 | |
paulb@22 | 508 | class listiterator: |
paulb@22 | 509 | def __init__(self, l): |
paulb@22 | 510 | self.l = l |
paulb@22 | 511 | |
paulb@22 | 512 | def next(self): |
paulb@22 | 513 | l = self.l |
paulb@22 | 514 | next = l.next |
paulb@22 | 515 | if next is not None: |
paulb@22 | 516 | self.l = next |
paulb@22 | 517 | return l.value |
paulb@22 | 518 | else: |
paulb@55 | 519 | raise StopIteration |
paulb@22 | 520 | |
paulb@130 | 521 | def __bool__(self): |
paulb@130 | 522 | return bool() |
paulb@22 | 523 | |
paulb@145 | 524 | class long: |
paulb@145 | 525 | def __iadd__(self, other): |
paulb@145 | 526 | if isinstance(other, int): |
paulb@145 | 527 | return long() |
paulb@145 | 528 | elif isinstance(other, long): |
paulb@145 | 529 | return long() |
paulb@145 | 530 | else: |
paulb@145 | 531 | raise TypeError |
paulb@145 | 532 | |
paulb@145 | 533 | def __isub__(self, other): |
paulb@145 | 534 | if isinstance(other, int): |
paulb@145 | 535 | return long() |
paulb@145 | 536 | elif isinstance(other, long): |
paulb@145 | 537 | return long() |
paulb@145 | 538 | else: |
paulb@145 | 539 | raise TypeError |
paulb@145 | 540 | |
paulb@145 | 541 | def __add__(self, other): |
paulb@145 | 542 | if isinstance(other, int): |
paulb@145 | 543 | return long() |
paulb@145 | 544 | elif isinstance(other, long): |
paulb@145 | 545 | return long() |
paulb@145 | 546 | else: |
paulb@145 | 547 | raise TypeError |
paulb@145 | 548 | |
paulb@145 | 549 | def __radd__(self, other): |
paulb@145 | 550 | if isinstance(other, int): |
paulb@145 | 551 | return long() |
paulb@145 | 552 | elif isinstance(other, long): |
paulb@145 | 553 | return long() |
paulb@145 | 554 | else: |
paulb@145 | 555 | raise TypeError |
paulb@145 | 556 | |
paulb@145 | 557 | def __sub__(self, other): |
paulb@145 | 558 | if isinstance(other, int): |
paulb@145 | 559 | return long() |
paulb@145 | 560 | elif isinstance(other, long): |
paulb@145 | 561 | return long() |
paulb@145 | 562 | else: |
paulb@145 | 563 | raise TypeError |
paulb@145 | 564 | |
paulb@145 | 565 | def __rsub__(self, other): |
paulb@145 | 566 | if isinstance(other, int): |
paulb@145 | 567 | return long() |
paulb@145 | 568 | elif isinstance(other, long): |
paulb@145 | 569 | return long() |
paulb@145 | 570 | else: |
paulb@145 | 571 | raise TypeError |
paulb@145 | 572 | |
paulb@145 | 573 | def __lt__(self, other): |
paulb@145 | 574 | if isinstance(other, int): |
paulb@145 | 575 | return bool() |
paulb@145 | 576 | elif isinstance(other, long): |
paulb@145 | 577 | return bool() |
paulb@145 | 578 | else: |
paulb@145 | 579 | raise TypeError |
paulb@145 | 580 | |
paulb@145 | 581 | def __gt__(self, other): |
paulb@145 | 582 | if isinstance(other, int): |
paulb@145 | 583 | return bool() |
paulb@145 | 584 | elif isinstance(other, long): |
paulb@145 | 585 | return bool() |
paulb@145 | 586 | else: |
paulb@145 | 587 | raise TypeError |
paulb@145 | 588 | |
paulb@145 | 589 | def __le__(self, other): |
paulb@145 | 590 | if isinstance(other, int): |
paulb@145 | 591 | return bool() |
paulb@145 | 592 | elif isinstance(other, long): |
paulb@145 | 593 | return bool() |
paulb@145 | 594 | else: |
paulb@145 | 595 | raise TypeError |
paulb@145 | 596 | |
paulb@145 | 597 | def __ge__(self, other): |
paulb@145 | 598 | if isinstance(other, int): |
paulb@145 | 599 | return bool() |
paulb@145 | 600 | elif isinstance(other, long): |
paulb@145 | 601 | return bool() |
paulb@145 | 602 | else: |
paulb@145 | 603 | raise TypeError |
paulb@145 | 604 | |
paulb@145 | 605 | def __eq__(self, other): |
paulb@145 | 606 | if isinstance(other, int): |
paulb@145 | 607 | return bool() |
paulb@145 | 608 | elif isinstance(other, long): |
paulb@145 | 609 | return bool() |
paulb@145 | 610 | else: |
paulb@145 | 611 | raise TypeError |
paulb@145 | 612 | |
paulb@145 | 613 | def __ne__(self, other): |
paulb@145 | 614 | if isinstance(other, int): |
paulb@145 | 615 | return bool() |
paulb@145 | 616 | elif isinstance(other, long): |
paulb@145 | 617 | return bool() |
paulb@145 | 618 | else: |
paulb@145 | 619 | raise TypeError |
paulb@145 | 620 | |
paulb@145 | 621 | def __neg__(self): |
paulb@145 | 622 | return long() |
paulb@145 | 623 | |
paulb@145 | 624 | def __pos__(self): |
paulb@145 | 625 | return self |
paulb@145 | 626 | |
paulb@145 | 627 | def __str__(self): |
paulb@145 | 628 | return str() |
paulb@145 | 629 | |
paulb@145 | 630 | def __bool__(self): |
paulb@145 | 631 | return self != 0 |
paulb@145 | 632 | |
paulb@145 | 633 | class none: |
paulb@145 | 634 | def __bool__(self): |
paulb@145 | 635 | return False |
paulb@145 | 636 | |
paulb@145 | 637 | def __str__(self): |
paulb@145 | 638 | return "None" |
paulb@145 | 639 | |
paulb@145 | 640 | class str: |
paulb@145 | 641 | def __init__(self, x=None): |
paulb@145 | 642 | x.__str__() |
paulb@145 | 643 | |
paulb@145 | 644 | def __add__(self, other): |
paulb@145 | 645 | if isinstance(other, str): |
paulb@145 | 646 | return str() |
paulb@145 | 647 | else: |
paulb@145 | 648 | raise TypeError |
paulb@145 | 649 | |
paulb@145 | 650 | def __radd__(self, other): |
paulb@145 | 651 | if isinstance(other, str): |
paulb@145 | 652 | return str() |
paulb@145 | 653 | else: |
paulb@145 | 654 | raise TypeError |
paulb@145 | 655 | |
paulb@145 | 656 | def __len__(self): |
paulb@145 | 657 | return int() |
paulb@145 | 658 | |
paulb@145 | 659 | def __str__(self): |
paulb@145 | 660 | return self |
paulb@145 | 661 | |
paulb@145 | 662 | def __bool__(self): |
paulb@145 | 663 | return self.__len__() != 0 |
paulb@145 | 664 | |
paulb@145 | 665 | def join(self, l): |
paulb@145 | 666 | total = 0 |
paulb@145 | 667 | first = 1 |
paulb@145 | 668 | self_len = self.__len__() |
paulb@145 | 669 | for i in l: |
paulb@145 | 670 | if not first: |
paulb@145 | 671 | total += self_len |
paulb@145 | 672 | total += len(str(i)) |
paulb@145 | 673 | first = 0 |
paulb@145 | 674 | b = buffer(total) |
paulb@145 | 675 | first = 1 |
paulb@145 | 676 | for i in l: |
paulb@145 | 677 | if not first: |
paulb@145 | 678 | b.append(self) |
paulb@145 | 679 | b.append(str(i)) |
paulb@145 | 680 | first = 0 |
paulb@145 | 681 | s = str(b) |
paulb@145 | 682 | return s |
paulb@145 | 683 | |
paulb@22 | 684 | class tuple: |
paulb@66 | 685 | def __init__(self, *args): |
paulb@22 | 686 | self.next = None |
paulb@22 | 687 | self.last = self |
paulb@66 | 688 | for arg in args: |
paulb@66 | 689 | self.append(arg) |
paulb@22 | 690 | |
paulb@22 | 691 | def __getitem__(self, index): |
paulb@22 | 692 | i = 0 |
paulb@22 | 693 | n = self |
paulb@22 | 694 | # NOTE: Support negative indices using last. |
paulb@22 | 695 | while i < index and n.next is not None: |
paulb@22 | 696 | n = n.next |
paulb@22 | 697 | i += 1 |
paulb@22 | 698 | if n.next is not None: |
paulb@22 | 699 | return n.value |
paulb@22 | 700 | else: |
paulb@22 | 701 | raise IndexError() # NOTE: Make this compliant with Python! |
paulb@22 | 702 | |
paulb@22 | 703 | def __getslice__(self, start, end=None): |
paulb@22 | 704 | # NOTE: Should probably return a tuple. |
paulb@22 | 705 | slice = [] |
paulb@22 | 706 | i = 0 |
paulb@22 | 707 | n = self |
paulb@22 | 708 | # NOTE: Support negative indices using last. |
paulb@22 | 709 | while (end is None or i < end) and n.next is not None: |
paulb@22 | 710 | if i >= start: |
paulb@22 | 711 | slice.append(n.value) |
paulb@22 | 712 | n = n.next |
paulb@22 | 713 | i += 1 |
paulb@22 | 714 | return slice |
paulb@22 | 715 | |
paulb@22 | 716 | # NOTE: The append method should be internal at most. |
paulb@22 | 717 | |
paulb@22 | 718 | def append(self, value): |
paulb@22 | 719 | n = self.last |
paulb@22 | 720 | n.value = value |
paulb@22 | 721 | n.next = self.__class__() |
paulb@22 | 722 | self.last = n.next |
paulb@22 | 723 | |
paulb@22 | 724 | def __len__(self): |
paulb@22 | 725 | i = 0 |
paulb@22 | 726 | n = self |
paulb@22 | 727 | while n.next is not None: |
paulb@22 | 728 | n = n.next |
paulb@22 | 729 | i += 1 |
paulb@22 | 730 | return i |
paulb@22 | 731 | |
paulb@22 | 732 | def __add__(self, other): |
paulb@22 | 733 | result = self.__class__() |
paulb@22 | 734 | for value in self: |
paulb@22 | 735 | result.append(value) |
paulb@22 | 736 | for value in other: |
paulb@22 | 737 | result.append(value) |
paulb@22 | 738 | return result |
paulb@22 | 739 | |
paulb@22 | 740 | def __str__(self): |
paulb@22 | 741 | output = ["("] |
paulb@22 | 742 | n = self |
paulb@22 | 743 | first = 1 |
paulb@22 | 744 | while n.next is not None: |
paulb@22 | 745 | if not first: |
paulb@22 | 746 | output.append(", ") |
paulb@22 | 747 | else: |
paulb@22 | 748 | first = 0 |
paulb@22 | 749 | output.append(str(n.value)) |
paulb@22 | 750 | n = n.next |
paulb@22 | 751 | output.append(")") |
paulb@22 | 752 | return "".join(output) |
paulb@22 | 753 | |
paulb@22 | 754 | def __iter__(self): |
paulb@22 | 755 | return tupleiterator(self) |
paulb@22 | 756 | |
paulb@130 | 757 | def __bool__(self): |
paulb@22 | 758 | return self.__len__() != 0 |
paulb@22 | 759 | |
paulb@22 | 760 | class tupleiterator: |
paulb@22 | 761 | def __init__(self, l): |
paulb@22 | 762 | self.l = l |
paulb@22 | 763 | |
paulb@22 | 764 | def next(self): |
paulb@22 | 765 | l = self.l |
paulb@22 | 766 | next = l.next |
paulb@22 | 767 | if next is not None: |
paulb@22 | 768 | self.l = next |
paulb@22 | 769 | return l.value |
paulb@22 | 770 | else: |
paulb@55 | 771 | raise StopIteration |
paulb@22 | 772 | |
paulb@130 | 773 | def __bool__(self): |
paulb@130 | 774 | return bool() |
paulb@22 | 775 | |
paulb@105 | 776 | class xrange: |
paulb@105 | 777 | def __init__(self, start, end, step=1): |
paulb@105 | 778 | self.start = start |
paulb@105 | 779 | self.end = end |
paulb@105 | 780 | self.step = step |
paulb@105 | 781 | self.current = start |
paulb@105 | 782 | |
paulb@105 | 783 | def __iter__(self): |
paulb@105 | 784 | return self |
paulb@105 | 785 | |
paulb@105 | 786 | def next(self): |
paulb@105 | 787 | if self.current >= self.end: |
paulb@105 | 788 | raise StopIteration |
paulb@105 | 789 | current = self.current |
paulb@105 | 790 | self.current += self.step |
paulb@105 | 791 | return current |
paulb@105 | 792 | |
paulb@145 | 793 | class Exception: |
paulb@145 | 794 | pass |
paulb@145 | 795 | |
paulb@145 | 796 | class AttributeError(Exception): |
paulb@145 | 797 | pass |
paulb@145 | 798 | |
paulb@145 | 799 | class IndexError(Exception): |
paulb@145 | 800 | pass |
paulb@145 | 801 | |
paulb@145 | 802 | class StopIteration(Exception): |
paulb@145 | 803 | pass |
paulb@145 | 804 | |
paulb@145 | 805 | class TypeError(Exception): |
paulb@145 | 806 | pass |
paulb@145 | 807 | |
paulb@105 | 808 | # General functions. |
paulb@105 | 809 | |
paulb@22 | 810 | def isinstance(obj, cls): |
paulb@130 | 811 | return bool() |
paulb@22 | 812 | |
paulb@22 | 813 | def issubclass(cls1, cls2): |
paulb@130 | 814 | return bool() |
paulb@22 | 815 | |
paulb@22 | 816 | def len(x): |
paulb@22 | 817 | return x.__len__() |
paulb@22 | 818 | |
paulb@22 | 819 | def max(*l): |
paulb@22 | 820 | max_so_far = l[0] |
paulb@22 | 821 | for i in l[1:]: |
paulb@22 | 822 | if i > max_so_far: |
paulb@22 | 823 | max_so_far = i |
paulb@22 | 824 | return max_so_far |
paulb@22 | 825 | |
paulb@105 | 826 | def range(start, end, step=None): |
paulb@105 | 827 | if start == end: |
paulb@105 | 828 | return [] |
paulb@105 | 829 | elif start > end: |
paulb@105 | 830 | step = step or 1 |
paulb@105 | 831 | i = start |
paulb@105 | 832 | result = [] |
paulb@105 | 833 | while i < end: |
paulb@105 | 834 | result.append(i) |
paulb@105 | 835 | i += 1 |
paulb@105 | 836 | return result |
paulb@105 | 837 | else: |
paulb@105 | 838 | step = step or -1 |
paulb@105 | 839 | i = start |
paulb@105 | 840 | result = [] |
paulb@105 | 841 | while i > end: |
paulb@105 | 842 | result.append(i) |
paulb@105 | 843 | i -= 1 |
paulb@105 | 844 | return result |
paulb@105 | 845 | |
paulb@129 | 846 | # Special values. |
paulb@22 | 847 | |
paulb@130 | 848 | True = bool() |
paulb@130 | 849 | False = bool() |
paulb@22 | 850 | None = none() |
paulb@145 | 851 | stdin = file() |
paulb@145 | 852 | stdout = file() |
paulb@145 | 853 | stderr = file() |
paulb@22 | 854 | |
paulb@22 | 855 | # Special functions. These all operate on references at run-time. |
paulb@22 | 856 | |
paulb@22 | 857 | def __is__(a, b): |
paulb@130 | 858 | return bool() |
paulb@22 | 859 | |
paulb@22 | 860 | def __not__(a): |
paulb@130 | 861 | return bool() |
paulb@22 | 862 | |
paulb@22 | 863 | # vim: tabstop=4 expandtab shiftwidth=4 |