1 #!/usr/bin/env python 2 3 """ 4 Simple built-in classes and functions. 5 6 Copyright (C) 2005, 2006 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 class bool: 31 def __bool__(self): 32 return self 33 34 def __str__(self): 35 if self: 36 return "True" 37 else: 38 return "False" 39 40 class int: 41 def __iadd__(self, other): 42 if isinstance(other, int): 43 return int() 44 else: 45 raise TypeError 46 47 def __isub__(self, other): 48 if isinstance(other, int): 49 return int() 50 else: 51 raise TypeError 52 53 def __add__(self, other): 54 if isinstance(other, int): 55 return int() 56 else: 57 raise TypeError 58 59 def __radd__(self, other): 60 if isinstance(other, int): 61 return int() 62 else: 63 raise TypeError 64 65 def __sub__(self, other): 66 if isinstance(other, int): 67 return int() 68 else: 69 raise TypeError 70 71 def __rsub__(self, other): 72 if isinstance(other, int): 73 return int() 74 else: 75 raise TypeError 76 77 def __mul__(self, other): 78 if isinstance(other, int): 79 return int() 80 else: 81 raise TypeError 82 83 def __rmul__(self, other): 84 if isinstance(other, int): 85 return int() 86 else: 87 raise TypeError 88 89 def __div__(self, other): 90 if isinstance(other, int): 91 return int() 92 else: 93 raise TypeError 94 95 def __rdiv__(self, other): 96 if isinstance(other, int): 97 return int() 98 else: 99 raise TypeError 100 101 def __floordiv__(self, other): 102 if isinstance(other, int): 103 return int() 104 else: 105 raise TypeError 106 107 def __rfloordiv__(self, other): 108 if isinstance(other, int): 109 return int() 110 else: 111 raise TypeError 112 113 def __pow__(self, other): 114 if isinstance(other, int): 115 return int() 116 else: 117 raise TypeError 118 119 def __lt__(self, other): 120 if isinstance(other, int): 121 return bool() 122 else: 123 raise TypeError 124 125 def __gt__(self, other): 126 if isinstance(other, int): 127 return bool() 128 else: 129 raise TypeError 130 131 def __le__(self, other): 132 if isinstance(other, int): 133 return bool() 134 else: 135 raise TypeError 136 137 def __ge__(self, other): 138 if isinstance(other, int): 139 return bool() 140 else: 141 raise TypeError 142 143 def __eq__(self, other): 144 if isinstance(other, int): 145 return bool() 146 else: 147 raise TypeError 148 149 def __ne__(self, other): 150 if isinstance(other, int): 151 return bool() 152 else: 153 raise TypeError 154 155 def __neg__(self): 156 return int() 157 158 def __pos__(self): 159 return self 160 161 def __str__(self): 162 return string() 163 164 def __bool__(self): 165 return self != 0 166 167 class long: 168 def __iadd__(self, other): 169 if isinstance(other, int): 170 return long() 171 elif isinstance(other, long): 172 return long() 173 else: 174 raise TypeError 175 176 def __isub__(self, other): 177 if isinstance(other, int): 178 return long() 179 elif isinstance(other, long): 180 return long() 181 else: 182 raise TypeError 183 184 def __add__(self, other): 185 if isinstance(other, int): 186 return long() 187 elif isinstance(other, long): 188 return long() 189 else: 190 raise TypeError 191 192 def __radd__(self, other): 193 if isinstance(other, int): 194 return long() 195 elif isinstance(other, long): 196 return long() 197 else: 198 raise TypeError 199 200 def __sub__(self, other): 201 if isinstance(other, int): 202 return long() 203 elif isinstance(other, long): 204 return long() 205 else: 206 raise TypeError 207 208 def __rsub__(self, other): 209 if isinstance(other, int): 210 return long() 211 elif isinstance(other, long): 212 return long() 213 else: 214 raise TypeError 215 216 def __lt__(self, other): 217 if isinstance(other, int): 218 return bool() 219 elif isinstance(other, long): 220 return bool() 221 else: 222 raise TypeError 223 224 def __gt__(self, other): 225 if isinstance(other, int): 226 return bool() 227 elif isinstance(other, long): 228 return bool() 229 else: 230 raise TypeError 231 232 def __le__(self, other): 233 if isinstance(other, int): 234 return bool() 235 elif isinstance(other, long): 236 return bool() 237 else: 238 raise TypeError 239 240 def __ge__(self, other): 241 if isinstance(other, int): 242 return bool() 243 elif isinstance(other, long): 244 return bool() 245 else: 246 raise TypeError 247 248 def __eq__(self, other): 249 if isinstance(other, int): 250 return bool() 251 elif isinstance(other, long): 252 return bool() 253 else: 254 raise TypeError 255 256 def __ne__(self, other): 257 if isinstance(other, int): 258 return bool() 259 elif isinstance(other, long): 260 return bool() 261 else: 262 raise TypeError 263 264 def __neg__(self): 265 return long() 266 267 def __pos__(self): 268 return self 269 270 def __str__(self): 271 return string() 272 273 def __bool__(self): 274 return self != 0 275 276 class float: 277 def __iadd__(self, other): 278 if isinstance(other, int): 279 return float() 280 elif isinstance(other, long): 281 return float() 282 elif isinstance(other, float): 283 return float() 284 else: 285 raise TypeError 286 287 def __isub__(self, other): 288 if isinstance(other, int): 289 return float() 290 elif isinstance(other, long): 291 return float() 292 elif isinstance(other, float): 293 return float() 294 else: 295 raise TypeError 296 297 def __add__(self, other): 298 if isinstance(other, int): 299 return float() 300 elif isinstance(other, long): 301 return float() 302 elif isinstance(other, float): 303 return float() 304 else: 305 raise TypeError 306 307 def __radd__(self, other): 308 if isinstance(other, int): 309 return float() 310 elif isinstance(other, long): 311 return float() 312 elif isinstance(other, float): 313 return float() 314 else: 315 raise TypeError 316 317 def __sub__(self, other): 318 if isinstance(other, int): 319 return float() 320 elif isinstance(other, long): 321 return float() 322 elif isinstance(other, float): 323 return float() 324 else: 325 raise TypeError 326 327 def __rsub__(self, other): 328 if isinstance(other, int): 329 return float() 330 elif isinstance(other, long): 331 return float() 332 elif isinstance(other, float): 333 return float() 334 else: 335 raise TypeError 336 337 def __mul__(self, other): 338 if isinstance(other, int): 339 return float() 340 elif isinstance(other, long): 341 return float() 342 elif isinstance(other, float): 343 return float() 344 else: 345 raise TypeError 346 347 def __rmul__(self, other): 348 if isinstance(other, int): 349 return float() 350 elif isinstance(other, long): 351 return float() 352 elif isinstance(other, float): 353 return float() 354 else: 355 raise TypeError 356 357 def __div__(self, other): 358 if isinstance(other, int): 359 return float() 360 elif isinstance(other, long): 361 return float() 362 elif isinstance(other, float): 363 return float() 364 else: 365 raise TypeError 366 367 def __rdiv__(self, other): 368 if isinstance(other, int): 369 return float() 370 elif isinstance(other, long): 371 return float() 372 elif isinstance(other, float): 373 return float() 374 else: 375 raise TypeError 376 377 def __floordiv__(self, other): 378 if isinstance(other, int): 379 return float() 380 elif isinstance(other, long): 381 return float() 382 elif isinstance(other, float): 383 return float() 384 else: 385 raise TypeError 386 387 def __rfloordiv__(self, other): 388 if isinstance(other, int): 389 return float() 390 elif isinstance(other, long): 391 return float() 392 elif isinstance(other, float): 393 return float() 394 else: 395 raise TypeError 396 397 def __pow__(self, other): 398 if isinstance(other, int): 399 return float() 400 elif isinstance(other, long): 401 return float() 402 elif isinstance(other, float): 403 return float() 404 else: 405 raise TypeError 406 407 def __rpow__(self, other): 408 if isinstance(other, int): 409 return float() 410 elif isinstance(other, long): 411 return float() 412 elif isinstance(other, float): 413 return float() 414 else: 415 raise TypeError 416 417 def __lt__(self, other): 418 if isinstance(other, int): 419 return bool() 420 elif isinstance(other, long): 421 return bool() 422 elif isinstance(other, float): 423 return bool() 424 else: 425 raise TypeError 426 427 def __gt__(self, other): 428 if isinstance(other, int): 429 return bool() 430 elif isinstance(other, long): 431 return bool() 432 elif isinstance(other, float): 433 return bool() 434 else: 435 raise TypeError 436 437 def __le__(self, other): 438 if isinstance(other, int): 439 return bool() 440 elif isinstance(other, long): 441 return bool() 442 elif isinstance(other, float): 443 return bool() 444 else: 445 raise TypeError 446 447 def __ge__(self, other): 448 if isinstance(other, int): 449 return bool() 450 elif isinstance(other, long): 451 return bool() 452 elif isinstance(other, float): 453 return bool() 454 else: 455 raise TypeError 456 457 def __eq__(self, other): 458 if isinstance(other, int): 459 return bool() 460 elif isinstance(other, long): 461 return bool() 462 elif isinstance(other, float): 463 return bool() 464 else: 465 raise TypeError 466 467 def __ne__(self, other): 468 if isinstance(other, int): 469 return bool() 470 elif isinstance(other, long): 471 return bool() 472 elif isinstance(other, float): 473 return bool() 474 else: 475 raise TypeError 476 477 def __neg__(self): 478 return float() 479 480 def __pos__(self): 481 return self 482 483 def __str__(self): 484 return string() 485 486 def __bool__(self): 487 return self != 0 488 489 class str: 490 def __init__(self, x=None): 491 x.__str__() 492 493 def __add__(self, other): 494 if isinstance(other, string): 495 return string() 496 else: 497 raise TypeError 498 499 def __radd__(self, other): 500 if isinstance(other, string): 501 return string() 502 else: 503 raise TypeError 504 505 def __len__(self): 506 return int() 507 508 def __str__(self): 509 return self 510 511 def __bool__(self): 512 return self.__len__() != 0 513 514 def join(self, l): 515 total = 0 516 first = 1 517 self_len = self.__len__() 518 for i in l: 519 if not first: 520 total += self_len 521 total += len(str(i)) 522 first = 0 523 b = buffer(total) 524 first = 1 525 for i in l: 526 if not first: 527 b.append(self) 528 b.append(str(i)) 529 first = 0 530 s = str(b) 531 return s 532 533 class buffer: 534 def __init__(self, size): 535 if not isinstance(size, int): 536 raise TypeError 537 538 def append(self, s): 539 if not isinstance(s, string): 540 raise TypeError 541 pass 542 543 def __str__(self): 544 return string() 545 546 class list: 547 def __init__(self, *args): 548 self.next = None 549 self.last = self 550 for arg in args: 551 self.append(arg) 552 553 def __getitem__(self, index): 554 i = 0 555 n = self 556 # NOTE: Support negative indices using last. 557 while i < index and n.next is not None: 558 n = n.next 559 i += 1 560 if n.next is not None: 561 return n.value 562 else: 563 raise IndexError() # NOTE: Make this compliant with Python! 564 565 def __setitem__(self, index, value): 566 i = 0 567 n = self 568 # NOTE: Support negative indices using last. 569 while i < index and n.next is not None: 570 n = n.next 571 i += 1 572 if n.next is not None: 573 n.value = value 574 else: 575 raise IndexError() # NOTE: Make this compliant with Python! 576 577 def __getslice__(self, start, end=None): 578 slice = [] 579 i = 0 580 n = self 581 # NOTE: Support negative indices using last. 582 while (end is None or i < end) and n.next is not None: 583 if i >= start: 584 slice.append(n.value) 585 n = n.next 586 i += 1 587 return slice 588 589 def __setslice__(self, start, end, slice): 590 i = 0 591 n = self 592 j = 0 593 p = slice 594 # NOTE: Support negative indices using last. 595 # NOTE: Support appending when start >= len(self). 596 while i < end and n is not None and p is not None: 597 if i >= start: 598 n.value = p.value 599 p = p.next 600 j += 1 601 n = n.next 602 i += 1 603 604 def append(self, value): 605 n = self.last 606 n.value = value 607 n.next = self.__class__() 608 self.last = n.next 609 610 def __len__(self): 611 i = 0 612 n = self 613 while n.next is not None: 614 n = n.next 615 i += 1 616 return i 617 618 def __add__(self, other): 619 result = self.__class__() 620 for value in self: 621 result.append(value) 622 for value in other: 623 result.append(value) 624 return result 625 626 def __iadd__(self, other): 627 for value in other: 628 self.append(value) 629 return self 630 631 def __str__(self): 632 output = ["["] 633 n = self 634 first = 1 635 while n.next is not None: 636 if not first: 637 output.append(", ") 638 else: 639 first = 0 640 output.append(str(n.value)) 641 n = n.next 642 output.append("]") 643 return "".join(output) 644 645 def __iter__(self): 646 return listiterator(self) 647 648 def __bool__(self): 649 return self.__len__() != 0 650 651 class listiterator: 652 def __init__(self, l): 653 self.l = l 654 655 def next(self): 656 l = self.l 657 next = l.next 658 if next is not None: 659 self.l = next 660 return l.value 661 else: 662 raise StopIteration 663 664 def __bool__(self): 665 return bool() 666 667 class tuple: 668 def __init__(self, *args): 669 self.next = None 670 self.last = self 671 for arg in args: 672 self.append(arg) 673 674 def __getitem__(self, index): 675 i = 0 676 n = self 677 # NOTE: Support negative indices using last. 678 while i < index and n.next is not None: 679 n = n.next 680 i += 1 681 if n.next is not None: 682 return n.value 683 else: 684 raise IndexError() # NOTE: Make this compliant with Python! 685 686 def __getslice__(self, start, end=None): 687 # NOTE: Should probably return a tuple. 688 slice = [] 689 i = 0 690 n = self 691 # NOTE: Support negative indices using last. 692 while (end is None or i < end) and n.next is not None: 693 if i >= start: 694 slice.append(n.value) 695 n = n.next 696 i += 1 697 return slice 698 699 # NOTE: The append method should be internal at most. 700 701 def append(self, value): 702 n = self.last 703 n.value = value 704 n.next = self.__class__() 705 self.last = n.next 706 707 def __len__(self): 708 i = 0 709 n = self 710 while n.next is not None: 711 n = n.next 712 i += 1 713 return i 714 715 def __add__(self, other): 716 result = self.__class__() 717 for value in self: 718 result.append(value) 719 for value in other: 720 result.append(value) 721 return result 722 723 def __str__(self): 724 output = ["("] 725 n = self 726 first = 1 727 while n.next is not None: 728 if not first: 729 output.append(", ") 730 else: 731 first = 0 732 output.append(str(n.value)) 733 n = n.next 734 output.append(")") 735 return "".join(output) 736 737 def __iter__(self): 738 return tupleiterator(self) 739 740 def __bool__(self): 741 return self.__len__() != 0 742 743 class tupleiterator: 744 def __init__(self, l): 745 self.l = l 746 747 def next(self): 748 l = self.l 749 next = l.next 750 if next is not None: 751 self.l = next 752 return l.value 753 else: 754 raise StopIteration 755 756 def __bool__(self): 757 return bool() 758 759 class dict: 760 pass 761 762 class Exception: 763 pass 764 765 class StopIteration(Exception): 766 pass 767 768 class IndexError(Exception): 769 pass 770 771 class AttributeError(Exception): 772 pass 773 774 class TypeError(Exception): 775 pass 776 777 class none: 778 def __bool__(self): 779 return False 780 781 def __str__(self): 782 return "None" 783 784 class undefined: 785 pass 786 787 class xrange: 788 def __init__(self, start, end, step=1): 789 self.start = start 790 self.end = end 791 self.step = step 792 self.current = start 793 794 def __iter__(self): 795 return self 796 797 def next(self): 798 if self.current >= self.end: 799 raise StopIteration 800 current = self.current 801 self.current += self.step 802 return current 803 804 # General functions. 805 806 def isinstance(obj, cls): 807 return bool() 808 809 def issubclass(cls1, cls2): 810 return bool() 811 812 def len(x): 813 return x.__len__() 814 815 def max(*l): 816 max_so_far = l[0] 817 for i in l[1:]: 818 if i > max_so_far: 819 max_so_far = i 820 return max_so_far 821 822 def range(start, end, step=None): 823 if start == end: 824 return [] 825 elif start > end: 826 step = step or 1 827 i = start 828 result = [] 829 while i < end: 830 result.append(i) 831 i += 1 832 return result 833 else: 834 step = step or -1 835 i = start 836 result = [] 837 while i > end: 838 result.append(i) 839 i -= 1 840 return result 841 842 # Special values. 843 844 True = bool() 845 False = bool() 846 None = none() 847 Undefined = undefined() 848 849 # Special functions. These all operate on references at run-time. 850 851 def __is__(a, b): 852 return bool() 853 854 def __is_not__(a, b): 855 return bool() 856 857 def __not__(a): 858 return bool() 859 860 # vim: tabstop=4 expandtab shiftwidth=4