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 NoneType = none 762 763 class slice: 764 def __init__(self, start_or_end, end=None, step=None): 765 if end is None: 766 self.start = 0 767 self.end = start_or_end 768 else: 769 self.start = start_or_end 770 self.end = end 771 self.step = step 772 773 class str: 774 __atomic__ = object 775 776 def __init__(self, x=None): 777 x.__str__() 778 779 def __iadd__(self, other): 780 if isinstance(other, str): 781 return str() 782 else: 783 raise TypeError 784 785 def __add__(self, other): 786 if isinstance(other, str): 787 return str() 788 else: 789 raise TypeError 790 791 def __radd__(self, other): 792 if isinstance(other, str): 793 return str() 794 else: 795 raise TypeError 796 797 def __mul__(self, other): 798 if isinstance(other, int) or isinstance(other, long): 799 return str() 800 else: 801 raise TypeError 802 803 def __radd__(self, other): 804 if isinstance(other, int) or isinstance(other, long): 805 return str() 806 else: 807 raise TypeError 808 809 def __mod__(self, other): 810 811 "The format operator for strings." 812 813 return str() 814 815 def __lt__(self, other): 816 if isinstance(other, str): 817 return bool() 818 else: 819 return NotImplemented 820 821 def __gt__(self, other): 822 if isinstance(other, str): 823 return bool() 824 else: 825 return NotImplemented 826 827 def __le__(self, other): 828 if isinstance(other, str): 829 return bool() 830 else: 831 return NotImplemented 832 833 def __ge__(self, other): 834 if isinstance(other, str): 835 return bool() 836 else: 837 return NotImplemented 838 839 def __eq__(self, other): 840 if isinstance(other, str): 841 return bool() 842 else: 843 return NotImplemented 844 845 def __ne__(self, other): 846 if isinstance(other, str): 847 return bool() 848 else: 849 return NotImplemented 850 851 def __len__(self): 852 return int() 853 854 def __str__(self): 855 return self 856 857 def __bool__(self): 858 return self.__len__() != 0 859 860 def join(self, l): 861 total = 0 862 first = 1 863 self_len = self.__len__() 864 for i in l: 865 if not first: 866 total += self_len 867 total += len(str(i)) 868 first = 0 869 b = buffer(total) 870 first = 1 871 for i in l: 872 if not first: 873 b.append(self) 874 b.append(str(i)) 875 first = 0 876 s = str(b) 877 return s 878 879 class tuple: 880 def __init__(self, args): 881 for arg in args: 882 self.value = arg 883 884 def __getitem__(self, index): 885 if -len(self) <= index < len(self): 886 return self.value 887 else: 888 raise IndexError, index 889 890 def __getslice__(self, start, end=None): 891 if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end: 892 return (self.value,) 893 else: 894 return () 895 896 def __len__(self): 897 return int() 898 899 def __add__(self, other): 900 result = list(self) 901 for value in other: 902 result.append(value) 903 return tuple(result) 904 905 def __str__(self): 906 return str() 907 908 def __iter__(self): 909 return tupleiterator(self) 910 911 def __bool__(self): 912 return self.__len__() != 0 913 914 class tupleiterator: 915 def __init__(self, l): 916 self.l = l 917 self.index = 0 918 919 def next(self): 920 if self.index < len(self.l): 921 self.index += 1 922 return self.l[self.index] 923 else: 924 raise StopIteration 925 926 def __bool__(self): 927 return bool() 928 929 class xrange: 930 def __init__(self, start_or_end, end=None, step=1): 931 if end is None: 932 self.start = 0 933 self.end = start_or_end 934 else: 935 self.start = start_or_end 936 self.end = end 937 self.step = step 938 self.current = self.start 939 940 def __iter__(self): 941 return self 942 943 def next(self): 944 if self.current >= self.end: 945 raise StopIteration 946 current = self.current 947 self.current += self.step 948 return current 949 950 class Exception: 951 def __init__(self, *args): 952 pass 953 954 class AssertionError(Exception): 955 __atomic__ = object 956 957 class AttributeError(Exception): 958 __atomic__ = object 959 960 class IndexError(Exception): 961 __atomic__ = object 962 963 class StopIteration(Exception): 964 __atomic__ = object 965 966 class TypeError(Exception): 967 __atomic__ = object 968 969 class NotImplementedType: 970 __atomic__ = object 971 972 # General functions. 973 974 def isinstance(obj, cls): 975 return bool() 976 977 def issubclass(cls1, cls2): 978 return bool() 979 980 def len(x): 981 return x.__len__() 982 983 def max(*l): 984 max_so_far = l[0] 985 for i in l[1:]: 986 if i > max_so_far: 987 max_so_far = i 988 return max_so_far 989 990 def range(start_or_end, end=None, step=None): 991 if end is None: 992 start = 0 993 end = start_or_end 994 else: 995 start = start_or_end 996 if start == end: 997 return [] 998 elif start > end: 999 step = step or 1 1000 i = start 1001 result = [] 1002 while i < end: 1003 result.append(i) 1004 i += 1 1005 return result 1006 else: 1007 step = step or -1 1008 i = start 1009 result = [] 1010 while i > end: 1011 result.append(i) 1012 i -= 1 1013 return result 1014 1015 # Special values. 1016 1017 True = bool() 1018 False = bool() 1019 None = none() 1020 stdin = file() 1021 stdout = file() 1022 stderr = file() 1023 NotImplemented = NotImplementedType() 1024 1025 # Special functions. These all operate on references at run-time. 1026 1027 def __is__(a, b): 1028 return bool() 1029 1030 def __not__(a): 1031 return bool() 1032 1033 # vim: tabstop=4 expandtab shiftwidth=4