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