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