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 __mod__(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 __pow__(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 __rpow__(self, other): 221 if isinstance(other, int): 222 return float() 223 elif isinstance(other, long): 224 return float() 225 elif isinstance(other, float): 226 return float() 227 else: 228 raise TypeError 229 230 def __lt__(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 __gt__(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 __le__(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 __ge__(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 __eq__(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 __ne__(self, other): 281 if isinstance(other, int): 282 return bool() 283 elif isinstance(other, long): 284 return bool() 285 elif isinstance(other, float): 286 return bool() 287 else: 288 return NotImplemented 289 290 def __neg__(self): 291 return float() 292 293 def __pos__(self): 294 return self 295 296 def __str__(self): 297 return str() 298 299 def __bool__(self): 300 return self != 0 301 302 class int: 303 __atomic__ = object 304 305 def __init__(self, number_or_string=None): 306 pass 307 308 def __iadd__(self, other): 309 if isinstance(other, int): 310 return int() 311 else: 312 raise TypeError 313 314 def __isub__(self, other): 315 if isinstance(other, int): 316 return int() 317 else: 318 raise TypeError 319 320 def __add__(self, other): 321 if isinstance(other, int): 322 return int() 323 else: 324 raise TypeError 325 326 def __radd__(self, other): 327 if isinstance(other, int): 328 return int() 329 else: 330 raise TypeError 331 332 def __sub__(self, other): 333 if isinstance(other, int): 334 return int() 335 else: 336 raise TypeError 337 338 def __rsub__(self, other): 339 if isinstance(other, int): 340 return int() 341 else: 342 raise TypeError 343 344 def __mul__(self, other): 345 if isinstance(other, int): 346 return int() 347 else: 348 raise TypeError 349 350 def __rmul__(self, other): 351 if isinstance(other, int): 352 return int() 353 else: 354 raise TypeError 355 356 def __div__(self, other): 357 if isinstance(other, int): 358 return int() 359 else: 360 raise TypeError 361 362 def __rdiv__(self, other): 363 if isinstance(other, int): 364 return int() 365 else: 366 raise TypeError 367 368 def __floordiv__(self, other): 369 if isinstance(other, int): 370 return int() 371 else: 372 raise TypeError 373 374 def __rfloordiv__(self, other): 375 if isinstance(other, int): 376 return int() 377 else: 378 raise TypeError 379 380 def __mod__(self, other): 381 if isinstance(other, int): 382 return int() 383 else: 384 raise TypeError 385 386 def __pow__(self, other): 387 if isinstance(other, int): 388 return int() 389 else: 390 raise TypeError 391 392 def __and__(self, other): 393 if isinstance(other, int): 394 return int() 395 else: 396 raise TypeError 397 398 def __rand__(self, other): 399 if isinstance(other, int): 400 return int() 401 else: 402 raise TypeError 403 404 def __or__(self, other): 405 if isinstance(other, int): 406 return int() 407 else: 408 raise TypeError 409 410 def __ror__(self, other): 411 if isinstance(other, int): 412 return int() 413 else: 414 raise TypeError 415 416 def __xor__(self, other): 417 if isinstance(other, int): 418 return int() 419 else: 420 raise TypeError 421 422 def __rxor__(self, other): 423 if isinstance(other, int): 424 return int() 425 else: 426 raise TypeError 427 428 def __lt__(self, other): 429 if isinstance(other, int): 430 return bool() 431 else: 432 return NotImplemented 433 434 def __gt__(self, other): 435 if isinstance(other, int): 436 return bool() 437 else: 438 return NotImplemented 439 440 def __le__(self, other): 441 if isinstance(other, int): 442 return bool() 443 else: 444 return NotImplemented 445 446 def __ge__(self, other): 447 if isinstance(other, int): 448 return bool() 449 else: 450 return NotImplemented 451 452 def __eq__(self, other): 453 if isinstance(other, int): 454 return bool() 455 else: 456 return NotImplemented 457 458 def __ne__(self, other): 459 if isinstance(other, int): 460 return bool() 461 else: 462 return NotImplemented 463 464 def __neg__(self): 465 return int() 466 467 def __pos__(self): 468 return self 469 470 def __str__(self): 471 return str() 472 473 def __bool__(self): 474 return self != 0 475 476 class list: 477 def __init__(self, args=None): 478 if args is not None: 479 for arg in args: 480 self.append(arg) 481 482 def __getitem__(self, index): 483 if -len(self) <= index < len(self): 484 return self.value 485 else: 486 raise IndexError, index 487 488 def __setitem__(self, index, value): 489 if -len(self) <= index < len(self): 490 self.value = value 491 else: 492 raise IndexError, index 493 494 def __getslice__(self, start, end=None): 495 if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end: 496 return [self.value] 497 else: 498 return [] 499 500 def __setslice__(self, start, end, slice): 501 #if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end: 502 for value in slice: 503 self.value = value 504 505 def append(self, value): 506 self.value = value 507 508 def __len__(self): 509 return int() 510 511 def __add__(self, other): 512 for value in other: 513 self.value = value 514 return self 515 516 __iadd__ = __add__ 517 518 def __str__(self): 519 return str() 520 521 def __iter__(self): 522 return listiterator(self) 523 524 def __bool__(self): 525 return self.__len__() != 0 526 527 class listiterator: 528 def __init__(self, l): 529 self.l = l 530 self.index = 0 531 532 def next(self): 533 if self.index < len(self.l): 534 self.index += 1 535 return self.l[self.index] 536 else: 537 raise StopIteration 538 539 def __bool__(self): 540 return bool() 541 542 class long: 543 __atomic__ = object 544 545 def __init__(self, number_or_string=None): 546 pass 547 548 def __iadd__(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 __isub__(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 __add__(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 __radd__(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 __sub__(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 __rsub__(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 __mul__(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 __rmul__(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 __div__(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 __rdiv__(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 __floordiv__(self, other): 629 if isinstance(other, int): 630 return long() 631 elif isinstance(other, long): 632 return long() 633 else: 634 raise TypeError 635 636 def __rfloordiv__(self, other): 637 if isinstance(other, int): 638 return long() 639 elif isinstance(other, long): 640 return long() 641 else: 642 raise TypeError 643 644 def __and__(self, other): 645 if isinstance(other, int): 646 return long() 647 elif isinstance(other, long): 648 return long() 649 else: 650 raise TypeError 651 652 def __rand__(self, other): 653 if isinstance(other, int): 654 return long() 655 elif isinstance(other, long): 656 return long() 657 else: 658 raise TypeError 659 660 def __or__(self, other): 661 if isinstance(other, int): 662 return long() 663 elif isinstance(other, long): 664 return long() 665 else: 666 raise TypeError 667 668 def __ror__(self, other): 669 if isinstance(other, int): 670 return long() 671 elif isinstance(other, long): 672 return long() 673 else: 674 raise TypeError 675 676 def __xor__(self, other): 677 if isinstance(other, int): 678 return long() 679 elif isinstance(other, long): 680 return long() 681 else: 682 raise TypeError 683 684 def __rxor__(self, other): 685 if isinstance(other, int): 686 return long() 687 elif isinstance(other, long): 688 return long() 689 else: 690 raise TypeError 691 692 def __lt__(self, other): 693 if isinstance(other, int): 694 return bool() 695 elif isinstance(other, long): 696 return bool() 697 else: 698 return NotImplemented 699 700 def __gt__(self, other): 701 if isinstance(other, int): 702 return bool() 703 elif isinstance(other, long): 704 return bool() 705 else: 706 return NotImplemented 707 708 def __le__(self, other): 709 if isinstance(other, int): 710 return bool() 711 elif isinstance(other, long): 712 return bool() 713 else: 714 return NotImplemented 715 716 def __ge__(self, other): 717 if isinstance(other, int): 718 return bool() 719 elif isinstance(other, long): 720 return bool() 721 else: 722 return NotImplemented 723 724 def __eq__(self, other): 725 if isinstance(other, int): 726 return bool() 727 elif isinstance(other, long): 728 return bool() 729 else: 730 return NotImplemented 731 732 def __ne__(self, other): 733 if isinstance(other, int): 734 return bool() 735 elif isinstance(other, long): 736 return bool() 737 else: 738 return NotImplemented 739 740 def __neg__(self): 741 return long() 742 743 def __pos__(self): 744 return self 745 746 def __str__(self): 747 return str() 748 749 def __bool__(self): 750 return self != 0 751 752 class none: 753 __atomic__ = object 754 755 def __bool__(self): 756 return False 757 758 def __str__(self): 759 return "None" 760 761 class slice: 762 def __init__(self, start_or_end, end=None, step=None): 763 if end is None: 764 self.start = 0 765 self.end = start_or_end 766 else: 767 self.start = start_or_end 768 self.end = end 769 self.step = step 770 771 class str: 772 __atomic__ = object 773 774 def __init__(self, x=None): 775 x.__str__() 776 777 def __iadd__(self, other): 778 if isinstance(other, str): 779 return str() 780 else: 781 raise TypeError 782 783 def __add__(self, other): 784 if isinstance(other, str): 785 return str() 786 else: 787 raise TypeError 788 789 def __radd__(self, other): 790 if isinstance(other, str): 791 return str() 792 else: 793 raise TypeError 794 795 def __mul__(self, other): 796 if isinstance(other, int) or isinstance(other, long): 797 return str() 798 else: 799 raise TypeError 800 801 def __radd__(self, other): 802 if isinstance(other, int) or isinstance(other, long): 803 return str() 804 else: 805 raise TypeError 806 807 def __mod__(self, other): 808 809 "The format operator for strings." 810 811 return str() 812 813 def __lt__(self, other): 814 if isinstance(other, str): 815 return bool() 816 else: 817 return NotImplemented 818 819 def __gt__(self, other): 820 if isinstance(other, str): 821 return bool() 822 else: 823 return NotImplemented 824 825 def __le__(self, other): 826 if isinstance(other, str): 827 return bool() 828 else: 829 return NotImplemented 830 831 def __ge__(self, other): 832 if isinstance(other, str): 833 return bool() 834 else: 835 return NotImplemented 836 837 def __eq__(self, other): 838 if isinstance(other, str): 839 return bool() 840 else: 841 return NotImplemented 842 843 def __ne__(self, other): 844 if isinstance(other, str): 845 return bool() 846 else: 847 return NotImplemented 848 849 def __len__(self): 850 return int() 851 852 def __str__(self): 853 return self 854 855 def __bool__(self): 856 return self.__len__() != 0 857 858 def join(self, l): 859 total = 0 860 first = 1 861 self_len = self.__len__() 862 for i in l: 863 if not first: 864 total += self_len 865 total += len(str(i)) 866 first = 0 867 b = buffer(total) 868 first = 1 869 for i in l: 870 if not first: 871 b.append(self) 872 b.append(str(i)) 873 first = 0 874 s = str(b) 875 return s 876 877 class tuple: 878 def __init__(self, args): 879 for arg in args: 880 self.value = arg 881 882 def __getitem__(self, index): 883 if -len(self) <= index < len(self): 884 return self.value 885 else: 886 raise IndexError, index 887 888 def __getslice__(self, start, end=None): 889 if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end: 890 return (self.value,) 891 else: 892 return () 893 894 def __len__(self): 895 return int() 896 897 def __add__(self, other): 898 result = list(self) 899 for value in other: 900 result.append(value) 901 return tuple(result) 902 903 def __str__(self): 904 return str() 905 906 def __iter__(self): 907 return tupleiterator(self) 908 909 def __bool__(self): 910 return self.__len__() != 0 911 912 class tupleiterator: 913 def __init__(self, l): 914 self.l = l 915 self.index = 0 916 917 def next(self): 918 if self.index < len(self.l): 919 self.index += 1 920 return self.l[self.index] 921 else: 922 raise StopIteration 923 924 def __bool__(self): 925 return bool() 926 927 class xrange: 928 def __init__(self, start_or_end, end=None, step=1): 929 if end is None: 930 self.start = 0 931 self.end = start_or_end 932 else: 933 self.start = start_or_end 934 self.end = end 935 self.step = step 936 self.current = self.start 937 938 def __iter__(self): 939 return self 940 941 def next(self): 942 if self.current >= self.end: 943 raise StopIteration 944 current = self.current 945 self.current += self.step 946 return current 947 948 class Exception: 949 def __init__(self, *args): 950 pass 951 952 class AssertionError(Exception): 953 __atomic__ = object 954 955 class AttributeError(Exception): 956 __atomic__ = object 957 958 class IndexError(Exception): 959 __atomic__ = object 960 961 class StopIteration(Exception): 962 __atomic__ = object 963 964 class TypeError(Exception): 965 __atomic__ = object 966 967 class NotImplementedType: 968 __atomic__ = object 969 970 # General functions. 971 972 def isinstance(obj, cls): 973 return bool() 974 975 def issubclass(cls1, cls2): 976 return bool() 977 978 def len(x): 979 return x.__len__() 980 981 def max(*l): 982 max_so_far = l[0] 983 for i in l[1:]: 984 if i > max_so_far: 985 max_so_far = i 986 return max_so_far 987 988 def range(start_or_end, end=None, step=None): 989 if end is None: 990 start = 0 991 end = start_or_end 992 else: 993 start = start_or_end 994 if start == end: 995 return [] 996 elif start > end: 997 step = step or 1 998 i = start 999 result = [] 1000 while i < end: 1001 result.append(i) 1002 i += 1 1003 return result 1004 else: 1005 step = step or -1 1006 i = start 1007 result = [] 1008 while i > end: 1009 result.append(i) 1010 i -= 1 1011 return result 1012 1013 # Special values. 1014 1015 True = bool() 1016 False = bool() 1017 None = none() 1018 stdin = file() 1019 stdout = file() 1020 stderr = file() 1021 NotImplemented = NotImplementedType() 1022 1023 # Special functions. These all operate on references at run-time. 1024 1025 def __is__(a, b): 1026 return bool() 1027 1028 def __not__(a): 1029 return bool() 1030 1031 # vim: tabstop=4 expandtab shiftwidth=4