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