1 #!/usr/bin/env python 2 3 """ 4 Simple built-in classes and functions. Objects which provide code that shall 5 always be compiled should provide docstrings. 6 7 Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011 Paul Boddie <paul@boddie.org.uk> 8 9 This program is free software; you can redistribute it and/or modify it under 10 the terms of the GNU General Public License as published by the Free Software 11 Foundation; either version 3 of the License, or (at your option) any later 12 version. 13 14 This program is distributed in the hope that it will be useful, but WITHOUT 15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 16 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 17 details. 18 19 You should have received a copy of the GNU General Public License along with 20 this program. If not, see <http://www.gnu.org/licenses/>. 21 """ 22 23 class object: 24 def __init__(self): pass 25 def __bool__(self): pass 26 27 class basestring(object): 28 def __init__(self, x=None): pass 29 def __contains__(self, value): pass 30 31 def __getitem__(self, index): 32 # Note usage. 33 IndexError 34 35 def __getslice__(self, start, end=None): pass 36 def __iadd__(self, other): pass 37 def __add__(self, other): pass 38 def __radd__(self, other): pass 39 def __mul__(self, other): pass 40 def __rmul__(self, other): pass 41 def __mod__(self, other): pass 42 def __rmod__(self, other): pass 43 def __lt__(self, other): pass 44 def __gt__(self, other): pass 45 def __le__(self, other): pass 46 def __ge__(self, other): pass 47 def __eq__(self, other): pass 48 def __ne__(self, other): pass 49 def __len__(self): pass 50 def __str__(self): pass 51 def __bool__(self): pass 52 def join(self, l): pass 53 def split(self, s): pass 54 def startswith(self, s): pass 55 def endswith(self, s): pass 56 57 class bool(object): 58 def __bool__(self): pass 59 def __str__(self): pass 60 61 class buffer(object): 62 def __init__(self, size): pass 63 def append(self, s): pass 64 def __str__(self): pass 65 66 class complex(object): 67 def __init__(self, real, imag=None): pass 68 69 class dict(object): 70 def __init__(self, *args): pass 71 def __setitem__(self, key, value): pass 72 73 def __getitem__(self, key): 74 # Note usage. 75 KeyError 76 77 class file(object): 78 def read(self, n=None): pass 79 def write(self, s): pass 80 def close(self): pass 81 82 class float(object): 83 def __init__(self, number_or_string=None): pass 84 def __iadd__(self, other): pass 85 def __isub__(self, other): pass 86 def __add__(self, other): pass 87 def __radd__(self, other): pass 88 def __sub__(self, other): pass 89 def __rsub__(self, other): pass 90 def __mul__(self, other): pass 91 def __rmul__(self, other): pass 92 def __div__(self, other): pass 93 def __rdiv__(self, other): pass 94 def __floordiv__(self, other): pass 95 def __rfloordiv__(self, other): pass 96 def __mod__(self, other): pass 97 def __rmod__(self, other): pass 98 def __pow__(self, other): pass 99 def __rpow__(self, other): pass 100 def __lt__(self, other): pass 101 def __gt__(self, other): pass 102 def __le__(self, other): pass 103 def __ge__(self, other): pass 104 def __eq__(self, other): pass 105 def __ne__(self, other): pass 106 def __neg__(self): pass 107 def __pos__(self): pass 108 def __str__(self): pass 109 def __bool__(self): pass 110 111 class frozenset(object): 112 def __init__(self, iterable): pass 113 114 class function(object): 115 pass 116 117 class int(object): 118 def __init__(self, number_or_string=None): pass 119 def __iadd__(self, other): pass 120 def __isub__(self, other): pass 121 def __iand__(self, other): pass 122 def __ior__(self, other): pass 123 def __add__(self, other): pass 124 def __radd__(self, other): pass 125 def __sub__(self, other): pass 126 def __rsub__(self, other): pass 127 def __mul__(self, other): pass 128 def __rmul__(self, other): pass 129 def __div__(self, other): pass 130 def __rdiv__(self, other): pass 131 def __floordiv__(self, other): pass 132 def __rfloordiv__(self, other): pass 133 def __mod__(self, other): pass 134 def __rmod__(self, other): pass 135 def __pow__(self, other): pass 136 def __rpow__(self, other): pass 137 def __and__(self, other): pass 138 def __rand__(self, other): pass 139 def __or__(self, other): pass 140 def __ror__(self, other): pass 141 def __xor__(self, other): pass 142 def __rxor__(self, other): pass 143 def __lt__(self, other): pass 144 def __gt__(self, other): pass 145 def __le__(self, other): pass 146 def __ge__(self, other): pass 147 def __eq__(self, other): pass 148 def __ne__(self, other): pass 149 def __neg__(self): pass 150 def __pos__(self): pass 151 def __str__(self): pass 152 def __bool__(self): pass 153 def __lshift__(self): pass 154 def __rlshift__(self): pass 155 def __rshift__(self): pass 156 def __rrshift__(self): pass 157 158 class list(object): 159 160 "Implementation of list." 161 162 def __init__(self, args=None): 163 164 "Initialise the list." 165 166 self.__new__() 167 168 if args is not None: 169 self.extend(args) 170 171 def __new__(self): 172 # Reserve space for a fragment reference. 173 self._elements = None 174 175 def __getitem__(self, index): 176 177 "Return the item or slice specified by 'index'." 178 179 return _getitem(self, index) 180 181 def __contains__(self, value): pass 182 def __setitem__(self, index, value): pass 183 184 def __getslice__(self, start, end=None): 185 186 "Return a slice starting from 'start', with the optional 'end'." 187 188 return _getslice(self, start, end) 189 190 def __setslice__(self, start, end, slice): pass 191 def append(self, value): pass 192 193 def extend(self, iterable): 194 195 "Extend the list with the contents of 'iterable'." 196 197 for i in iterable: 198 self.append(i) 199 200 def sort(self, cmp=None, key=None, reverse=0): pass 201 def __len__(self): pass 202 def __add__(self, other): pass 203 def __iadd__(self, other): pass 204 def __str__(self): pass 205 def __bool__(self): pass 206 207 def __iter__(self): 208 209 "Return an iterator." 210 211 return listiterator(self) 212 213 # Special implementation methods. 214 215 def __get_single_item__(self, index): pass 216 217 class listiterator(object): 218 219 "Implementation of listiterator." 220 221 def __init__(self, l): 222 223 "Initialise with the given list 'l'." 224 225 self.l = l 226 self.i = 0 227 228 def next(self): 229 230 "Return the next item." 231 232 try: 233 value = self.l[self.i] 234 self.i += 1 235 return value 236 except IndexError: 237 raise StopIteration() 238 239 class long(object): 240 def __init__(self, number_or_string=None): pass 241 def __iadd__(self, other): pass 242 def __isub__(self, other): pass 243 def __add__(self, other): pass 244 def __radd__(self, other): pass 245 def __sub__(self, other): pass 246 def __rsub__(self, other): pass 247 def __mul__(self, other): pass 248 def __rmul__(self, other): pass 249 def __div__(self, other): pass 250 def __rdiv__(self, other): pass 251 def __floordiv__(self, other): pass 252 def __rfloordiv__(self, other): pass 253 def __and__(self, other): pass 254 def __rand__(self, other): pass 255 def __or__(self, other): pass 256 def __ror__(self, other): pass 257 def __xor__(self, other): pass 258 def __rxor__(self, other): pass 259 def __lt__(self, other): pass 260 def __gt__(self, other): pass 261 def __le__(self, other): pass 262 def __ge__(self, other): pass 263 def __eq__(self, other): pass 264 def __ne__(self, other): pass 265 def __neg__(self): pass 266 def __pos__(self): pass 267 def __str__(self): pass 268 def __bool__(self): pass 269 270 class set(object): 271 def __init__(self, iterable): pass 272 273 # See below for slice. 274 275 class str(basestring): 276 pass 277 278 class type(object): 279 pass 280 281 class tuple(object): 282 283 #"Implementation of tuple." 284 285 def __init__(self, args): pass 286 287 def __getitem__(self, index): 288 289 "Return the item or slice specified by 'index'." 290 291 return _getitem(self, index) 292 293 def __getslice__(self, start, end=None): 294 295 "Return a slice starting from 'start', with the optional 'end'." 296 297 return _tuple(_getslice(self, start, end)) 298 299 def __len__(self): pass 300 def __add__(self, other): pass 301 def __str__(self): pass 302 def __bool__(self): pass 303 304 def __iter__(self): 305 306 "Return an iterator." 307 308 return listiterator(self) 309 310 # Special implementation methods. 311 312 def __get_single_item__(self, index): pass 313 314 class unicode(basestring): 315 pass 316 317 class xrange(object): 318 319 "Implementation of xrange." 320 321 NO_END = object() 322 323 def __init__(self, start_or_end, end=NO_END, step=1): 324 325 "Initialise the xrange with the given 'start_or_end', 'end' and 'step'." 326 327 if end is xrange.NO_END: 328 self.start = 0 329 self.end = start_or_end 330 else: 331 self.start = start_or_end 332 self.end = end 333 334 self.step = step 335 self.current = self.start 336 self.limited = self.end is not xrange.NO_END 337 338 def __iter__(self): 339 340 "Return an iterator, currently self." 341 342 return self 343 344 def next(self): 345 346 "Return the next item or raise a StopIteration exception." 347 348 if self.limited: 349 if self.step < 0 and self.current <= self.end or self.step > 0 and self.current >= self.end: 350 raise StopIteration() 351 352 current = self.current 353 self.current += self.step 354 return current 355 356 class slice(xrange): 357 358 "Implementation of slice." 359 360 def __init__(self, start_or_end=None, end=xrange.NO_END, step=1): 361 362 "Initialise the slice with the given 'start_or_end', 'end' and 'step'." 363 364 xrange.__init__(self, start_or_end, end, step) 365 366 # Exceptions and warnings. 367 368 class BaseException(object): 369 370 "Implementation of BaseException." 371 372 def __init__(self, *args): 373 self._pc = None # remember where the exception occurred 374 self.args = args 375 376 class Exception(BaseException): pass 377 class Warning(object): pass 378 379 class ArithmeticError(Exception): pass 380 class AssertionError(Exception): pass 381 class AttributeError(Exception): pass 382 class DeprecationWarning(Exception): pass 383 class EOFError(Exception): pass 384 class EnvironmentError(Exception): pass 385 class FloatingPointError(Exception): pass 386 class FutureWarning(Warning): pass 387 class GeneratorExit(Exception): pass 388 class ImportError(Exception): pass 389 class ImportWarning(Warning): pass 390 class IndentationError(Exception): pass 391 class IndexError(Exception): pass 392 class IOError(Exception): pass 393 class KeyError(Exception): pass 394 class KeyboardInterrupt(Exception): pass 395 class LookupError(Exception): pass 396 class MemoryError(Exception): pass 397 class NameError(Exception): pass 398 class NotImplementedError(Exception): pass 399 class OSError(Exception): pass 400 class OverflowError(Exception): pass 401 class PendingDeprecationWarning(Warning): pass 402 class ReferenceError(Exception): pass 403 class RuntimeError(Exception): pass 404 class RuntimeWarning(Warning): pass 405 class StandardError(Exception): pass 406 class StopIteration(Exception): "Implementation of StopIteration." 407 class SyntaxError(Exception): pass 408 class SyntaxWarning(Warning): pass 409 class SystemError(Exception): pass 410 class SystemExit(Exception): pass 411 class TabError(Exception): pass 412 class TypeError(Exception): pass 413 class UnboundLocalError(Exception): pass 414 class UnicodeDecodeError(Exception): pass 415 class UnicodeEncodeError(Exception): pass 416 class UnicodeError(Exception): pass 417 class UnicodeTranslateError(Exception): pass 418 class UnicodeWarning(Warning): pass 419 class UserWarning(Warning): pass 420 class ValueError(Exception): pass 421 class ZeroDivisionError(Exception): pass 422 423 # Various types. 424 425 #class ellipsis: pass 426 class NoneType: pass 427 class NotImplementedType: pass 428 429 # General functions. 430 # NOTE: Some of these are actually provided by classes in CPython. 431 # NOTE: We may refuse to support some of these in practice, such as... 432 # NOTE: __import__, super, reload. 433 434 def abs(number): pass 435 def all(iterable): pass 436 def any(iterable): pass 437 def callable(obj): pass 438 def chr(i): pass 439 def classmethod(function): pass 440 def cmp(x, y): pass 441 def compile(source, filename, mode, flags=None, dont_inherit=None): pass 442 def delattr(obj, name): pass 443 def dir(obj=None): pass 444 def divmod(x, y): pass 445 def enumerate(iterable): pass 446 def eval(source, globals=None, locals=None): pass 447 def execfile(filename, globals=None, locals=None): pass 448 def filter(function, sequence): pass 449 def getattr(obj, name, default=None): pass 450 def globals(): pass 451 def hasattr(obj, name): pass 452 def hash(obj): pass 453 def help(*args): pass 454 def hex(number): pass 455 def id(obj): pass 456 def input(prompt=None): pass 457 458 def isinstance(obj, cls_or_tuple): 459 460 """ 461 Return whether 'obj' is an instance of 'cls_or_tuple', where the latter is 462 either a class or a tuple of classes. 463 """ 464 465 # NOTE: CPython insists on tuples, but any sequence might be considered 466 # NOTE: acceptable. 467 468 if _isinstance(cls_or_tuple, tuple): 469 for cls in cls_or_tuple: 470 if obj.__class__ is cls or _isinstance(obj, cls): 471 return True 472 return False 473 else: 474 return obj.__class__ is cls_or_tuple or _isinstance(obj, cls_or_tuple) 475 476 def issubclass(obj, cls_or_tuple): pass 477 478 def iter(collection): 479 480 "Implementation of iter without callable plus sentinel support." 481 482 return collection.__iter__() 483 484 def len(obj): 485 486 "Implementation of len." 487 488 return obj.__len__() 489 490 def locals(): pass 491 492 def map(function, *args): pass 493 494 def max(*args): 495 496 "Implementation of max." 497 498 highest = args[0] 499 for arg in args[1:]: 500 if arg > highest: 501 highest = arg 502 return highest 503 504 def min(*args): 505 506 "Implementation of min." 507 508 lowest = args[0] 509 for arg in args[1:]: 510 if arg > lowest: 511 lowest = arg 512 return lowest 513 514 def oct(number): pass 515 def open(name, mode=None, buffering=None): pass 516 def ord(c): pass 517 def pow(x, y, z=None): pass 518 def property(fget=None, fset=None, fdel=None, doc=None): pass 519 520 def range(start_or_end, end=None, step=1): 521 522 "Implementation of range." 523 524 return list(xrange(start_or_end, end, step)) 525 526 def raw_input(prompt=None): pass 527 def reduce(function, sequence, initial=None): pass 528 def reload(module): pass 529 def repr(obj): pass 530 def reversed(sequence): pass 531 def round(number, ndigits=None): pass 532 def setattr(obj, name, value): pass 533 def sorted(iterable, cmp=None, key=None, reverse=False): pass 534 def staticmethod(function): pass 535 def sum(sequence, start=0): pass 536 def super(*args): pass 537 def unichr(i): pass 538 def vars(obj=None): pass 539 def zip(*args): pass 540 541 # Utility functions. 542 543 def _get_absolute_index(index, length): 544 545 """ 546 Return the absolute index for 'index' given a collection having the 547 specified 'length'. 548 """ 549 550 if index < 0: 551 return length + index 552 else: 553 return index 554 555 def _normalise_index(index, length): 556 557 "Normalise 'index' for a collection having the specified 'length'." 558 559 return _min(length, _max(0, _get_absolute_index(index, length))) 560 561 def _max(x, y): 562 563 "Return the maximum of 'x' and 'y'." 564 565 if x >= y: 566 return x 567 else: 568 return y 569 570 def _min(x, y): 571 572 "Return the minimum of 'x' and 'y'." 573 574 if x <= y: 575 return x 576 else: 577 return y 578 579 def _getitem(seq, index): 580 581 "Return the item or slice specified by 'index'." 582 583 if isinstance(index, int): 584 index = _normalise_index(index, len(seq)) 585 return seq.__get_single_item__(index) 586 elif isinstance(index, slice): 587 return seq.__getslice__(index.start, index.end) 588 else: 589 raise TypeError 590 591 def _getslice(seq, start, end=None): 592 593 "Return a slice starting from 'start', with the optional 'end'." 594 595 length = len(seq) 596 597 if start is None: 598 start = 0 599 else: 600 start = _normalise_index(start, length) 601 602 if end is None: 603 end = length 604 else: 605 end = _normalise_index(end, length) 606 607 result = [] 608 while start < end: 609 result.append(seq.__get_single_item__(start)) 610 start += 1 611 return result 612 613 # Special implementation functions. 614 615 def _isinstance(obj, cls): pass 616 def _print(dest, *args): pass 617 def _printnl(dest, *args): pass 618 def _tuple(l): pass 619 620 # Reference some names to ensure their existence. This should be everything 621 # mentioned in a get_builtin or load_builtin call. Instances from this module 622 # should be predefined constants. 623 624 function 625 AttributeError 626 #IndexError 627 #NoneType 628 #NotImplementedType 629 #StopIteration 630 TypeError 631 632 #bool 633 #ellipsis 634 #list 635 tuple 636 type 637 #xrange 638 639 # vim: tabstop=4 expandtab shiftwidth=4