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 __lt__(self, other): 377 if isinstance(other, int): 378 return bool() 379 else: 380 return NotImplemented 381 382 def __gt__(self, other): 383 if isinstance(other, int): 384 return bool() 385 else: 386 return NotImplemented 387 388 def __le__(self, other): 389 if isinstance(other, int): 390 return bool() 391 else: 392 return NotImplemented 393 394 def __ge__(self, other): 395 if isinstance(other, int): 396 return bool() 397 else: 398 return NotImplemented 399 400 def __eq__(self, other): 401 if isinstance(other, int): 402 return bool() 403 else: 404 return NotImplemented 405 406 def __ne__(self, other): 407 if isinstance(other, int): 408 return bool() 409 else: 410 return NotImplemented 411 412 def __neg__(self): 413 return int() 414 415 def __pos__(self): 416 return self 417 418 def __str__(self): 419 return str() 420 421 def __bool__(self): 422 return self != 0 423 424 class list: 425 def __init__(self, *args): 426 for arg in args: 427 self.append(arg) 428 429 def __getitem__(self, index): 430 if -len(self) <= index < len(self): 431 return self.value 432 else: 433 raise IndexError, index 434 435 def __setitem__(self, index, value): 436 if -len(self) <= index < len(self): 437 self.value = value 438 else: 439 raise IndexError, index 440 441 def __getslice__(self, start, end=None): 442 if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end: 443 return list(self.value) 444 else: 445 raise IndexError, index 446 447 def __setslice__(self, start, end, slice): 448 if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end: 449 for value in slice: 450 self.value = value 451 else: 452 raise IndexError, index 453 454 def append(self, value): 455 self.value = value 456 457 def __len__(self): 458 return int() 459 460 def __add__(self, other): 461 for value in other: 462 self.value = value 463 return self 464 465 __iadd__ = __add__ 466 467 def __str__(self): 468 return str() 469 470 def __iter__(self): 471 return listiterator(self) 472 473 def __bool__(self): 474 return self.__len__() != 0 475 476 class listiterator: 477 def __init__(self, l): 478 self.l = l 479 self.index = 0 480 481 def next(self): 482 if self.index < len(self.l): 483 self.index += 1 484 return self.l[self.index] 485 else: 486 raise StopIteration 487 488 def __bool__(self): 489 return bool() 490 491 class long: 492 __atomic__ = 1 493 494 def __init__(self, number_or_string=None): 495 pass 496 497 def __iadd__(self, other): 498 if isinstance(other, int): 499 return long() 500 elif isinstance(other, long): 501 return long() 502 else: 503 raise TypeError 504 505 def __isub__(self, other): 506 if isinstance(other, int): 507 return long() 508 elif isinstance(other, long): 509 return long() 510 else: 511 raise TypeError 512 513 def __add__(self, other): 514 if isinstance(other, int): 515 return long() 516 elif isinstance(other, long): 517 return long() 518 else: 519 raise TypeError 520 521 def __radd__(self, other): 522 if isinstance(other, int): 523 return long() 524 elif isinstance(other, long): 525 return long() 526 else: 527 raise TypeError 528 529 def __sub__(self, other): 530 if isinstance(other, int): 531 return long() 532 elif isinstance(other, long): 533 return long() 534 else: 535 raise TypeError 536 537 def __rsub__(self, other): 538 if isinstance(other, int): 539 return long() 540 elif isinstance(other, long): 541 return long() 542 else: 543 raise TypeError 544 545 def __lt__(self, other): 546 if isinstance(other, int): 547 return bool() 548 elif isinstance(other, long): 549 return bool() 550 else: 551 return NotImplemented 552 553 def __gt__(self, other): 554 if isinstance(other, int): 555 return bool() 556 elif isinstance(other, long): 557 return bool() 558 else: 559 return NotImplemented 560 561 def __le__(self, other): 562 if isinstance(other, int): 563 return bool() 564 elif isinstance(other, long): 565 return bool() 566 else: 567 return NotImplemented 568 569 def __ge__(self, other): 570 if isinstance(other, int): 571 return bool() 572 elif isinstance(other, long): 573 return bool() 574 else: 575 return NotImplemented 576 577 def __eq__(self, other): 578 if isinstance(other, int): 579 return bool() 580 elif isinstance(other, long): 581 return bool() 582 else: 583 return NotImplemented 584 585 def __ne__(self, other): 586 if isinstance(other, int): 587 return bool() 588 elif isinstance(other, long): 589 return bool() 590 else: 591 return NotImplemented 592 593 def __neg__(self): 594 return long() 595 596 def __pos__(self): 597 return self 598 599 def __str__(self): 600 return str() 601 602 def __bool__(self): 603 return self != 0 604 605 class none: 606 __atomic__ = 1 607 608 def __bool__(self): 609 return False 610 611 def __str__(self): 612 return "None" 613 614 class slice: 615 def __init__(self, start_or_end, end=None, step=None): 616 if end is None: 617 self.start = 0 618 self.end = start_or_end 619 else: 620 self.start = start_or_end 621 self.end = end 622 self.step = step 623 624 class str: 625 __atomic__ = 1 626 627 def __init__(self, x=None): 628 x.__str__() 629 630 def __add__(self, other): 631 if isinstance(other, str): 632 return str() 633 else: 634 raise TypeError 635 636 def __radd__(self, other): 637 if isinstance(other, str): 638 return str() 639 else: 640 raise TypeError 641 642 def __mod__(self, other): 643 644 "The format operator for strings." 645 646 return str() 647 648 def __len__(self): 649 return int() 650 651 def __str__(self): 652 return self 653 654 def __bool__(self): 655 return self.__len__() != 0 656 657 def join(self, l): 658 total = 0 659 first = 1 660 self_len = self.__len__() 661 for i in l: 662 if not first: 663 total += self_len 664 total += len(str(i)) 665 first = 0 666 b = buffer(total) 667 first = 1 668 for i in l: 669 if not first: 670 b.append(self) 671 b.append(str(i)) 672 first = 0 673 s = str(b) 674 return s 675 676 class tuple: 677 def __init__(self, *args): 678 for arg in args: 679 self.append(arg) 680 681 def __getitem__(self, index): 682 if -len(self) <= index < len(self): 683 return self.value 684 else: 685 raise IndexError, index 686 687 def __setitem__(self, index, value): 688 if -len(self) <= index < len(self): 689 self.value = value 690 else: 691 raise IndexError, index 692 693 def __getslice__(self, start, end=None): 694 if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end: 695 return list(self.value) 696 else: 697 raise IndexError, index 698 699 def __setslice__(self, start, end, slice): 700 if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end: 701 for value in slice: 702 self.value = value 703 else: 704 raise IndexError, index 705 706 # NOTE: The append method should be internal at most. 707 708 def append(self, value): 709 self.value = value 710 711 def __len__(self): 712 return int() 713 714 def __add__(self, other): 715 for value in other: 716 self.value = value 717 return self 718 719 def __str__(self): 720 return str() 721 722 def __iter__(self): 723 return tupleiterator(self) 724 725 def __bool__(self): 726 return self.__len__() != 0 727 728 class tupleiterator: 729 def __init__(self, l): 730 self.l = l 731 self.index = 0 732 733 def next(self): 734 if self.index < len(self.l): 735 self.index += 1 736 return self.l[self.index] 737 else: 738 raise StopIteration 739 740 def __bool__(self): 741 return bool() 742 743 class xrange: 744 def __init__(self, start_or_end, end=None, step=1): 745 if end is None: 746 self.start = 0 747 self.end = start_or_end 748 else: 749 self.start = start_or_end 750 self.end = end 751 self.step = step 752 self.current = self.start 753 754 def __iter__(self): 755 return self 756 757 def next(self): 758 if self.current >= self.end: 759 raise StopIteration 760 current = self.current 761 self.current += self.step 762 return current 763 764 class Exception: 765 def __init__(self, *args): 766 pass 767 768 class AttributeError(Exception): 769 __atomic__ = 1 770 771 class IndexError(Exception): 772 __atomic__ = 1 773 774 class StopIteration(Exception): 775 __atomic__ = 1 776 777 class TypeError(Exception): 778 __atomic__ = 1 779 780 class NotImplementedType: 781 __atomic__ = 1 782 783 # General functions. 784 785 def isinstance(obj, cls): 786 return bool() 787 788 def issubclass(cls1, cls2): 789 return bool() 790 791 def len(x): 792 return x.__len__() 793 794 def max(*l): 795 max_so_far = l[0] 796 for i in l[1:]: 797 if i > max_so_far: 798 max_so_far = i 799 return max_so_far 800 801 def range(start, end, step=None): 802 if start == end: 803 return [] 804 elif start > end: 805 step = step or 1 806 i = start 807 result = [] 808 while i < end: 809 result.append(i) 810 i += 1 811 return result 812 else: 813 step = step or -1 814 i = start 815 result = [] 816 while i > end: 817 result.append(i) 818 i -= 1 819 return result 820 821 # Special values. 822 823 True = bool() 824 False = bool() 825 None = none() 826 stdin = file() 827 stdout = file() 828 stderr = file() 829 NotImplemented = NotImplementedType() 830 831 # Special functions. These all operate on references at run-time. 832 833 def __is__(a, b): 834 return bool() 835 836 def __not__(a): 837 return bool() 838 839 # vim: tabstop=4 expandtab shiftwidth=4