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 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 def __init__(self, args): pass 283 284 def __getitem__(self, index): 285 286 "Return the item or slice specified by 'index'." 287 288 return _getitem(self, index) 289 290 def __getslice__(self, start, end=None): 291 292 "Return a slice starting from 'start', with the optional 'end'." 293 294 return tuple(_getslice(self, start, end)) 295 296 def __len__(self): pass 297 def __add__(self, other): pass 298 def __str__(self): pass 299 def __bool__(self): pass 300 301 def __iter__(self): 302 303 "Return an iterator." 304 305 return listiterator(self) 306 307 # Special implementation methods. 308 309 def __get_single_item__(self, index): pass 310 311 class unicode(basestring): 312 pass 313 314 class xrange(object): 315 316 "Implementation of xrange." 317 318 NO_END = object() 319 320 def __init__(self, start_or_end, end=NO_END, step=1): 321 322 "Initialise the xrange with the given 'start_or_end', 'end' and 'step'." 323 324 if end is xrange.NO_END: 325 self.start = 0 326 self.end = start_or_end 327 else: 328 self.start = start_or_end 329 self.end = end 330 331 self.step = step 332 self.current = self.start 333 self.limited = self.end is not xrange.NO_END 334 335 def __iter__(self): 336 337 "Return an iterator, currently self." 338 339 return self 340 341 def next(self): 342 343 "Return the next item or raise a StopIteration exception." 344 345 if self.limited: 346 if self.step < 0 and self.current <= self.end or self.step > 0 and self.current >= self.end: 347 raise StopIteration() 348 349 current = self.current 350 self.current += self.step 351 return current 352 353 class slice(xrange): 354 355 "Implementation of slice." 356 357 def __init__(self, start_or_end=None, end=xrange.NO_END, step=1): 358 359 "Initialise the slice with the given 'start_or_end', 'end' and 'step'." 360 361 xrange.__init__(self, start_or_end, end, step) 362 363 # Exceptions and warnings. 364 365 class BaseException(object): 366 367 "Implementation of BaseException." 368 369 def __init__(self, *args): 370 self.args = args 371 372 class Exception(BaseException): pass 373 class Warning(object): pass 374 375 class ArithmeticError(Exception): pass 376 class AssertionError(Exception): pass 377 class AttributeError(Exception): pass 378 class DeprecationWarning(Exception): pass 379 class EOFError(Exception): pass 380 class EnvironmentError(Exception): pass 381 class FloatingPointError(Exception): pass 382 class FutureWarning(Warning): pass 383 class GeneratorExit(Exception): pass 384 class ImportError(Exception): pass 385 class ImportWarning(Warning): pass 386 class IndentationError(Exception): pass 387 class IndexError(Exception): pass 388 class IOError(Exception): pass 389 class KeyError(Exception): pass 390 class KeyboardInterrupt(Exception): pass 391 class LookupError(Exception): pass 392 class MemoryError(Exception): pass 393 class NameError(Exception): pass 394 class NotImplementedError(Exception): pass 395 class OSError(Exception): pass 396 class OverflowError(Exception): pass 397 class PendingDeprecationWarning(Warning): pass 398 class ReferenceError(Exception): pass 399 class RuntimeError(Exception): pass 400 class RuntimeWarning(Warning): pass 401 class StandardError(Exception): pass 402 class StopIteration(Exception): "Implementation of StopIteration." 403 class SyntaxError(Exception): pass 404 class SyntaxWarning(Warning): pass 405 class SystemError(Exception): pass 406 class SystemExit(Exception): pass 407 class TabError(Exception): pass 408 class TypeError(Exception): pass 409 class UnboundLocalError(Exception): pass 410 class UnicodeDecodeError(Exception): pass 411 class UnicodeEncodeError(Exception): pass 412 class UnicodeError(Exception): pass 413 class UnicodeTranslateError(Exception): pass 414 class UnicodeWarning(Warning): pass 415 class UserWarning(Warning): pass 416 class ValueError(Exception): pass 417 class ZeroDivisionError(Exception): pass 418 419 # Various types. 420 421 #class ellipsis: pass 422 class NoneType: pass 423 class NotImplementedType: pass 424 425 # General functions. 426 # NOTE: Some of these are actually provided by classes in CPython. 427 # NOTE: We may refuse to support some of these in practice, such as... 428 # NOTE: __import__, super, reload. 429 430 def abs(number): pass 431 def all(iterable): pass 432 def any(iterable): pass 433 def callable(obj): pass 434 def chr(i): pass 435 def classmethod(function): pass 436 def cmp(x, y): pass 437 def compile(source, filename, mode, flags=None, dont_inherit=None): pass 438 def delattr(obj, name): pass 439 def dir(obj=None): pass 440 def divmod(x, y): pass 441 def enumerate(iterable): pass 442 def eval(source, globals=None, locals=None): pass 443 def execfile(filename, globals=None, locals=None): pass 444 def filter(function, sequence): pass 445 def getattr(obj, name, default=None): pass 446 def globals(): pass 447 def hasattr(obj, name): pass 448 def hash(obj): pass 449 def help(*args): pass 450 def hex(number): pass 451 def id(obj): pass 452 def input(prompt=None): pass 453 454 def isinstance(obj, cls_or_tuple): 455 456 """ 457 Return whether 'obj' is an instance of 'cls_or_tuple', where the latter is 458 either a class or a tuple of classes. 459 """ 460 461 # NOTE: CPython insists on tuples, but any sequence might be considered 462 # NOTE: acceptable. 463 # NOTE: tuple.__class__ is tuple in micropython! 464 465 if cls_or_tuple is not tuple and cls_or_tuple.__class__ is tuple: 466 for cls in cls_or_tuple: 467 if _isinstance(obj, cls): 468 return True 469 return False 470 else: 471 return _isinstance(obj, cls_or_tuple) 472 473 def issubclass(obj, cls_or_tuple): pass 474 475 def iter(collection): 476 477 "Implementation of iter without callable plus sentinel support." 478 479 return collection.__iter__() 480 481 def len(obj): 482 483 "Implementation of len." 484 485 return obj.__len__() 486 487 def locals(): pass 488 489 def map(function, *args): pass 490 491 def max(*args): 492 493 "Implementation of max." 494 495 highest = args[0] 496 for arg in args[1:]: 497 if arg > highest: 498 highest = arg 499 return highest 500 501 def min(*args): 502 503 "Implementation of min." 504 505 lowest = args[0] 506 for arg in args[1:]: 507 if arg > lowest: 508 lowest = arg 509 return lowest 510 511 def oct(number): pass 512 def open(name, mode=None, buffering=None): pass 513 def ord(c): pass 514 def pow(x, y, z=None): pass 515 def property(fget=None, fset=None, fdel=None, doc=None): pass 516 517 def range(start_or_end, end=None, step=1): 518 519 "Implementation of range." 520 521 return list(xrange(start_or_end, end, step)) 522 523 def raw_input(prompt=None): pass 524 def reduce(function, sequence, initial=None): pass 525 def reload(module): pass 526 def repr(obj): pass 527 def reversed(sequence): pass 528 def round(number, ndigits=None): pass 529 def setattr(obj, name, value): pass 530 def sorted(iterable, cmp=None, key=None, reverse=False): pass 531 def staticmethod(function): pass 532 def sum(sequence, start=0): pass 533 def super(*args): pass 534 def unichr(i): pass 535 def vars(obj=None): pass 536 def zip(*args): pass 537 538 # Utility functions. 539 540 def _get_absolute_index(index, length): 541 542 """ 543 Return the absolute index for 'index' given a collection having the 544 specified 'length'. 545 """ 546 547 if index < 0: 548 return length + index 549 else: 550 return index 551 552 def _normalise_index(index, length): 553 554 "Normalise 'index' for a collection having the specified 'length'." 555 556 return _min(length, _max(0, _get_absolute_index(index, length))) 557 558 def _max(x, y): 559 560 "Return the maximum of 'x' and 'y'." 561 562 if x >= y: 563 return x 564 else: 565 return y 566 567 def _min(x, y): 568 569 "Return the minimum of 'x' and 'y'." 570 571 if x <= y: 572 return x 573 else: 574 return y 575 576 def _getitem(seq, index): 577 578 "Return the item or slice specified by 'index'." 579 580 if isinstance(index, int): 581 return seq.__get_single_item__(index) 582 elif isinstance(index, slice): 583 return seq.__getslice__(index.start, index.end) 584 else: 585 raise TypeError 586 587 def _getslice(seq, start, end=None): 588 589 "Return a slice starting from 'start', with the optional 'end'." 590 591 length = len(seq) 592 593 if start is None: 594 start = 0 595 else: 596 start = _normalise_index(start, length) 597 598 if end is None: 599 end = length 600 else: 601 end = _normalise_index(end, length) 602 603 result = [] 604 while start < end: 605 result.append(seq.__get_single_item__(start)) 606 start += 1 607 return result 608 609 # Special implementation functions. 610 611 def _isinstance(obj, cls): pass 612 613 # Reference some names to ensure their existence. This should be everything 614 # mentioned in a get_builtin or load_builtin call. Instances from this module 615 # should be predefined constants. 616 617 function 618 AttributeError 619 #StopIteration 620 TypeError 621 #IndexError 622 623 #list 624 tuple 625 #xrange 626 #ellipsis 627 #bool 628 629 # vim: tabstop=4 expandtab shiftwidth=4