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=()): 478 for arg in args: 479 self.append(arg) 480 481 def __getitem__(self, index): 482 if -len(self) <= index < len(self): 483 return self.value 484 else: 485 raise IndexError, index 486 487 def __setitem__(self, index, value): 488 if -len(self) <= index < len(self): 489 self.value = value 490 else: 491 raise IndexError, index 492 493 def __getslice__(self, start, end=None): 494 if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end: 495 return [self.value] 496 else: 497 return [] 498 499 def __setslice__(self, start, end, slice): 500 #if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end: 501 for value in slice: 502 self.value = value 503 504 def append(self, value): 505 self.value = value 506 507 def __len__(self): 508 return int() 509 510 def __add__(self, other): 511 for value in other: 512 self.value = value 513 return self 514 515 __iadd__ = __add__ 516 517 def __str__(self): 518 return str() 519 520 def __iter__(self): 521 return listiterator(self) 522 523 def __bool__(self): 524 return self.__len__() != 0 525 526 class listiterator: 527 def __init__(self, l): 528 self.l = l 529 self.index = 0 530 531 def next(self): 532 if self.index < len(self.l): 533 self.index += 1 534 return self.l[self.index] 535 else: 536 raise StopIteration 537 538 def __bool__(self): 539 return bool() 540 541 class long: 542 __atomic__ = object 543 544 def __init__(self, number_or_string=None): 545 pass 546 547 def __iadd__(self, other): 548 if isinstance(other, int): 549 return long() 550 elif isinstance(other, long): 551 return long() 552 else: 553 raise TypeError 554 555 def __isub__(self, other): 556 if isinstance(other, int): 557 return long() 558 elif isinstance(other, long): 559 return long() 560 else: 561 raise TypeError 562 563 def __add__(self, other): 564 if isinstance(other, int): 565 return long() 566 elif isinstance(other, long): 567 return long() 568 else: 569 raise TypeError 570 571 def __radd__(self, other): 572 if isinstance(other, int): 573 return long() 574 elif isinstance(other, long): 575 return long() 576 else: 577 raise TypeError 578 579 def __sub__(self, other): 580 if isinstance(other, int): 581 return long() 582 elif isinstance(other, long): 583 return long() 584 else: 585 raise TypeError 586 587 def __rsub__(self, other): 588 if isinstance(other, int): 589 return long() 590 elif isinstance(other, long): 591 return long() 592 else: 593 raise TypeError 594 595 def __mul__(self, other): 596 if isinstance(other, int): 597 return long() 598 elif isinstance(other, long): 599 return long() 600 else: 601 raise TypeError 602 603 def __rmul__(self, other): 604 if isinstance(other, int): 605 return long() 606 elif isinstance(other, long): 607 return long() 608 else: 609 raise TypeError 610 611 def __div__(self, other): 612 if isinstance(other, int): 613 return long() 614 elif isinstance(other, long): 615 return long() 616 else: 617 raise TypeError 618 619 def __rdiv__(self, other): 620 if isinstance(other, int): 621 return long() 622 elif isinstance(other, long): 623 return long() 624 else: 625 raise TypeError 626 627 def __floordiv__(self, other): 628 if isinstance(other, int): 629 return long() 630 elif isinstance(other, long): 631 return long() 632 else: 633 raise TypeError 634 635 def __rfloordiv__(self, other): 636 if isinstance(other, int): 637 return long() 638 elif isinstance(other, long): 639 return long() 640 else: 641 raise TypeError 642 643 def __and__(self, other): 644 if isinstance(other, int): 645 return long() 646 elif isinstance(other, long): 647 return long() 648 else: 649 raise TypeError 650 651 def __rand__(self, other): 652 if isinstance(other, int): 653 return long() 654 elif isinstance(other, long): 655 return long() 656 else: 657 raise TypeError 658 659 def __or__(self, other): 660 if isinstance(other, int): 661 return long() 662 elif isinstance(other, long): 663 return long() 664 else: 665 raise TypeError 666 667 def __ror__(self, other): 668 if isinstance(other, int): 669 return long() 670 elif isinstance(other, long): 671 return long() 672 else: 673 raise TypeError 674 675 def __xor__(self, other): 676 if isinstance(other, int): 677 return long() 678 elif isinstance(other, long): 679 return long() 680 else: 681 raise TypeError 682 683 def __rxor__(self, other): 684 if isinstance(other, int): 685 return long() 686 elif isinstance(other, long): 687 return long() 688 else: 689 raise TypeError 690 691 def __lt__(self, other): 692 if isinstance(other, int): 693 return bool() 694 elif isinstance(other, long): 695 return bool() 696 else: 697 return NotImplemented 698 699 def __gt__(self, other): 700 if isinstance(other, int): 701 return bool() 702 elif isinstance(other, long): 703 return bool() 704 else: 705 return NotImplemented 706 707 def __le__(self, other): 708 if isinstance(other, int): 709 return bool() 710 elif isinstance(other, long): 711 return bool() 712 else: 713 return NotImplemented 714 715 def __ge__(self, other): 716 if isinstance(other, int): 717 return bool() 718 elif isinstance(other, long): 719 return bool() 720 else: 721 return NotImplemented 722 723 def __eq__(self, other): 724 if isinstance(other, int): 725 return bool() 726 elif isinstance(other, long): 727 return bool() 728 else: 729 return NotImplemented 730 731 def __ne__(self, other): 732 if isinstance(other, int): 733 return bool() 734 elif isinstance(other, long): 735 return bool() 736 else: 737 return NotImplemented 738 739 def __neg__(self): 740 return long() 741 742 def __pos__(self): 743 return self 744 745 def __str__(self): 746 return str() 747 748 def __bool__(self): 749 return self != 0 750 751 class none: 752 __atomic__ = object 753 754 def __bool__(self): 755 return False 756 757 def __str__(self): 758 return "None" 759 760 NoneType = none 761 762 class slice: 763 def __init__(self, start_or_end, end=None, step=None): 764 if end is None: 765 self.start = 0 766 self.end = start_or_end 767 else: 768 self.start = start_or_end 769 self.end = end 770 self.step = step 771 772 class str: 773 __atomic__ = object 774 775 def __init__(self, x=None): 776 x.__str__() 777 778 def __getitem__(self, index): 779 if -len(self) <= index < len(self): 780 return str() 781 else: 782 raise IndexError, index 783 784 def __getslice__(self, start, end=None): 785 return str() 786 787 def __iadd__(self, other): 788 if isinstance(other, str): 789 return str() 790 else: 791 raise TypeError 792 793 def __add__(self, other): 794 if isinstance(other, str): 795 return str() 796 else: 797 raise TypeError 798 799 def __radd__(self, other): 800 if isinstance(other, str): 801 return str() 802 else: 803 raise TypeError 804 805 def __mul__(self, other): 806 if isinstance(other, int) or isinstance(other, long): 807 return str() 808 else: 809 raise TypeError 810 811 def __radd__(self, other): 812 if isinstance(other, int) or isinstance(other, long): 813 return str() 814 else: 815 raise TypeError 816 817 def __mod__(self, other): 818 819 "The format operator for strings." 820 821 return str() 822 823 def __lt__(self, other): 824 if isinstance(other, str): 825 return bool() 826 else: 827 return NotImplemented 828 829 def __gt__(self, other): 830 if isinstance(other, str): 831 return bool() 832 else: 833 return NotImplemented 834 835 def __le__(self, other): 836 if isinstance(other, str): 837 return bool() 838 else: 839 return NotImplemented 840 841 def __ge__(self, other): 842 if isinstance(other, str): 843 return bool() 844 else: 845 return NotImplemented 846 847 def __eq__(self, other): 848 if isinstance(other, str): 849 return bool() 850 else: 851 return NotImplemented 852 853 def __ne__(self, other): 854 if isinstance(other, str): 855 return bool() 856 else: 857 return NotImplemented 858 859 def __len__(self): 860 return int() 861 862 def __str__(self): 863 return self 864 865 def __bool__(self): 866 return self.__len__() != 0 867 868 def join(self, l): 869 total = 0 870 first = 1 871 self_len = self.__len__() 872 for i in l: 873 if not first: 874 total += self_len 875 total += len(str(i)) 876 first = 0 877 b = buffer(total) 878 first = 1 879 for i in l: 880 if not first: 881 b.append(self) 882 b.append(str(i)) 883 first = 0 884 s = str(b) 885 return s 886 887 class tuple: 888 def __init__(self, args): 889 for arg in args: 890 self.value = arg 891 892 def __getitem__(self, index): 893 if -len(self) <= index < len(self): 894 return self.value 895 else: 896 raise IndexError, index 897 898 def __getslice__(self, start, end=None): 899 if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end: 900 return (self.value,) 901 else: 902 return () 903 904 def __len__(self): 905 return int() 906 907 def __add__(self, other): 908 result = list(self) 909 for value in other: 910 result.append(value) 911 return tuple(result) 912 913 def __str__(self): 914 return str() 915 916 def __iter__(self): 917 return tupleiterator(self) 918 919 def __bool__(self): 920 return self.__len__() != 0 921 922 class tupleiterator: 923 def __init__(self, l): 924 self.l = l 925 self.index = 0 926 927 def next(self): 928 if self.index < len(self.l): 929 self.index += 1 930 return self.l[self.index] 931 else: 932 raise StopIteration 933 934 def __bool__(self): 935 return bool() 936 937 class xrange: 938 def __init__(self, start_or_end, end=None, step=1): 939 if end is None: 940 self.start = 0 941 self.end = start_or_end 942 else: 943 self.start = start_or_end 944 self.end = end 945 self.step = step 946 self.current = self.start 947 948 def __iter__(self): 949 return self 950 951 def next(self): 952 if self.current >= self.end: 953 raise StopIteration 954 current = self.current 955 self.current += self.step 956 return current 957 958 class Exception: 959 def __init__(self, *args): 960 pass 961 962 class AssertionError(Exception): 963 __atomic__ = object 964 965 class AttributeError(Exception): 966 __atomic__ = object 967 968 class IndexError(Exception): 969 __atomic__ = object 970 971 class StopIteration(Exception): 972 __atomic__ = object 973 974 class TypeError(Exception): 975 __atomic__ = object 976 977 class NotImplementedType: 978 __atomic__ = object 979 980 # General functions. 981 982 def isinstance(obj, cls): 983 return bool() 984 985 def issubclass(cls1, cls2): 986 return bool() 987 988 def len(x): 989 return x.__len__() 990 991 def max(*l): 992 max_so_far = l[0] 993 for i in l[1:]: 994 if i > max_so_far: 995 max_so_far = i 996 return max_so_far 997 998 def range(start_or_end, end=None, step=None): 999 if end is None: 1000 start = 0 1001 end = start_or_end 1002 else: 1003 start = start_or_end 1004 if start == end: 1005 return [] 1006 elif start > end: 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 else: 1015 step = step or -1 1016 i = start 1017 result = [] 1018 while i > end: 1019 result.append(i) 1020 i -= 1 1021 return result 1022 1023 # Special values. 1024 1025 True = bool() 1026 False = bool() 1027 None = none() 1028 stdin = file() 1029 stdout = file() 1030 stderr = file() 1031 NotImplemented = NotImplementedType() 1032 1033 # Special functions. These all operate on references at run-time. 1034 1035 def __is__(a, b): 1036 return bool() 1037 1038 def __not__(a): 1039 return bool() 1040 1041 # vim: tabstop=4 expandtab shiftwidth=4