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