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