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