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@167 | 57 | def __init__(self, *args): |
paulb@167 | 58 | for key, value in args: |
paulb@167 | 59 | self[key] = value |
paulb@167 | 60 | |
paulb@167 | 61 | def __setitem__(self, key, value): |
paulb@167 | 62 | self.key = key |
paulb@167 | 63 | self.value = value |
paulb@167 | 64 | |
paulb@167 | 65 | def __getitem__(self, key): |
paulb@167 | 66 | return self.value |
paulb@22 | 67 | |
paulb@145 | 68 | class file: |
paulb@145 | 69 | def write(self, s): |
paulb@145 | 70 | pass |
paulb@22 | 71 | |
paulb@22 | 72 | class float: |
paulb@22 | 73 | def __iadd__(self, other): |
paulb@22 | 74 | if isinstance(other, int): |
paulb@22 | 75 | return float() |
paulb@22 | 76 | elif isinstance(other, long): |
paulb@22 | 77 | return float() |
paulb@22 | 78 | elif isinstance(other, float): |
paulb@22 | 79 | return float() |
paulb@22 | 80 | else: |
paulb@55 | 81 | raise TypeError |
paulb@22 | 82 | |
paulb@22 | 83 | def __isub__(self, other): |
paulb@22 | 84 | if isinstance(other, int): |
paulb@22 | 85 | return float() |
paulb@22 | 86 | elif isinstance(other, long): |
paulb@22 | 87 | return float() |
paulb@22 | 88 | elif isinstance(other, float): |
paulb@22 | 89 | return float() |
paulb@22 | 90 | else: |
paulb@55 | 91 | raise TypeError |
paulb@22 | 92 | |
paulb@22 | 93 | def __add__(self, other): |
paulb@22 | 94 | if isinstance(other, int): |
paulb@22 | 95 | return float() |
paulb@22 | 96 | elif isinstance(other, long): |
paulb@22 | 97 | return float() |
paulb@22 | 98 | elif isinstance(other, float): |
paulb@22 | 99 | return float() |
paulb@22 | 100 | else: |
paulb@55 | 101 | raise TypeError |
paulb@22 | 102 | |
paulb@22 | 103 | def __radd__(self, other): |
paulb@22 | 104 | if isinstance(other, int): |
paulb@22 | 105 | return float() |
paulb@22 | 106 | elif isinstance(other, long): |
paulb@22 | 107 | return float() |
paulb@22 | 108 | elif isinstance(other, float): |
paulb@22 | 109 | return float() |
paulb@22 | 110 | else: |
paulb@55 | 111 | raise TypeError |
paulb@22 | 112 | |
paulb@22 | 113 | def __sub__(self, other): |
paulb@22 | 114 | if isinstance(other, int): |
paulb@22 | 115 | return float() |
paulb@22 | 116 | elif isinstance(other, long): |
paulb@22 | 117 | return float() |
paulb@22 | 118 | elif isinstance(other, float): |
paulb@22 | 119 | return float() |
paulb@22 | 120 | else: |
paulb@55 | 121 | raise TypeError |
paulb@22 | 122 | |
paulb@22 | 123 | def __rsub__(self, other): |
paulb@22 | 124 | if isinstance(other, int): |
paulb@22 | 125 | return float() |
paulb@22 | 126 | elif isinstance(other, long): |
paulb@22 | 127 | return float() |
paulb@22 | 128 | elif isinstance(other, float): |
paulb@22 | 129 | return float() |
paulb@22 | 130 | else: |
paulb@55 | 131 | raise TypeError |
paulb@22 | 132 | |
paulb@22 | 133 | def __mul__(self, other): |
paulb@22 | 134 | if isinstance(other, int): |
paulb@22 | 135 | return float() |
paulb@22 | 136 | elif isinstance(other, long): |
paulb@22 | 137 | return float() |
paulb@22 | 138 | elif isinstance(other, float): |
paulb@22 | 139 | return float() |
paulb@22 | 140 | else: |
paulb@55 | 141 | raise TypeError |
paulb@22 | 142 | |
paulb@22 | 143 | def __rmul__(self, other): |
paulb@22 | 144 | if isinstance(other, int): |
paulb@22 | 145 | return float() |
paulb@22 | 146 | elif isinstance(other, long): |
paulb@22 | 147 | return float() |
paulb@22 | 148 | elif isinstance(other, float): |
paulb@22 | 149 | return float() |
paulb@22 | 150 | else: |
paulb@55 | 151 | raise TypeError |
paulb@22 | 152 | |
paulb@22 | 153 | def __div__(self, other): |
paulb@22 | 154 | if isinstance(other, int): |
paulb@22 | 155 | return float() |
paulb@22 | 156 | elif isinstance(other, long): |
paulb@22 | 157 | return float() |
paulb@22 | 158 | elif isinstance(other, float): |
paulb@22 | 159 | return float() |
paulb@22 | 160 | else: |
paulb@55 | 161 | raise TypeError |
paulb@22 | 162 | |
paulb@22 | 163 | def __rdiv__(self, other): |
paulb@22 | 164 | if isinstance(other, int): |
paulb@22 | 165 | return float() |
paulb@22 | 166 | elif isinstance(other, long): |
paulb@22 | 167 | return float() |
paulb@22 | 168 | elif isinstance(other, float): |
paulb@22 | 169 | return float() |
paulb@22 | 170 | else: |
paulb@55 | 171 | raise TypeError |
paulb@22 | 172 | |
paulb@136 | 173 | def __floordiv__(self, other): |
paulb@136 | 174 | if isinstance(other, int): |
paulb@136 | 175 | return float() |
paulb@136 | 176 | elif isinstance(other, long): |
paulb@136 | 177 | return float() |
paulb@136 | 178 | elif isinstance(other, float): |
paulb@136 | 179 | return float() |
paulb@136 | 180 | else: |
paulb@136 | 181 | raise TypeError |
paulb@136 | 182 | |
paulb@136 | 183 | def __rfloordiv__(self, other): |
paulb@136 | 184 | if isinstance(other, int): |
paulb@136 | 185 | return float() |
paulb@136 | 186 | elif isinstance(other, long): |
paulb@136 | 187 | return float() |
paulb@136 | 188 | elif isinstance(other, float): |
paulb@136 | 189 | return float() |
paulb@136 | 190 | else: |
paulb@136 | 191 | raise TypeError |
paulb@136 | 192 | |
paulb@22 | 193 | def __pow__(self, other): |
paulb@22 | 194 | if isinstance(other, int): |
paulb@22 | 195 | return float() |
paulb@22 | 196 | elif isinstance(other, long): |
paulb@22 | 197 | return float() |
paulb@22 | 198 | elif isinstance(other, float): |
paulb@22 | 199 | return float() |
paulb@22 | 200 | else: |
paulb@55 | 201 | raise TypeError |
paulb@22 | 202 | |
paulb@22 | 203 | def __rpow__(self, other): |
paulb@22 | 204 | if isinstance(other, int): |
paulb@22 | 205 | return float() |
paulb@22 | 206 | elif isinstance(other, long): |
paulb@22 | 207 | return float() |
paulb@22 | 208 | elif isinstance(other, float): |
paulb@22 | 209 | return float() |
paulb@22 | 210 | else: |
paulb@55 | 211 | raise TypeError |
paulb@22 | 212 | |
paulb@22 | 213 | def __lt__(self, other): |
paulb@22 | 214 | if isinstance(other, int): |
paulb@130 | 215 | return bool() |
paulb@22 | 216 | elif isinstance(other, long): |
paulb@130 | 217 | return bool() |
paulb@22 | 218 | elif isinstance(other, float): |
paulb@130 | 219 | return bool() |
paulb@22 | 220 | else: |
paulb@186 | 221 | return NotImplemented |
paulb@22 | 222 | |
paulb@22 | 223 | def __gt__(self, other): |
paulb@22 | 224 | if isinstance(other, int): |
paulb@130 | 225 | return bool() |
paulb@22 | 226 | elif isinstance(other, long): |
paulb@130 | 227 | return bool() |
paulb@22 | 228 | elif isinstance(other, float): |
paulb@130 | 229 | return bool() |
paulb@22 | 230 | else: |
paulb@186 | 231 | return NotImplemented |
paulb@22 | 232 | |
paulb@22 | 233 | def __le__(self, other): |
paulb@22 | 234 | if isinstance(other, int): |
paulb@130 | 235 | return bool() |
paulb@22 | 236 | elif isinstance(other, long): |
paulb@130 | 237 | return bool() |
paulb@22 | 238 | elif isinstance(other, float): |
paulb@130 | 239 | return bool() |
paulb@22 | 240 | else: |
paulb@186 | 241 | return NotImplemented |
paulb@22 | 242 | |
paulb@22 | 243 | def __ge__(self, other): |
paulb@22 | 244 | if isinstance(other, int): |
paulb@130 | 245 | return bool() |
paulb@22 | 246 | elif isinstance(other, long): |
paulb@130 | 247 | return bool() |
paulb@22 | 248 | elif isinstance(other, float): |
paulb@130 | 249 | return bool() |
paulb@22 | 250 | else: |
paulb@186 | 251 | return NotImplemented |
paulb@22 | 252 | |
paulb@22 | 253 | def __eq__(self, other): |
paulb@22 | 254 | if isinstance(other, int): |
paulb@130 | 255 | return bool() |
paulb@22 | 256 | elif isinstance(other, long): |
paulb@130 | 257 | return bool() |
paulb@22 | 258 | elif isinstance(other, float): |
paulb@130 | 259 | return bool() |
paulb@22 | 260 | else: |
paulb@186 | 261 | return NotImplemented |
paulb@22 | 262 | |
paulb@22 | 263 | def __ne__(self, other): |
paulb@22 | 264 | if isinstance(other, int): |
paulb@130 | 265 | return bool() |
paulb@22 | 266 | elif isinstance(other, long): |
paulb@130 | 267 | return bool() |
paulb@22 | 268 | elif isinstance(other, float): |
paulb@130 | 269 | return bool() |
paulb@22 | 270 | else: |
paulb@186 | 271 | return NotImplemented |
paulb@22 | 272 | |
paulb@22 | 273 | def __neg__(self): |
paulb@22 | 274 | return float() |
paulb@22 | 275 | |
paulb@22 | 276 | def __pos__(self): |
paulb@22 | 277 | return self |
paulb@22 | 278 | |
paulb@22 | 279 | def __str__(self): |
paulb@145 | 280 | return str() |
paulb@22 | 281 | |
paulb@130 | 282 | def __bool__(self): |
paulb@22 | 283 | return self != 0 |
paulb@22 | 284 | |
paulb@145 | 285 | class int: |
paulb@145 | 286 | def __iadd__(self, other): |
paulb@145 | 287 | if isinstance(other, int): |
paulb@145 | 288 | return int() |
paulb@145 | 289 | else: |
paulb@145 | 290 | raise TypeError |
paulb@145 | 291 | |
paulb@145 | 292 | def __isub__(self, other): |
paulb@145 | 293 | if isinstance(other, int): |
paulb@145 | 294 | return int() |
paulb@145 | 295 | else: |
paulb@145 | 296 | raise TypeError |
paulb@128 | 297 | |
paulb@22 | 298 | def __add__(self, other): |
paulb@145 | 299 | if isinstance(other, int): |
paulb@145 | 300 | return int() |
paulb@22 | 301 | else: |
paulb@55 | 302 | raise TypeError |
paulb@22 | 303 | |
paulb@22 | 304 | def __radd__(self, other): |
paulb@145 | 305 | if isinstance(other, int): |
paulb@145 | 306 | return int() |
paulb@145 | 307 | else: |
paulb@145 | 308 | raise TypeError |
paulb@145 | 309 | |
paulb@145 | 310 | def __sub__(self, other): |
paulb@145 | 311 | if isinstance(other, int): |
paulb@145 | 312 | return int() |
paulb@145 | 313 | else: |
paulb@145 | 314 | raise TypeError |
paulb@145 | 315 | |
paulb@145 | 316 | def __rsub__(self, other): |
paulb@145 | 317 | if isinstance(other, int): |
paulb@145 | 318 | return int() |
paulb@145 | 319 | else: |
paulb@145 | 320 | raise TypeError |
paulb@145 | 321 | |
paulb@145 | 322 | def __mul__(self, other): |
paulb@145 | 323 | if isinstance(other, int): |
paulb@145 | 324 | return int() |
paulb@145 | 325 | else: |
paulb@145 | 326 | raise TypeError |
paulb@145 | 327 | |
paulb@145 | 328 | def __rmul__(self, other): |
paulb@145 | 329 | if isinstance(other, int): |
paulb@145 | 330 | return int() |
paulb@145 | 331 | else: |
paulb@145 | 332 | raise TypeError |
paulb@145 | 333 | |
paulb@145 | 334 | def __div__(self, other): |
paulb@145 | 335 | if isinstance(other, int): |
paulb@145 | 336 | return int() |
paulb@145 | 337 | else: |
paulb@145 | 338 | raise TypeError |
paulb@145 | 339 | |
paulb@145 | 340 | def __rdiv__(self, other): |
paulb@145 | 341 | if isinstance(other, int): |
paulb@145 | 342 | return int() |
paulb@145 | 343 | else: |
paulb@145 | 344 | raise TypeError |
paulb@145 | 345 | |
paulb@145 | 346 | def __floordiv__(self, other): |
paulb@145 | 347 | if isinstance(other, int): |
paulb@145 | 348 | return int() |
paulb@145 | 349 | else: |
paulb@145 | 350 | raise TypeError |
paulb@145 | 351 | |
paulb@145 | 352 | def __rfloordiv__(self, other): |
paulb@145 | 353 | if isinstance(other, int): |
paulb@145 | 354 | return int() |
paulb@22 | 355 | else: |
paulb@55 | 356 | raise TypeError |
paulb@22 | 357 | |
paulb@145 | 358 | def __pow__(self, other): |
paulb@145 | 359 | if isinstance(other, int): |
paulb@145 | 360 | return int() |
paulb@145 | 361 | else: |
paulb@145 | 362 | raise TypeError |
paulb@145 | 363 | |
paulb@145 | 364 | def __lt__(self, other): |
paulb@145 | 365 | if isinstance(other, int): |
paulb@145 | 366 | return bool() |
paulb@145 | 367 | else: |
paulb@186 | 368 | return NotImplemented |
paulb@145 | 369 | |
paulb@145 | 370 | def __gt__(self, other): |
paulb@145 | 371 | if isinstance(other, int): |
paulb@145 | 372 | return bool() |
paulb@145 | 373 | else: |
paulb@186 | 374 | return NotImplemented |
paulb@145 | 375 | |
paulb@145 | 376 | def __le__(self, other): |
paulb@145 | 377 | if isinstance(other, int): |
paulb@145 | 378 | return bool() |
paulb@145 | 379 | else: |
paulb@186 | 380 | return NotImplemented |
paulb@145 | 381 | |
paulb@145 | 382 | def __ge__(self, other): |
paulb@145 | 383 | if isinstance(other, int): |
paulb@145 | 384 | return bool() |
paulb@145 | 385 | else: |
paulb@186 | 386 | return NotImplemented |
paulb@145 | 387 | |
paulb@145 | 388 | def __eq__(self, other): |
paulb@145 | 389 | if isinstance(other, int): |
paulb@145 | 390 | return bool() |
paulb@145 | 391 | else: |
paulb@186 | 392 | return NotImplemented |
paulb@145 | 393 | |
paulb@145 | 394 | def __ne__(self, other): |
paulb@145 | 395 | if isinstance(other, int): |
paulb@145 | 396 | return bool() |
paulb@145 | 397 | else: |
paulb@186 | 398 | return NotImplemented |
paulb@145 | 399 | |
paulb@145 | 400 | def __neg__(self): |
paulb@22 | 401 | return int() |
paulb@22 | 402 | |
paulb@145 | 403 | def __pos__(self): |
paulb@145 | 404 | return self |
paulb@145 | 405 | |
paulb@22 | 406 | def __str__(self): |
paulb@145 | 407 | return str() |
paulb@22 | 408 | |
paulb@130 | 409 | def __bool__(self): |
paulb@145 | 410 | return self != 0 |
paulb@22 | 411 | |
paulb@22 | 412 | class list: |
paulb@89 | 413 | def __init__(self, *args): |
paulb@89 | 414 | for arg in args: |
paulb@89 | 415 | self.append(arg) |
paulb@22 | 416 | |
paulb@22 | 417 | def __getitem__(self, index): |
paulb@167 | 418 | if -len(self) <= index < len(self): |
paulb@167 | 419 | return self.value |
paulb@22 | 420 | else: |
paulb@167 | 421 | raise IndexError, index |
paulb@22 | 422 | |
paulb@22 | 423 | def __setitem__(self, index, value): |
paulb@167 | 424 | if -len(self) <= index < len(self): |
paulb@167 | 425 | self.value = value |
paulb@22 | 426 | else: |
paulb@167 | 427 | raise IndexError, index |
paulb@22 | 428 | |
paulb@22 | 429 | def __getslice__(self, start, end=None): |
paulb@167 | 430 | if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end: |
paulb@167 | 431 | return list(self.value) |
paulb@167 | 432 | else: |
paulb@167 | 433 | raise IndexError, index |
paulb@22 | 434 | |
paulb@22 | 435 | def __setslice__(self, start, end, slice): |
paulb@167 | 436 | if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end: |
paulb@167 | 437 | for value in slice: |
paulb@167 | 438 | self.value = value |
paulb@167 | 439 | else: |
paulb@167 | 440 | raise IndexError, index |
paulb@22 | 441 | |
paulb@22 | 442 | def append(self, value): |
paulb@167 | 443 | self.value = value |
paulb@22 | 444 | |
paulb@22 | 445 | def __len__(self): |
paulb@167 | 446 | return int() |
paulb@22 | 447 | |
paulb@22 | 448 | def __add__(self, other): |
paulb@22 | 449 | for value in other: |
paulb@167 | 450 | self.value = value |
paulb@167 | 451 | return self |
paulb@22 | 452 | |
paulb@167 | 453 | __iadd__ = __add__ |
paulb@97 | 454 | |
paulb@22 | 455 | def __str__(self): |
paulb@167 | 456 | return str() |
paulb@22 | 457 | |
paulb@22 | 458 | def __iter__(self): |
paulb@22 | 459 | return listiterator(self) |
paulb@22 | 460 | |
paulb@130 | 461 | def __bool__(self): |
paulb@22 | 462 | return self.__len__() != 0 |
paulb@22 | 463 | |
paulb@22 | 464 | class listiterator: |
paulb@22 | 465 | def __init__(self, l): |
paulb@22 | 466 | self.l = l |
paulb@167 | 467 | self.index = 0 |
paulb@22 | 468 | |
paulb@22 | 469 | def next(self): |
paulb@167 | 470 | if self.index < len(self.l): |
paulb@167 | 471 | self.index += 1 |
paulb@167 | 472 | return self.l[self.index] |
paulb@22 | 473 | else: |
paulb@55 | 474 | raise StopIteration |
paulb@22 | 475 | |
paulb@130 | 476 | def __bool__(self): |
paulb@130 | 477 | return bool() |
paulb@22 | 478 | |
paulb@145 | 479 | class long: |
paulb@145 | 480 | def __iadd__(self, other): |
paulb@145 | 481 | if isinstance(other, int): |
paulb@145 | 482 | return long() |
paulb@145 | 483 | elif isinstance(other, long): |
paulb@145 | 484 | return long() |
paulb@145 | 485 | else: |
paulb@145 | 486 | raise TypeError |
paulb@145 | 487 | |
paulb@145 | 488 | def __isub__(self, other): |
paulb@145 | 489 | if isinstance(other, int): |
paulb@145 | 490 | return long() |
paulb@145 | 491 | elif isinstance(other, long): |
paulb@145 | 492 | return long() |
paulb@145 | 493 | else: |
paulb@145 | 494 | raise TypeError |
paulb@145 | 495 | |
paulb@145 | 496 | def __add__(self, other): |
paulb@145 | 497 | if isinstance(other, int): |
paulb@145 | 498 | return long() |
paulb@145 | 499 | elif isinstance(other, long): |
paulb@145 | 500 | return long() |
paulb@145 | 501 | else: |
paulb@145 | 502 | raise TypeError |
paulb@145 | 503 | |
paulb@145 | 504 | def __radd__(self, other): |
paulb@145 | 505 | if isinstance(other, int): |
paulb@145 | 506 | return long() |
paulb@145 | 507 | elif isinstance(other, long): |
paulb@145 | 508 | return long() |
paulb@145 | 509 | else: |
paulb@145 | 510 | raise TypeError |
paulb@145 | 511 | |
paulb@145 | 512 | def __sub__(self, other): |
paulb@145 | 513 | if isinstance(other, int): |
paulb@145 | 514 | return long() |
paulb@145 | 515 | elif isinstance(other, long): |
paulb@145 | 516 | return long() |
paulb@145 | 517 | else: |
paulb@145 | 518 | raise TypeError |
paulb@145 | 519 | |
paulb@145 | 520 | def __rsub__(self, other): |
paulb@145 | 521 | if isinstance(other, int): |
paulb@145 | 522 | return long() |
paulb@145 | 523 | elif isinstance(other, long): |
paulb@145 | 524 | return long() |
paulb@145 | 525 | else: |
paulb@145 | 526 | raise TypeError |
paulb@145 | 527 | |
paulb@145 | 528 | def __lt__(self, other): |
paulb@145 | 529 | if isinstance(other, int): |
paulb@145 | 530 | return bool() |
paulb@145 | 531 | elif isinstance(other, long): |
paulb@145 | 532 | return bool() |
paulb@145 | 533 | else: |
paulb@186 | 534 | return NotImplemented |
paulb@145 | 535 | |
paulb@145 | 536 | def __gt__(self, other): |
paulb@145 | 537 | if isinstance(other, int): |
paulb@145 | 538 | return bool() |
paulb@145 | 539 | elif isinstance(other, long): |
paulb@145 | 540 | return bool() |
paulb@145 | 541 | else: |
paulb@186 | 542 | return NotImplemented |
paulb@145 | 543 | |
paulb@145 | 544 | def __le__(self, other): |
paulb@145 | 545 | if isinstance(other, int): |
paulb@145 | 546 | return bool() |
paulb@145 | 547 | elif isinstance(other, long): |
paulb@145 | 548 | return bool() |
paulb@145 | 549 | else: |
paulb@186 | 550 | return NotImplemented |
paulb@145 | 551 | |
paulb@145 | 552 | def __ge__(self, other): |
paulb@145 | 553 | if isinstance(other, int): |
paulb@145 | 554 | return bool() |
paulb@145 | 555 | elif isinstance(other, long): |
paulb@145 | 556 | return bool() |
paulb@145 | 557 | else: |
paulb@186 | 558 | return NotImplemented |
paulb@145 | 559 | |
paulb@145 | 560 | def __eq__(self, other): |
paulb@145 | 561 | if isinstance(other, int): |
paulb@145 | 562 | return bool() |
paulb@145 | 563 | elif isinstance(other, long): |
paulb@145 | 564 | return bool() |
paulb@145 | 565 | else: |
paulb@186 | 566 | return NotImplemented |
paulb@145 | 567 | |
paulb@145 | 568 | def __ne__(self, other): |
paulb@145 | 569 | if isinstance(other, int): |
paulb@145 | 570 | return bool() |
paulb@145 | 571 | elif isinstance(other, long): |
paulb@145 | 572 | return bool() |
paulb@145 | 573 | else: |
paulb@186 | 574 | return NotImplemented |
paulb@145 | 575 | |
paulb@145 | 576 | def __neg__(self): |
paulb@145 | 577 | return long() |
paulb@145 | 578 | |
paulb@145 | 579 | def __pos__(self): |
paulb@145 | 580 | return self |
paulb@145 | 581 | |
paulb@145 | 582 | def __str__(self): |
paulb@145 | 583 | return str() |
paulb@145 | 584 | |
paulb@145 | 585 | def __bool__(self): |
paulb@145 | 586 | return self != 0 |
paulb@145 | 587 | |
paulb@145 | 588 | class none: |
paulb@145 | 589 | def __bool__(self): |
paulb@145 | 590 | return False |
paulb@145 | 591 | |
paulb@145 | 592 | def __str__(self): |
paulb@145 | 593 | return "None" |
paulb@145 | 594 | |
paulb@145 | 595 | class str: |
paulb@145 | 596 | def __init__(self, x=None): |
paulb@145 | 597 | x.__str__() |
paulb@145 | 598 | |
paulb@145 | 599 | def __add__(self, other): |
paulb@145 | 600 | if isinstance(other, str): |
paulb@145 | 601 | return str() |
paulb@145 | 602 | else: |
paulb@145 | 603 | raise TypeError |
paulb@145 | 604 | |
paulb@145 | 605 | def __radd__(self, other): |
paulb@145 | 606 | if isinstance(other, str): |
paulb@145 | 607 | return str() |
paulb@145 | 608 | else: |
paulb@145 | 609 | raise TypeError |
paulb@145 | 610 | |
paulb@145 | 611 | def __len__(self): |
paulb@145 | 612 | return int() |
paulb@145 | 613 | |
paulb@145 | 614 | def __str__(self): |
paulb@145 | 615 | return self |
paulb@145 | 616 | |
paulb@145 | 617 | def __bool__(self): |
paulb@145 | 618 | return self.__len__() != 0 |
paulb@145 | 619 | |
paulb@145 | 620 | def join(self, l): |
paulb@145 | 621 | total = 0 |
paulb@145 | 622 | first = 1 |
paulb@145 | 623 | self_len = self.__len__() |
paulb@145 | 624 | for i in l: |
paulb@145 | 625 | if not first: |
paulb@145 | 626 | total += self_len |
paulb@145 | 627 | total += len(str(i)) |
paulb@145 | 628 | first = 0 |
paulb@145 | 629 | b = buffer(total) |
paulb@145 | 630 | first = 1 |
paulb@145 | 631 | for i in l: |
paulb@145 | 632 | if not first: |
paulb@145 | 633 | b.append(self) |
paulb@145 | 634 | b.append(str(i)) |
paulb@145 | 635 | first = 0 |
paulb@145 | 636 | s = str(b) |
paulb@145 | 637 | return s |
paulb@145 | 638 | |
paulb@22 | 639 | class tuple: |
paulb@66 | 640 | def __init__(self, *args): |
paulb@66 | 641 | for arg in args: |
paulb@66 | 642 | self.append(arg) |
paulb@22 | 643 | |
paulb@22 | 644 | def __getitem__(self, index): |
paulb@167 | 645 | if -len(self) <= index < len(self): |
paulb@167 | 646 | return self.value |
paulb@22 | 647 | else: |
paulb@167 | 648 | raise IndexError, index |
paulb@167 | 649 | |
paulb@167 | 650 | def __setitem__(self, index, value): |
paulb@167 | 651 | if -len(self) <= index < len(self): |
paulb@167 | 652 | self.value = value |
paulb@167 | 653 | else: |
paulb@167 | 654 | raise IndexError, index |
paulb@22 | 655 | |
paulb@22 | 656 | def __getslice__(self, start, end=None): |
paulb@167 | 657 | if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end: |
paulb@167 | 658 | return list(self.value) |
paulb@167 | 659 | else: |
paulb@167 | 660 | raise IndexError, index |
paulb@167 | 661 | |
paulb@167 | 662 | def __setslice__(self, start, end, slice): |
paulb@167 | 663 | if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end: |
paulb@167 | 664 | for value in slice: |
paulb@167 | 665 | self.value = value |
paulb@167 | 666 | else: |
paulb@167 | 667 | raise IndexError, index |
paulb@22 | 668 | |
paulb@22 | 669 | # NOTE: The append method should be internal at most. |
paulb@22 | 670 | |
paulb@22 | 671 | def append(self, value): |
paulb@167 | 672 | self.value = value |
paulb@22 | 673 | |
paulb@22 | 674 | def __len__(self): |
paulb@167 | 675 | return int() |
paulb@22 | 676 | |
paulb@22 | 677 | def __add__(self, other): |
paulb@22 | 678 | for value in other: |
paulb@167 | 679 | self.value = value |
paulb@167 | 680 | return self |
paulb@22 | 681 | |
paulb@22 | 682 | def __str__(self): |
paulb@167 | 683 | return str() |
paulb@22 | 684 | |
paulb@22 | 685 | def __iter__(self): |
paulb@22 | 686 | return tupleiterator(self) |
paulb@22 | 687 | |
paulb@130 | 688 | def __bool__(self): |
paulb@22 | 689 | return self.__len__() != 0 |
paulb@22 | 690 | |
paulb@22 | 691 | class tupleiterator: |
paulb@22 | 692 | def __init__(self, l): |
paulb@22 | 693 | self.l = l |
paulb@167 | 694 | self.index = 0 |
paulb@22 | 695 | |
paulb@22 | 696 | def next(self): |
paulb@167 | 697 | if self.index < len(self.l): |
paulb@167 | 698 | self.index += 1 |
paulb@167 | 699 | return self.l[self.index] |
paulb@22 | 700 | else: |
paulb@55 | 701 | raise StopIteration |
paulb@22 | 702 | |
paulb@130 | 703 | def __bool__(self): |
paulb@130 | 704 | return bool() |
paulb@22 | 705 | |
paulb@105 | 706 | class xrange: |
paulb@105 | 707 | def __init__(self, start, end, step=1): |
paulb@105 | 708 | self.start = start |
paulb@105 | 709 | self.end = end |
paulb@105 | 710 | self.step = step |
paulb@105 | 711 | self.current = start |
paulb@105 | 712 | |
paulb@105 | 713 | def __iter__(self): |
paulb@105 | 714 | return self |
paulb@105 | 715 | |
paulb@105 | 716 | def next(self): |
paulb@105 | 717 | if self.current >= self.end: |
paulb@105 | 718 | raise StopIteration |
paulb@105 | 719 | current = self.current |
paulb@105 | 720 | self.current += self.step |
paulb@105 | 721 | return current |
paulb@105 | 722 | |
paulb@145 | 723 | class Exception: |
paulb@167 | 724 | def __init__(self, *args): |
paulb@167 | 725 | pass |
paulb@145 | 726 | |
paulb@145 | 727 | class AttributeError(Exception): |
paulb@145 | 728 | pass |
paulb@145 | 729 | |
paulb@145 | 730 | class IndexError(Exception): |
paulb@145 | 731 | pass |
paulb@145 | 732 | |
paulb@145 | 733 | class StopIteration(Exception): |
paulb@145 | 734 | pass |
paulb@145 | 735 | |
paulb@145 | 736 | class TypeError(Exception): |
paulb@145 | 737 | pass |
paulb@145 | 738 | |
paulb@186 | 739 | class NotImplementedType: |
paulb@186 | 740 | pass |
paulb@186 | 741 | |
paulb@105 | 742 | # General functions. |
paulb@105 | 743 | |
paulb@22 | 744 | def isinstance(obj, cls): |
paulb@130 | 745 | return bool() |
paulb@22 | 746 | |
paulb@22 | 747 | def issubclass(cls1, cls2): |
paulb@130 | 748 | return bool() |
paulb@22 | 749 | |
paulb@22 | 750 | def len(x): |
paulb@22 | 751 | return x.__len__() |
paulb@22 | 752 | |
paulb@22 | 753 | def max(*l): |
paulb@22 | 754 | max_so_far = l[0] |
paulb@22 | 755 | for i in l[1:]: |
paulb@22 | 756 | if i > max_so_far: |
paulb@22 | 757 | max_so_far = i |
paulb@22 | 758 | return max_so_far |
paulb@22 | 759 | |
paulb@105 | 760 | def range(start, end, step=None): |
paulb@105 | 761 | if start == end: |
paulb@105 | 762 | return [] |
paulb@105 | 763 | elif start > end: |
paulb@105 | 764 | step = step or 1 |
paulb@105 | 765 | i = start |
paulb@105 | 766 | result = [] |
paulb@105 | 767 | while i < end: |
paulb@105 | 768 | result.append(i) |
paulb@105 | 769 | i += 1 |
paulb@105 | 770 | return result |
paulb@105 | 771 | else: |
paulb@105 | 772 | step = step or -1 |
paulb@105 | 773 | i = start |
paulb@105 | 774 | result = [] |
paulb@105 | 775 | while i > end: |
paulb@105 | 776 | result.append(i) |
paulb@105 | 777 | i -= 1 |
paulb@105 | 778 | return result |
paulb@105 | 779 | |
paulb@129 | 780 | # Special values. |
paulb@22 | 781 | |
paulb@130 | 782 | True = bool() |
paulb@130 | 783 | False = bool() |
paulb@22 | 784 | None = none() |
paulb@145 | 785 | stdin = file() |
paulb@145 | 786 | stdout = file() |
paulb@145 | 787 | stderr = file() |
paulb@186 | 788 | NotImplemented = NotImplementedType() |
paulb@22 | 789 | |
paulb@22 | 790 | # Special functions. These all operate on references at run-time. |
paulb@22 | 791 | |
paulb@22 | 792 | def __is__(a, b): |
paulb@130 | 793 | return bool() |
paulb@22 | 794 | |
paulb@22 | 795 | def __not__(a): |
paulb@130 | 796 | return bool() |
paulb@22 | 797 | |
paulb@22 | 798 | # vim: tabstop=4 expandtab shiftwidth=4 |