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