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