paulb@22 | 1 | #!/usr/bin/env python |
paulb@22 | 2 | |
paulb@22 | 3 | """ |
paulb@22 | 4 | Simple built-in classes and functions. |
paulb@22 | 5 | |
paulb@22 | 6 | Copyright (C) 2005, 2006 Paul Boddie <paul@boddie.org.uk> |
paulb@22 | 7 | |
paulb@22 | 8 | This software is free software; you can redistribute it and/or |
paulb@22 | 9 | modify it under the terms of the GNU General Public License as |
paulb@22 | 10 | published by the Free Software Foundation; either version 2 of |
paulb@22 | 11 | the License, or (at your option) any later version. |
paulb@22 | 12 | |
paulb@22 | 13 | This software is distributed in the hope that it will be useful, |
paulb@22 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@22 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paulb@22 | 16 | GNU General Public License for more details. |
paulb@22 | 17 | |
paulb@22 | 18 | You should have received a copy of the GNU General Public |
paulb@22 | 19 | License along with this library; see the file LICENCE.txt |
paulb@22 | 20 | If not, write to the Free Software Foundation, Inc., |
paulb@22 | 21 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@22 | 22 | """ |
paulb@22 | 23 | |
paulb@129 | 24 | class object: |
paulb@130 | 25 | def __bool__(self): |
paulb@130 | 26 | # NOTE: Should really test for __len__ and then resort to False. |
paulb@130 | 27 | # NOTE: See the reference: 3.3.1 Basic customization |
paulb@129 | 28 | return False |
paulb@129 | 29 | |
paulb@130 | 30 | class bool: |
paulb@130 | 31 | def __bool__(self): |
paulb@22 | 32 | return self |
paulb@22 | 33 | |
paulb@22 | 34 | def __str__(self): |
paulb@22 | 35 | if self: |
paulb@22 | 36 | return "True" |
paulb@22 | 37 | else: |
paulb@22 | 38 | return "False" |
paulb@22 | 39 | |
paulb@22 | 40 | class int: |
paulb@22 | 41 | def __iadd__(self, other): |
paulb@22 | 42 | if isinstance(other, int): |
paulb@22 | 43 | return int() |
paulb@22 | 44 | else: |
paulb@55 | 45 | raise TypeError |
paulb@22 | 46 | |
paulb@22 | 47 | def __isub__(self, other): |
paulb@22 | 48 | if isinstance(other, int): |
paulb@22 | 49 | return int() |
paulb@22 | 50 | else: |
paulb@55 | 51 | raise TypeError |
paulb@22 | 52 | |
paulb@22 | 53 | def __add__(self, other): |
paulb@22 | 54 | if isinstance(other, int): |
paulb@22 | 55 | return int() |
paulb@22 | 56 | else: |
paulb@55 | 57 | raise TypeError |
paulb@22 | 58 | |
paulb@22 | 59 | def __radd__(self, other): |
paulb@22 | 60 | if isinstance(other, int): |
paulb@22 | 61 | return int() |
paulb@22 | 62 | else: |
paulb@55 | 63 | raise TypeError |
paulb@22 | 64 | |
paulb@22 | 65 | def __sub__(self, other): |
paulb@22 | 66 | if isinstance(other, int): |
paulb@22 | 67 | return int() |
paulb@22 | 68 | else: |
paulb@55 | 69 | raise TypeError |
paulb@22 | 70 | |
paulb@22 | 71 | def __rsub__(self, other): |
paulb@22 | 72 | if isinstance(other, int): |
paulb@22 | 73 | return int() |
paulb@22 | 74 | else: |
paulb@55 | 75 | raise TypeError |
paulb@22 | 76 | |
paulb@22 | 77 | def __mul__(self, other): |
paulb@22 | 78 | if isinstance(other, int): |
paulb@22 | 79 | return int() |
paulb@22 | 80 | else: |
paulb@55 | 81 | raise TypeError |
paulb@22 | 82 | |
paulb@22 | 83 | def __rmul__(self, other): |
paulb@22 | 84 | if isinstance(other, int): |
paulb@22 | 85 | return int() |
paulb@22 | 86 | else: |
paulb@55 | 87 | raise TypeError |
paulb@22 | 88 | |
paulb@22 | 89 | def __div__(self, other): |
paulb@22 | 90 | if isinstance(other, int): |
paulb@22 | 91 | return int() |
paulb@22 | 92 | else: |
paulb@55 | 93 | raise TypeError |
paulb@22 | 94 | |
paulb@22 | 95 | def __rdiv__(self, other): |
paulb@22 | 96 | if isinstance(other, int): |
paulb@22 | 97 | return int() |
paulb@22 | 98 | else: |
paulb@55 | 99 | raise TypeError |
paulb@22 | 100 | |
paulb@136 | 101 | def __floordiv__(self, other): |
paulb@136 | 102 | if isinstance(other, int): |
paulb@136 | 103 | return int() |
paulb@136 | 104 | else: |
paulb@136 | 105 | raise TypeError |
paulb@136 | 106 | |
paulb@136 | 107 | def __rfloordiv__(self, other): |
paulb@136 | 108 | if isinstance(other, int): |
paulb@136 | 109 | return int() |
paulb@136 | 110 | else: |
paulb@136 | 111 | raise TypeError |
paulb@136 | 112 | |
paulb@22 | 113 | def __pow__(self, other): |
paulb@22 | 114 | if isinstance(other, int): |
paulb@22 | 115 | return int() |
paulb@22 | 116 | else: |
paulb@55 | 117 | raise TypeError |
paulb@22 | 118 | |
paulb@22 | 119 | def __lt__(self, other): |
paulb@22 | 120 | if isinstance(other, int): |
paulb@130 | 121 | return bool() |
paulb@22 | 122 | else: |
paulb@55 | 123 | raise TypeError |
paulb@22 | 124 | |
paulb@22 | 125 | def __gt__(self, other): |
paulb@22 | 126 | if isinstance(other, int): |
paulb@130 | 127 | return bool() |
paulb@22 | 128 | else: |
paulb@55 | 129 | raise TypeError |
paulb@22 | 130 | |
paulb@22 | 131 | def __le__(self, other): |
paulb@22 | 132 | if isinstance(other, int): |
paulb@130 | 133 | return bool() |
paulb@22 | 134 | else: |
paulb@55 | 135 | raise TypeError |
paulb@22 | 136 | |
paulb@22 | 137 | def __ge__(self, other): |
paulb@22 | 138 | if isinstance(other, int): |
paulb@130 | 139 | return bool() |
paulb@22 | 140 | else: |
paulb@55 | 141 | raise TypeError |
paulb@22 | 142 | |
paulb@22 | 143 | def __eq__(self, other): |
paulb@22 | 144 | if isinstance(other, int): |
paulb@130 | 145 | return bool() |
paulb@22 | 146 | else: |
paulb@55 | 147 | raise TypeError |
paulb@22 | 148 | |
paulb@22 | 149 | def __ne__(self, other): |
paulb@22 | 150 | if isinstance(other, int): |
paulb@130 | 151 | return bool() |
paulb@22 | 152 | else: |
paulb@55 | 153 | raise TypeError |
paulb@22 | 154 | |
paulb@22 | 155 | def __neg__(self): |
paulb@22 | 156 | return int() |
paulb@22 | 157 | |
paulb@22 | 158 | def __pos__(self): |
paulb@22 | 159 | return self |
paulb@22 | 160 | |
paulb@22 | 161 | def __str__(self): |
paulb@22 | 162 | return string() |
paulb@22 | 163 | |
paulb@130 | 164 | def __bool__(self): |
paulb@22 | 165 | return self != 0 |
paulb@22 | 166 | |
paulb@22 | 167 | class long: |
paulb@22 | 168 | def __iadd__(self, other): |
paulb@22 | 169 | if isinstance(other, int): |
paulb@22 | 170 | return long() |
paulb@22 | 171 | elif isinstance(other, long): |
paulb@22 | 172 | return long() |
paulb@22 | 173 | else: |
paulb@55 | 174 | raise TypeError |
paulb@22 | 175 | |
paulb@22 | 176 | def __isub__(self, other): |
paulb@22 | 177 | if isinstance(other, int): |
paulb@22 | 178 | return long() |
paulb@22 | 179 | elif isinstance(other, long): |
paulb@22 | 180 | return long() |
paulb@22 | 181 | else: |
paulb@55 | 182 | raise TypeError |
paulb@22 | 183 | |
paulb@22 | 184 | def __add__(self, other): |
paulb@22 | 185 | if isinstance(other, int): |
paulb@22 | 186 | return long() |
paulb@22 | 187 | elif isinstance(other, long): |
paulb@22 | 188 | return long() |
paulb@22 | 189 | else: |
paulb@55 | 190 | raise TypeError |
paulb@22 | 191 | |
paulb@22 | 192 | def __radd__(self, other): |
paulb@22 | 193 | if isinstance(other, int): |
paulb@22 | 194 | return long() |
paulb@22 | 195 | elif isinstance(other, long): |
paulb@22 | 196 | return long() |
paulb@22 | 197 | else: |
paulb@55 | 198 | raise TypeError |
paulb@22 | 199 | |
paulb@22 | 200 | def __sub__(self, other): |
paulb@22 | 201 | if isinstance(other, int): |
paulb@22 | 202 | return long() |
paulb@22 | 203 | elif isinstance(other, long): |
paulb@22 | 204 | return long() |
paulb@22 | 205 | else: |
paulb@55 | 206 | raise TypeError |
paulb@22 | 207 | |
paulb@22 | 208 | def __rsub__(self, other): |
paulb@22 | 209 | if isinstance(other, int): |
paulb@22 | 210 | return long() |
paulb@22 | 211 | elif isinstance(other, long): |
paulb@22 | 212 | return long() |
paulb@22 | 213 | else: |
paulb@55 | 214 | raise TypeError |
paulb@22 | 215 | |
paulb@22 | 216 | def __lt__(self, other): |
paulb@22 | 217 | if isinstance(other, int): |
paulb@130 | 218 | return bool() |
paulb@22 | 219 | elif isinstance(other, long): |
paulb@130 | 220 | return bool() |
paulb@22 | 221 | else: |
paulb@55 | 222 | raise TypeError |
paulb@22 | 223 | |
paulb@22 | 224 | def __gt__(self, other): |
paulb@22 | 225 | if isinstance(other, int): |
paulb@130 | 226 | return bool() |
paulb@22 | 227 | elif isinstance(other, long): |
paulb@130 | 228 | return bool() |
paulb@22 | 229 | else: |
paulb@55 | 230 | raise TypeError |
paulb@22 | 231 | |
paulb@22 | 232 | def __le__(self, other): |
paulb@22 | 233 | if isinstance(other, int): |
paulb@130 | 234 | return bool() |
paulb@22 | 235 | elif isinstance(other, long): |
paulb@130 | 236 | return bool() |
paulb@22 | 237 | else: |
paulb@55 | 238 | raise TypeError |
paulb@22 | 239 | |
paulb@22 | 240 | def __ge__(self, other): |
paulb@22 | 241 | if isinstance(other, int): |
paulb@130 | 242 | return bool() |
paulb@22 | 243 | elif isinstance(other, long): |
paulb@130 | 244 | return bool() |
paulb@22 | 245 | else: |
paulb@55 | 246 | raise TypeError |
paulb@22 | 247 | |
paulb@22 | 248 | def __eq__(self, other): |
paulb@22 | 249 | if isinstance(other, int): |
paulb@130 | 250 | return bool() |
paulb@22 | 251 | elif isinstance(other, long): |
paulb@130 | 252 | return bool() |
paulb@22 | 253 | else: |
paulb@55 | 254 | raise TypeError |
paulb@22 | 255 | |
paulb@22 | 256 | def __ne__(self, other): |
paulb@22 | 257 | if isinstance(other, int): |
paulb@130 | 258 | return bool() |
paulb@22 | 259 | elif isinstance(other, long): |
paulb@130 | 260 | return bool() |
paulb@22 | 261 | else: |
paulb@55 | 262 | raise TypeError |
paulb@22 | 263 | |
paulb@22 | 264 | def __neg__(self): |
paulb@22 | 265 | return long() |
paulb@22 | 266 | |
paulb@22 | 267 | def __pos__(self): |
paulb@22 | 268 | return self |
paulb@22 | 269 | |
paulb@22 | 270 | def __str__(self): |
paulb@22 | 271 | return string() |
paulb@22 | 272 | |
paulb@130 | 273 | def __bool__(self): |
paulb@22 | 274 | return self != 0 |
paulb@22 | 275 | |
paulb@22 | 276 | class float: |
paulb@22 | 277 | def __iadd__(self, other): |
paulb@22 | 278 | if isinstance(other, int): |
paulb@22 | 279 | return float() |
paulb@22 | 280 | elif isinstance(other, long): |
paulb@22 | 281 | return float() |
paulb@22 | 282 | elif isinstance(other, float): |
paulb@22 | 283 | return float() |
paulb@22 | 284 | else: |
paulb@55 | 285 | raise TypeError |
paulb@22 | 286 | |
paulb@22 | 287 | def __isub__(self, other): |
paulb@22 | 288 | if isinstance(other, int): |
paulb@22 | 289 | return float() |
paulb@22 | 290 | elif isinstance(other, long): |
paulb@22 | 291 | return float() |
paulb@22 | 292 | elif isinstance(other, float): |
paulb@22 | 293 | return float() |
paulb@22 | 294 | else: |
paulb@55 | 295 | raise TypeError |
paulb@22 | 296 | |
paulb@22 | 297 | def __add__(self, other): |
paulb@22 | 298 | if isinstance(other, int): |
paulb@22 | 299 | return float() |
paulb@22 | 300 | elif isinstance(other, long): |
paulb@22 | 301 | return float() |
paulb@22 | 302 | elif isinstance(other, float): |
paulb@22 | 303 | return float() |
paulb@22 | 304 | else: |
paulb@55 | 305 | raise TypeError |
paulb@22 | 306 | |
paulb@22 | 307 | def __radd__(self, other): |
paulb@22 | 308 | if isinstance(other, int): |
paulb@22 | 309 | return float() |
paulb@22 | 310 | elif isinstance(other, long): |
paulb@22 | 311 | return float() |
paulb@22 | 312 | elif isinstance(other, float): |
paulb@22 | 313 | return float() |
paulb@22 | 314 | else: |
paulb@55 | 315 | raise TypeError |
paulb@22 | 316 | |
paulb@22 | 317 | def __sub__(self, other): |
paulb@22 | 318 | if isinstance(other, int): |
paulb@22 | 319 | return float() |
paulb@22 | 320 | elif isinstance(other, long): |
paulb@22 | 321 | return float() |
paulb@22 | 322 | elif isinstance(other, float): |
paulb@22 | 323 | return float() |
paulb@22 | 324 | else: |
paulb@55 | 325 | raise TypeError |
paulb@22 | 326 | |
paulb@22 | 327 | def __rsub__(self, other): |
paulb@22 | 328 | if isinstance(other, int): |
paulb@22 | 329 | return float() |
paulb@22 | 330 | elif isinstance(other, long): |
paulb@22 | 331 | return float() |
paulb@22 | 332 | elif isinstance(other, float): |
paulb@22 | 333 | return float() |
paulb@22 | 334 | else: |
paulb@55 | 335 | raise TypeError |
paulb@22 | 336 | |
paulb@22 | 337 | def __mul__(self, other): |
paulb@22 | 338 | if isinstance(other, int): |
paulb@22 | 339 | return float() |
paulb@22 | 340 | elif isinstance(other, long): |
paulb@22 | 341 | return float() |
paulb@22 | 342 | elif isinstance(other, float): |
paulb@22 | 343 | return float() |
paulb@22 | 344 | else: |
paulb@55 | 345 | raise TypeError |
paulb@22 | 346 | |
paulb@22 | 347 | def __rmul__(self, other): |
paulb@22 | 348 | if isinstance(other, int): |
paulb@22 | 349 | return float() |
paulb@22 | 350 | elif isinstance(other, long): |
paulb@22 | 351 | return float() |
paulb@22 | 352 | elif isinstance(other, float): |
paulb@22 | 353 | return float() |
paulb@22 | 354 | else: |
paulb@55 | 355 | raise TypeError |
paulb@22 | 356 | |
paulb@22 | 357 | def __div__(self, other): |
paulb@22 | 358 | if isinstance(other, int): |
paulb@22 | 359 | return float() |
paulb@22 | 360 | elif isinstance(other, long): |
paulb@22 | 361 | return float() |
paulb@22 | 362 | elif isinstance(other, float): |
paulb@22 | 363 | return float() |
paulb@22 | 364 | else: |
paulb@55 | 365 | raise TypeError |
paulb@22 | 366 | |
paulb@22 | 367 | def __rdiv__(self, other): |
paulb@22 | 368 | if isinstance(other, int): |
paulb@22 | 369 | return float() |
paulb@22 | 370 | elif isinstance(other, long): |
paulb@22 | 371 | return float() |
paulb@22 | 372 | elif isinstance(other, float): |
paulb@22 | 373 | return float() |
paulb@22 | 374 | else: |
paulb@55 | 375 | raise TypeError |
paulb@22 | 376 | |
paulb@136 | 377 | def __floordiv__(self, other): |
paulb@136 | 378 | if isinstance(other, int): |
paulb@136 | 379 | return float() |
paulb@136 | 380 | elif isinstance(other, long): |
paulb@136 | 381 | return float() |
paulb@136 | 382 | elif isinstance(other, float): |
paulb@136 | 383 | return float() |
paulb@136 | 384 | else: |
paulb@136 | 385 | raise TypeError |
paulb@136 | 386 | |
paulb@136 | 387 | def __rfloordiv__(self, other): |
paulb@136 | 388 | if isinstance(other, int): |
paulb@136 | 389 | return float() |
paulb@136 | 390 | elif isinstance(other, long): |
paulb@136 | 391 | return float() |
paulb@136 | 392 | elif isinstance(other, float): |
paulb@136 | 393 | return float() |
paulb@136 | 394 | else: |
paulb@136 | 395 | raise TypeError |
paulb@136 | 396 | |
paulb@22 | 397 | def __pow__(self, other): |
paulb@22 | 398 | if isinstance(other, int): |
paulb@22 | 399 | return float() |
paulb@22 | 400 | elif isinstance(other, long): |
paulb@22 | 401 | return float() |
paulb@22 | 402 | elif isinstance(other, float): |
paulb@22 | 403 | return float() |
paulb@22 | 404 | else: |
paulb@55 | 405 | raise TypeError |
paulb@22 | 406 | |
paulb@22 | 407 | def __rpow__(self, other): |
paulb@22 | 408 | if isinstance(other, int): |
paulb@22 | 409 | return float() |
paulb@22 | 410 | elif isinstance(other, long): |
paulb@22 | 411 | return float() |
paulb@22 | 412 | elif isinstance(other, float): |
paulb@22 | 413 | return float() |
paulb@22 | 414 | else: |
paulb@55 | 415 | raise TypeError |
paulb@22 | 416 | |
paulb@22 | 417 | def __lt__(self, other): |
paulb@22 | 418 | if isinstance(other, int): |
paulb@130 | 419 | return bool() |
paulb@22 | 420 | elif isinstance(other, long): |
paulb@130 | 421 | return bool() |
paulb@22 | 422 | elif isinstance(other, float): |
paulb@130 | 423 | return bool() |
paulb@22 | 424 | else: |
paulb@55 | 425 | raise TypeError |
paulb@22 | 426 | |
paulb@22 | 427 | def __gt__(self, other): |
paulb@22 | 428 | if isinstance(other, int): |
paulb@130 | 429 | return bool() |
paulb@22 | 430 | elif isinstance(other, long): |
paulb@130 | 431 | return bool() |
paulb@22 | 432 | elif isinstance(other, float): |
paulb@130 | 433 | return bool() |
paulb@22 | 434 | else: |
paulb@55 | 435 | raise TypeError |
paulb@22 | 436 | |
paulb@22 | 437 | def __le__(self, other): |
paulb@22 | 438 | if isinstance(other, int): |
paulb@130 | 439 | return bool() |
paulb@22 | 440 | elif isinstance(other, long): |
paulb@130 | 441 | return bool() |
paulb@22 | 442 | elif isinstance(other, float): |
paulb@130 | 443 | return bool() |
paulb@22 | 444 | else: |
paulb@55 | 445 | raise TypeError |
paulb@22 | 446 | |
paulb@22 | 447 | def __ge__(self, other): |
paulb@22 | 448 | if isinstance(other, int): |
paulb@130 | 449 | return bool() |
paulb@22 | 450 | elif isinstance(other, long): |
paulb@130 | 451 | return bool() |
paulb@22 | 452 | elif isinstance(other, float): |
paulb@130 | 453 | return bool() |
paulb@22 | 454 | else: |
paulb@55 | 455 | raise TypeError |
paulb@22 | 456 | |
paulb@22 | 457 | def __eq__(self, other): |
paulb@22 | 458 | if isinstance(other, int): |
paulb@130 | 459 | return bool() |
paulb@22 | 460 | elif isinstance(other, long): |
paulb@130 | 461 | return bool() |
paulb@22 | 462 | elif isinstance(other, float): |
paulb@130 | 463 | return bool() |
paulb@22 | 464 | else: |
paulb@55 | 465 | raise TypeError |
paulb@22 | 466 | |
paulb@22 | 467 | def __ne__(self, other): |
paulb@22 | 468 | if isinstance(other, int): |
paulb@130 | 469 | return bool() |
paulb@22 | 470 | elif isinstance(other, long): |
paulb@130 | 471 | return bool() |
paulb@22 | 472 | elif isinstance(other, float): |
paulb@130 | 473 | return bool() |
paulb@22 | 474 | else: |
paulb@55 | 475 | raise TypeError |
paulb@22 | 476 | |
paulb@22 | 477 | def __neg__(self): |
paulb@22 | 478 | return float() |
paulb@22 | 479 | |
paulb@22 | 480 | def __pos__(self): |
paulb@22 | 481 | return self |
paulb@22 | 482 | |
paulb@22 | 483 | def __str__(self): |
paulb@22 | 484 | return string() |
paulb@22 | 485 | |
paulb@130 | 486 | def __bool__(self): |
paulb@22 | 487 | return self != 0 |
paulb@22 | 488 | |
paulb@128 | 489 | class str: |
paulb@128 | 490 | def __init__(self, x=None): |
paulb@128 | 491 | x.__str__() |
paulb@128 | 492 | |
paulb@22 | 493 | def __add__(self, other): |
paulb@22 | 494 | if isinstance(other, string): |
paulb@22 | 495 | return string() |
paulb@22 | 496 | else: |
paulb@55 | 497 | raise TypeError |
paulb@22 | 498 | |
paulb@22 | 499 | def __radd__(self, other): |
paulb@22 | 500 | if isinstance(other, string): |
paulb@22 | 501 | return string() |
paulb@22 | 502 | else: |
paulb@55 | 503 | raise TypeError |
paulb@22 | 504 | |
paulb@22 | 505 | def __len__(self): |
paulb@22 | 506 | return int() |
paulb@22 | 507 | |
paulb@22 | 508 | def __str__(self): |
paulb@22 | 509 | return self |
paulb@22 | 510 | |
paulb@130 | 511 | def __bool__(self): |
paulb@22 | 512 | return self.__len__() != 0 |
paulb@22 | 513 | |
paulb@22 | 514 | def join(self, l): |
paulb@22 | 515 | total = 0 |
paulb@22 | 516 | first = 1 |
paulb@22 | 517 | self_len = self.__len__() |
paulb@22 | 518 | for i in l: |
paulb@22 | 519 | if not first: |
paulb@22 | 520 | total += self_len |
paulb@22 | 521 | total += len(str(i)) |
paulb@22 | 522 | first = 0 |
paulb@22 | 523 | b = buffer(total) |
paulb@22 | 524 | first = 1 |
paulb@22 | 525 | for i in l: |
paulb@22 | 526 | if not first: |
paulb@22 | 527 | b.append(self) |
paulb@22 | 528 | b.append(str(i)) |
paulb@22 | 529 | first = 0 |
paulb@22 | 530 | s = str(b) |
paulb@22 | 531 | return s |
paulb@22 | 532 | |
paulb@22 | 533 | class buffer: |
paulb@22 | 534 | def __init__(self, size): |
paulb@22 | 535 | if not isinstance(size, int): |
paulb@55 | 536 | raise TypeError |
paulb@22 | 537 | |
paulb@22 | 538 | def append(self, s): |
paulb@22 | 539 | if not isinstance(s, string): |
paulb@55 | 540 | raise TypeError |
paulb@22 | 541 | pass |
paulb@22 | 542 | |
paulb@22 | 543 | def __str__(self): |
paulb@22 | 544 | return string() |
paulb@22 | 545 | |
paulb@22 | 546 | class list: |
paulb@89 | 547 | def __init__(self, *args): |
paulb@22 | 548 | self.next = None |
paulb@22 | 549 | self.last = self |
paulb@89 | 550 | for arg in args: |
paulb@89 | 551 | self.append(arg) |
paulb@22 | 552 | |
paulb@22 | 553 | def __getitem__(self, index): |
paulb@22 | 554 | i = 0 |
paulb@22 | 555 | n = self |
paulb@22 | 556 | # NOTE: Support negative indices using last. |
paulb@22 | 557 | while i < index and n.next is not None: |
paulb@22 | 558 | n = n.next |
paulb@22 | 559 | i += 1 |
paulb@22 | 560 | if n.next is not None: |
paulb@22 | 561 | return n.value |
paulb@22 | 562 | else: |
paulb@22 | 563 | raise IndexError() # NOTE: Make this compliant with Python! |
paulb@22 | 564 | |
paulb@22 | 565 | def __setitem__(self, index, value): |
paulb@22 | 566 | i = 0 |
paulb@22 | 567 | n = self |
paulb@22 | 568 | # NOTE: Support negative indices using last. |
paulb@22 | 569 | while i < index and n.next is not None: |
paulb@22 | 570 | n = n.next |
paulb@22 | 571 | i += 1 |
paulb@22 | 572 | if n.next is not None: |
paulb@22 | 573 | n.value = value |
paulb@22 | 574 | else: |
paulb@22 | 575 | raise IndexError() # NOTE: Make this compliant with Python! |
paulb@22 | 576 | |
paulb@22 | 577 | def __getslice__(self, start, end=None): |
paulb@22 | 578 | slice = [] |
paulb@22 | 579 | i = 0 |
paulb@22 | 580 | n = self |
paulb@22 | 581 | # NOTE: Support negative indices using last. |
paulb@22 | 582 | while (end is None or i < end) and n.next is not None: |
paulb@22 | 583 | if i >= start: |
paulb@22 | 584 | slice.append(n.value) |
paulb@22 | 585 | n = n.next |
paulb@22 | 586 | i += 1 |
paulb@22 | 587 | return slice |
paulb@22 | 588 | |
paulb@22 | 589 | def __setslice__(self, start, end, slice): |
paulb@22 | 590 | i = 0 |
paulb@22 | 591 | n = self |
paulb@22 | 592 | j = 0 |
paulb@22 | 593 | p = slice |
paulb@22 | 594 | # NOTE: Support negative indices using last. |
paulb@22 | 595 | # NOTE: Support appending when start >= len(self). |
paulb@22 | 596 | while i < end and n is not None and p is not None: |
paulb@22 | 597 | if i >= start: |
paulb@22 | 598 | n.value = p.value |
paulb@22 | 599 | p = p.next |
paulb@22 | 600 | j += 1 |
paulb@22 | 601 | n = n.next |
paulb@22 | 602 | i += 1 |
paulb@22 | 603 | |
paulb@22 | 604 | def append(self, value): |
paulb@22 | 605 | n = self.last |
paulb@22 | 606 | n.value = value |
paulb@22 | 607 | n.next = self.__class__() |
paulb@22 | 608 | self.last = n.next |
paulb@22 | 609 | |
paulb@22 | 610 | def __len__(self): |
paulb@22 | 611 | i = 0 |
paulb@22 | 612 | n = self |
paulb@22 | 613 | while n.next is not None: |
paulb@22 | 614 | n = n.next |
paulb@22 | 615 | i += 1 |
paulb@22 | 616 | return i |
paulb@22 | 617 | |
paulb@22 | 618 | def __add__(self, other): |
paulb@22 | 619 | result = self.__class__() |
paulb@22 | 620 | for value in self: |
paulb@22 | 621 | result.append(value) |
paulb@22 | 622 | for value in other: |
paulb@22 | 623 | result.append(value) |
paulb@22 | 624 | return result |
paulb@22 | 625 | |
paulb@97 | 626 | def __iadd__(self, other): |
paulb@97 | 627 | for value in other: |
paulb@97 | 628 | self.append(value) |
paulb@97 | 629 | return self |
paulb@97 | 630 | |
paulb@22 | 631 | def __str__(self): |
paulb@22 | 632 | output = ["["] |
paulb@22 | 633 | n = self |
paulb@22 | 634 | first = 1 |
paulb@22 | 635 | while n.next is not None: |
paulb@22 | 636 | if not first: |
paulb@22 | 637 | output.append(", ") |
paulb@22 | 638 | else: |
paulb@22 | 639 | first = 0 |
paulb@22 | 640 | output.append(str(n.value)) |
paulb@22 | 641 | n = n.next |
paulb@22 | 642 | output.append("]") |
paulb@22 | 643 | return "".join(output) |
paulb@22 | 644 | |
paulb@22 | 645 | def __iter__(self): |
paulb@22 | 646 | return listiterator(self) |
paulb@22 | 647 | |
paulb@130 | 648 | def __bool__(self): |
paulb@22 | 649 | return self.__len__() != 0 |
paulb@22 | 650 | |
paulb@22 | 651 | class listiterator: |
paulb@22 | 652 | def __init__(self, l): |
paulb@22 | 653 | self.l = l |
paulb@22 | 654 | |
paulb@22 | 655 | def next(self): |
paulb@22 | 656 | l = self.l |
paulb@22 | 657 | next = l.next |
paulb@22 | 658 | if next is not None: |
paulb@22 | 659 | self.l = next |
paulb@22 | 660 | return l.value |
paulb@22 | 661 | else: |
paulb@55 | 662 | raise StopIteration |
paulb@22 | 663 | |
paulb@130 | 664 | def __bool__(self): |
paulb@130 | 665 | return bool() |
paulb@22 | 666 | |
paulb@22 | 667 | class tuple: |
paulb@66 | 668 | def __init__(self, *args): |
paulb@22 | 669 | self.next = None |
paulb@22 | 670 | self.last = self |
paulb@66 | 671 | for arg in args: |
paulb@66 | 672 | self.append(arg) |
paulb@22 | 673 | |
paulb@22 | 674 | def __getitem__(self, index): |
paulb@22 | 675 | i = 0 |
paulb@22 | 676 | n = self |
paulb@22 | 677 | # NOTE: Support negative indices using last. |
paulb@22 | 678 | while i < index and n.next is not None: |
paulb@22 | 679 | n = n.next |
paulb@22 | 680 | i += 1 |
paulb@22 | 681 | if n.next is not None: |
paulb@22 | 682 | return n.value |
paulb@22 | 683 | else: |
paulb@22 | 684 | raise IndexError() # NOTE: Make this compliant with Python! |
paulb@22 | 685 | |
paulb@22 | 686 | def __getslice__(self, start, end=None): |
paulb@22 | 687 | # NOTE: Should probably return a tuple. |
paulb@22 | 688 | slice = [] |
paulb@22 | 689 | i = 0 |
paulb@22 | 690 | n = self |
paulb@22 | 691 | # NOTE: Support negative indices using last. |
paulb@22 | 692 | while (end is None or i < end) and n.next is not None: |
paulb@22 | 693 | if i >= start: |
paulb@22 | 694 | slice.append(n.value) |
paulb@22 | 695 | n = n.next |
paulb@22 | 696 | i += 1 |
paulb@22 | 697 | return slice |
paulb@22 | 698 | |
paulb@22 | 699 | # NOTE: The append method should be internal at most. |
paulb@22 | 700 | |
paulb@22 | 701 | def append(self, value): |
paulb@22 | 702 | n = self.last |
paulb@22 | 703 | n.value = value |
paulb@22 | 704 | n.next = self.__class__() |
paulb@22 | 705 | self.last = n.next |
paulb@22 | 706 | |
paulb@22 | 707 | def __len__(self): |
paulb@22 | 708 | i = 0 |
paulb@22 | 709 | n = self |
paulb@22 | 710 | while n.next is not None: |
paulb@22 | 711 | n = n.next |
paulb@22 | 712 | i += 1 |
paulb@22 | 713 | return i |
paulb@22 | 714 | |
paulb@22 | 715 | def __add__(self, other): |
paulb@22 | 716 | result = self.__class__() |
paulb@22 | 717 | for value in self: |
paulb@22 | 718 | result.append(value) |
paulb@22 | 719 | for value in other: |
paulb@22 | 720 | result.append(value) |
paulb@22 | 721 | return result |
paulb@22 | 722 | |
paulb@22 | 723 | def __str__(self): |
paulb@22 | 724 | output = ["("] |
paulb@22 | 725 | n = self |
paulb@22 | 726 | first = 1 |
paulb@22 | 727 | while n.next is not None: |
paulb@22 | 728 | if not first: |
paulb@22 | 729 | output.append(", ") |
paulb@22 | 730 | else: |
paulb@22 | 731 | first = 0 |
paulb@22 | 732 | output.append(str(n.value)) |
paulb@22 | 733 | n = n.next |
paulb@22 | 734 | output.append(")") |
paulb@22 | 735 | return "".join(output) |
paulb@22 | 736 | |
paulb@22 | 737 | def __iter__(self): |
paulb@22 | 738 | return tupleiterator(self) |
paulb@22 | 739 | |
paulb@130 | 740 | def __bool__(self): |
paulb@22 | 741 | return self.__len__() != 0 |
paulb@22 | 742 | |
paulb@22 | 743 | class tupleiterator: |
paulb@22 | 744 | def __init__(self, l): |
paulb@22 | 745 | self.l = l |
paulb@22 | 746 | |
paulb@22 | 747 | def next(self): |
paulb@22 | 748 | l = self.l |
paulb@22 | 749 | next = l.next |
paulb@22 | 750 | if next is not None: |
paulb@22 | 751 | self.l = next |
paulb@22 | 752 | return l.value |
paulb@22 | 753 | else: |
paulb@55 | 754 | raise StopIteration |
paulb@22 | 755 | |
paulb@130 | 756 | def __bool__(self): |
paulb@130 | 757 | return bool() |
paulb@22 | 758 | |
paulb@54 | 759 | class dict: |
paulb@54 | 760 | pass |
paulb@54 | 761 | |
paulb@22 | 762 | class Exception: |
paulb@22 | 763 | pass |
paulb@22 | 764 | |
paulb@22 | 765 | class StopIteration(Exception): |
paulb@22 | 766 | pass |
paulb@22 | 767 | |
paulb@22 | 768 | class IndexError(Exception): |
paulb@22 | 769 | pass |
paulb@22 | 770 | |
paulb@22 | 771 | class AttributeError(Exception): |
paulb@22 | 772 | pass |
paulb@22 | 773 | |
paulb@22 | 774 | class TypeError(Exception): |
paulb@22 | 775 | pass |
paulb@22 | 776 | |
paulb@22 | 777 | class none: |
paulb@130 | 778 | def __bool__(self): |
paulb@22 | 779 | return False |
paulb@22 | 780 | |
paulb@22 | 781 | def __str__(self): |
paulb@22 | 782 | return "None" |
paulb@22 | 783 | |
paulb@22 | 784 | class undefined: |
paulb@22 | 785 | pass |
paulb@22 | 786 | |
paulb@105 | 787 | class xrange: |
paulb@105 | 788 | def __init__(self, start, end, step=1): |
paulb@105 | 789 | self.start = start |
paulb@105 | 790 | self.end = end |
paulb@105 | 791 | self.step = step |
paulb@105 | 792 | self.current = start |
paulb@105 | 793 | |
paulb@105 | 794 | def __iter__(self): |
paulb@105 | 795 | return self |
paulb@105 | 796 | |
paulb@105 | 797 | def next(self): |
paulb@105 | 798 | if self.current >= self.end: |
paulb@105 | 799 | raise StopIteration |
paulb@105 | 800 | current = self.current |
paulb@105 | 801 | self.current += self.step |
paulb@105 | 802 | return current |
paulb@105 | 803 | |
paulb@105 | 804 | # General functions. |
paulb@105 | 805 | |
paulb@22 | 806 | def isinstance(obj, cls): |
paulb@130 | 807 | return bool() |
paulb@22 | 808 | |
paulb@22 | 809 | def issubclass(cls1, cls2): |
paulb@130 | 810 | return bool() |
paulb@22 | 811 | |
paulb@22 | 812 | def len(x): |
paulb@22 | 813 | return x.__len__() |
paulb@22 | 814 | |
paulb@22 | 815 | def max(*l): |
paulb@22 | 816 | max_so_far = l[0] |
paulb@22 | 817 | for i in l[1:]: |
paulb@22 | 818 | if i > max_so_far: |
paulb@22 | 819 | max_so_far = i |
paulb@22 | 820 | return max_so_far |
paulb@22 | 821 | |
paulb@105 | 822 | def range(start, end, step=None): |
paulb@105 | 823 | if start == end: |
paulb@105 | 824 | return [] |
paulb@105 | 825 | elif start > end: |
paulb@105 | 826 | step = step or 1 |
paulb@105 | 827 | i = start |
paulb@105 | 828 | result = [] |
paulb@105 | 829 | while i < end: |
paulb@105 | 830 | result.append(i) |
paulb@105 | 831 | i += 1 |
paulb@105 | 832 | return result |
paulb@105 | 833 | else: |
paulb@105 | 834 | step = step or -1 |
paulb@105 | 835 | i = start |
paulb@105 | 836 | result = [] |
paulb@105 | 837 | while i > end: |
paulb@105 | 838 | result.append(i) |
paulb@105 | 839 | i -= 1 |
paulb@105 | 840 | return result |
paulb@105 | 841 | |
paulb@129 | 842 | # Special values. |
paulb@22 | 843 | |
paulb@130 | 844 | True = bool() |
paulb@130 | 845 | False = bool() |
paulb@22 | 846 | None = none() |
paulb@22 | 847 | Undefined = undefined() |
paulb@22 | 848 | |
paulb@22 | 849 | # Special functions. These all operate on references at run-time. |
paulb@22 | 850 | |
paulb@22 | 851 | def __is__(a, b): |
paulb@130 | 852 | return bool() |
paulb@22 | 853 | |
paulb@22 | 854 | def __is_not__(a, b): |
paulb@130 | 855 | return bool() |
paulb@22 | 856 | |
paulb@22 | 857 | def __not__(a): |
paulb@130 | 858 | return bool() |
paulb@22 | 859 | |
paulb@22 | 860 | # vim: tabstop=4 expandtab shiftwidth=4 |