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 __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 str: 615 __atomic__ = 1 616 617 def __init__(self, x=None): 618 x.__str__() 619 620 def __add__(self, other): 621 if isinstance(other, str): 622 return str() 623 else: 624 raise TypeError 625 626 def __radd__(self, other): 627 if isinstance(other, str): 628 return str() 629 else: 630 raise TypeError 631 632 def __mod__(self, other): 633 634 "The format operator for strings." 635 636 return str() 637 638 def __len__(self): 639 return int() 640 641 def __str__(self): 642 return self 643 644 def __bool__(self): 645 return self.__len__() != 0 646 647 def join(self, l): 648 total = 0 649 first = 1 650 self_len = self.__len__() 651 for i in l: 652 if not first: 653 total += self_len 654 total += len(str(i)) 655 first = 0 656 b = buffer(total) 657 first = 1 658 for i in l: 659 if not first: 660 b.append(self) 661 b.append(str(i)) 662 first = 0 663 s = str(b) 664 return s 665 666 class tuple: 667 def __init__(self, *args): 668 for arg in args: 669 self.append(arg) 670 671 def __getitem__(self, index): 672 if -len(self) <= index < len(self): 673 return self.value 674 else: 675 raise IndexError, index 676 677 def __setitem__(self, index, value): 678 if -len(self) <= index < len(self): 679 self.value = value 680 else: 681 raise IndexError, index 682 683 def __getslice__(self, start, end=None): 684 if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end: 685 return list(self.value) 686 else: 687 raise IndexError, index 688 689 def __setslice__(self, start, end, slice): 690 if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end: 691 for value in slice: 692 self.value = value 693 else: 694 raise IndexError, index 695 696 # NOTE: The append method should be internal at most. 697 698 def append(self, value): 699 self.value = value 700 701 def __len__(self): 702 return int() 703 704 def __add__(self, other): 705 for value in other: 706 self.value = value 707 return self 708 709 def __str__(self): 710 return str() 711 712 def __iter__(self): 713 return tupleiterator(self) 714 715 def __bool__(self): 716 return self.__len__() != 0 717 718 class tupleiterator: 719 def __init__(self, l): 720 self.l = l 721 self.index = 0 722 723 def next(self): 724 if self.index < len(self.l): 725 self.index += 1 726 return self.l[self.index] 727 else: 728 raise StopIteration 729 730 def __bool__(self): 731 return bool() 732 733 class xrange: 734 def __init__(self, start_or_end, end=None, step=1): 735 if end is None: 736 self.start = 0 737 self.end = start_or_end 738 else: 739 self.start = start_or_end 740 self.end = end 741 self.step = step 742 self.current = self.start 743 744 def __iter__(self): 745 return self 746 747 def next(self): 748 if self.current >= self.end: 749 raise StopIteration 750 current = self.current 751 self.current += self.step 752 return current 753 754 class Exception: 755 def __init__(self, *args): 756 pass 757 758 class AttributeError(Exception): 759 __atomic__ = 1 760 761 class IndexError(Exception): 762 __atomic__ = 1 763 764 class StopIteration(Exception): 765 __atomic__ = 1 766 767 class TypeError(Exception): 768 __atomic__ = 1 769 770 class NotImplementedType: 771 __atomic__ = 1 772 773 # General functions. 774 775 def isinstance(obj, cls): 776 return bool() 777 778 def issubclass(cls1, cls2): 779 return bool() 780 781 def len(x): 782 return x.__len__() 783 784 def max(*l): 785 max_so_far = l[0] 786 for i in l[1:]: 787 if i > max_so_far: 788 max_so_far = i 789 return max_so_far 790 791 def range(start, end, step=None): 792 if start == end: 793 return [] 794 elif start > end: 795 step = step or 1 796 i = start 797 result = [] 798 while i < end: 799 result.append(i) 800 i += 1 801 return result 802 else: 803 step = step or -1 804 i = start 805 result = [] 806 while i > end: 807 result.append(i) 808 i -= 1 809 return result 810 811 # Special values. 812 813 True = bool() 814 False = bool() 815 None = none() 816 stdin = file() 817 stdout = file() 818 stderr = file() 819 NotImplemented = NotImplementedType() 820 821 # Special functions. These all operate on references at run-time. 822 823 def __is__(a, b): 824 return bool() 825 826 def __not__(a): 827 return bool() 828 829 # vim: tabstop=4 expandtab shiftwidth=4