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@201 | 6 | Copyright (C) 2005, 2006, 2007 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@145 | 30 | def __iadd__(self, other): |
paulb@145 | 31 | return self.__add__(other) |
paulb@145 | 32 | |
paulb@130 | 33 | class bool: |
paulb@212 | 34 | __atomic__ = object |
paulb@198 | 35 | |
paulb@130 | 36 | def __bool__(self): |
paulb@22 | 37 | return self |
paulb@22 | 38 | |
paulb@22 | 39 | def __str__(self): |
paulb@22 | 40 | if self: |
paulb@22 | 41 | return "True" |
paulb@22 | 42 | else: |
paulb@22 | 43 | return "False" |
paulb@22 | 44 | |
paulb@145 | 45 | class buffer: |
paulb@145 | 46 | def __init__(self, size): |
paulb@145 | 47 | if not isinstance(size, int): |
paulb@55 | 48 | raise TypeError |
paulb@22 | 49 | |
paulb@145 | 50 | def append(self, s): |
paulb@145 | 51 | if not isinstance(s, str): |
paulb@145 | 52 | raise TypeError |
paulb@145 | 53 | pass |
paulb@22 | 54 | |
paulb@22 | 55 | def __str__(self): |
paulb@145 | 56 | return str() |
paulb@22 | 57 | |
paulb@145 | 58 | class dict: |
paulb@167 | 59 | def __init__(self, *args): |
paulb@167 | 60 | for key, value in args: |
paulb@167 | 61 | self[key] = value |
paulb@167 | 62 | |
paulb@167 | 63 | def __setitem__(self, key, value): |
paulb@167 | 64 | self.key = key |
paulb@167 | 65 | self.value = value |
paulb@167 | 66 | |
paulb@167 | 67 | def __getitem__(self, key): |
paulb@167 | 68 | return self.value |
paulb@22 | 69 | |
paulb@145 | 70 | class file: |
paulb@145 | 71 | def write(self, s): |
paulb@145 | 72 | pass |
paulb@22 | 73 | |
paulb@22 | 74 | class float: |
paulb@212 | 75 | __atomic__ = object |
paulb@198 | 76 | |
paulb@200 | 77 | def __init__(self, number_or_string=None): |
paulb@200 | 78 | pass |
paulb@200 | 79 | |
paulb@22 | 80 | def __iadd__(self, other): |
paulb@22 | 81 | if isinstance(other, int): |
paulb@22 | 82 | return float() |
paulb@22 | 83 | elif isinstance(other, long): |
paulb@22 | 84 | return float() |
paulb@22 | 85 | elif isinstance(other, float): |
paulb@22 | 86 | return float() |
paulb@22 | 87 | else: |
paulb@55 | 88 | raise TypeError |
paulb@22 | 89 | |
paulb@22 | 90 | def __isub__(self, other): |
paulb@22 | 91 | if isinstance(other, int): |
paulb@22 | 92 | return float() |
paulb@22 | 93 | elif isinstance(other, long): |
paulb@22 | 94 | return float() |
paulb@22 | 95 | elif isinstance(other, float): |
paulb@22 | 96 | return float() |
paulb@22 | 97 | else: |
paulb@55 | 98 | raise TypeError |
paulb@22 | 99 | |
paulb@22 | 100 | def __add__(self, other): |
paulb@22 | 101 | if isinstance(other, int): |
paulb@22 | 102 | return float() |
paulb@22 | 103 | elif isinstance(other, long): |
paulb@22 | 104 | return float() |
paulb@22 | 105 | elif isinstance(other, float): |
paulb@22 | 106 | return float() |
paulb@22 | 107 | else: |
paulb@55 | 108 | raise TypeError |
paulb@22 | 109 | |
paulb@22 | 110 | def __radd__(self, other): |
paulb@22 | 111 | if isinstance(other, int): |
paulb@22 | 112 | return float() |
paulb@22 | 113 | elif isinstance(other, long): |
paulb@22 | 114 | return float() |
paulb@22 | 115 | elif isinstance(other, float): |
paulb@22 | 116 | return float() |
paulb@22 | 117 | else: |
paulb@55 | 118 | raise TypeError |
paulb@22 | 119 | |
paulb@22 | 120 | def __sub__(self, other): |
paulb@22 | 121 | if isinstance(other, int): |
paulb@22 | 122 | return float() |
paulb@22 | 123 | elif isinstance(other, long): |
paulb@22 | 124 | return float() |
paulb@22 | 125 | elif isinstance(other, float): |
paulb@22 | 126 | return float() |
paulb@22 | 127 | else: |
paulb@55 | 128 | raise TypeError |
paulb@22 | 129 | |
paulb@22 | 130 | def __rsub__(self, other): |
paulb@22 | 131 | if isinstance(other, int): |
paulb@22 | 132 | return float() |
paulb@22 | 133 | elif isinstance(other, long): |
paulb@22 | 134 | return float() |
paulb@22 | 135 | elif isinstance(other, float): |
paulb@22 | 136 | return float() |
paulb@22 | 137 | else: |
paulb@55 | 138 | raise TypeError |
paulb@22 | 139 | |
paulb@22 | 140 | def __mul__(self, other): |
paulb@22 | 141 | if isinstance(other, int): |
paulb@22 | 142 | return float() |
paulb@22 | 143 | elif isinstance(other, long): |
paulb@22 | 144 | return float() |
paulb@22 | 145 | elif isinstance(other, float): |
paulb@22 | 146 | return float() |
paulb@22 | 147 | else: |
paulb@55 | 148 | raise TypeError |
paulb@22 | 149 | |
paulb@22 | 150 | def __rmul__(self, other): |
paulb@22 | 151 | if isinstance(other, int): |
paulb@22 | 152 | return float() |
paulb@22 | 153 | elif isinstance(other, long): |
paulb@22 | 154 | return float() |
paulb@22 | 155 | elif isinstance(other, float): |
paulb@22 | 156 | return float() |
paulb@22 | 157 | else: |
paulb@55 | 158 | raise TypeError |
paulb@22 | 159 | |
paulb@22 | 160 | def __div__(self, other): |
paulb@22 | 161 | if isinstance(other, int): |
paulb@22 | 162 | return float() |
paulb@22 | 163 | elif isinstance(other, long): |
paulb@22 | 164 | return float() |
paulb@22 | 165 | elif isinstance(other, float): |
paulb@22 | 166 | return float() |
paulb@22 | 167 | else: |
paulb@55 | 168 | raise TypeError |
paulb@22 | 169 | |
paulb@22 | 170 | def __rdiv__(self, other): |
paulb@22 | 171 | if isinstance(other, int): |
paulb@22 | 172 | return float() |
paulb@22 | 173 | elif isinstance(other, long): |
paulb@22 | 174 | return float() |
paulb@22 | 175 | elif isinstance(other, float): |
paulb@22 | 176 | return float() |
paulb@22 | 177 | else: |
paulb@55 | 178 | raise TypeError |
paulb@22 | 179 | |
paulb@136 | 180 | def __floordiv__(self, other): |
paulb@136 | 181 | if isinstance(other, int): |
paulb@136 | 182 | return float() |
paulb@136 | 183 | elif isinstance(other, long): |
paulb@136 | 184 | return float() |
paulb@136 | 185 | elif isinstance(other, float): |
paulb@136 | 186 | return float() |
paulb@136 | 187 | else: |
paulb@136 | 188 | raise TypeError |
paulb@136 | 189 | |
paulb@136 | 190 | def __rfloordiv__(self, other): |
paulb@136 | 191 | if isinstance(other, int): |
paulb@136 | 192 | return float() |
paulb@136 | 193 | elif isinstance(other, long): |
paulb@136 | 194 | return float() |
paulb@136 | 195 | elif isinstance(other, float): |
paulb@136 | 196 | return float() |
paulb@136 | 197 | else: |
paulb@136 | 198 | raise TypeError |
paulb@136 | 199 | |
paulb@238 | 200 | def __mod__(self, other): |
paulb@238 | 201 | if isinstance(other, int): |
paulb@238 | 202 | return float() |
paulb@238 | 203 | elif isinstance(other, long): |
paulb@238 | 204 | return float() |
paulb@238 | 205 | elif isinstance(other, float): |
paulb@238 | 206 | return float() |
paulb@238 | 207 | else: |
paulb@238 | 208 | raise TypeError |
paulb@238 | 209 | |
paulb@22 | 210 | def __pow__(self, other): |
paulb@22 | 211 | if isinstance(other, int): |
paulb@22 | 212 | return float() |
paulb@22 | 213 | elif isinstance(other, long): |
paulb@22 | 214 | return float() |
paulb@22 | 215 | elif isinstance(other, float): |
paulb@22 | 216 | return float() |
paulb@22 | 217 | else: |
paulb@55 | 218 | raise TypeError |
paulb@22 | 219 | |
paulb@22 | 220 | def __rpow__(self, other): |
paulb@22 | 221 | if isinstance(other, int): |
paulb@22 | 222 | return float() |
paulb@22 | 223 | elif isinstance(other, long): |
paulb@22 | 224 | return float() |
paulb@22 | 225 | elif isinstance(other, float): |
paulb@22 | 226 | return float() |
paulb@22 | 227 | else: |
paulb@55 | 228 | raise TypeError |
paulb@22 | 229 | |
paulb@22 | 230 | def __lt__(self, other): |
paulb@22 | 231 | if isinstance(other, int): |
paulb@130 | 232 | return bool() |
paulb@22 | 233 | elif isinstance(other, long): |
paulb@130 | 234 | return bool() |
paulb@22 | 235 | elif isinstance(other, float): |
paulb@130 | 236 | return bool() |
paulb@22 | 237 | else: |
paulb@186 | 238 | return NotImplemented |
paulb@22 | 239 | |
paulb@22 | 240 | def __gt__(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 | elif isinstance(other, float): |
paulb@130 | 246 | return bool() |
paulb@22 | 247 | else: |
paulb@186 | 248 | return NotImplemented |
paulb@22 | 249 | |
paulb@22 | 250 | def __le__(self, other): |
paulb@22 | 251 | if isinstance(other, int): |
paulb@130 | 252 | return bool() |
paulb@22 | 253 | elif isinstance(other, long): |
paulb@130 | 254 | return bool() |
paulb@22 | 255 | elif isinstance(other, float): |
paulb@130 | 256 | return bool() |
paulb@22 | 257 | else: |
paulb@186 | 258 | return NotImplemented |
paulb@22 | 259 | |
paulb@22 | 260 | def __ge__(self, other): |
paulb@22 | 261 | if isinstance(other, int): |
paulb@130 | 262 | return bool() |
paulb@22 | 263 | elif isinstance(other, long): |
paulb@130 | 264 | return bool() |
paulb@22 | 265 | elif isinstance(other, float): |
paulb@130 | 266 | return bool() |
paulb@22 | 267 | else: |
paulb@186 | 268 | return NotImplemented |
paulb@22 | 269 | |
paulb@22 | 270 | def __eq__(self, other): |
paulb@22 | 271 | if isinstance(other, int): |
paulb@130 | 272 | return bool() |
paulb@22 | 273 | elif isinstance(other, long): |
paulb@130 | 274 | return bool() |
paulb@22 | 275 | elif isinstance(other, float): |
paulb@130 | 276 | return bool() |
paulb@22 | 277 | else: |
paulb@186 | 278 | return NotImplemented |
paulb@22 | 279 | |
paulb@22 | 280 | def __ne__(self, other): |
paulb@22 | 281 | if isinstance(other, int): |
paulb@130 | 282 | return bool() |
paulb@22 | 283 | elif isinstance(other, long): |
paulb@130 | 284 | return bool() |
paulb@22 | 285 | elif isinstance(other, float): |
paulb@130 | 286 | return bool() |
paulb@22 | 287 | else: |
paulb@186 | 288 | return NotImplemented |
paulb@22 | 289 | |
paulb@22 | 290 | def __neg__(self): |
paulb@22 | 291 | return float() |
paulb@22 | 292 | |
paulb@22 | 293 | def __pos__(self): |
paulb@22 | 294 | return self |
paulb@22 | 295 | |
paulb@22 | 296 | def __str__(self): |
paulb@145 | 297 | return str() |
paulb@22 | 298 | |
paulb@130 | 299 | def __bool__(self): |
paulb@22 | 300 | return self != 0 |
paulb@22 | 301 | |
paulb@145 | 302 | class int: |
paulb@212 | 303 | __atomic__ = object |
paulb@198 | 304 | |
paulb@200 | 305 | def __init__(self, number_or_string=None): |
paulb@200 | 306 | pass |
paulb@200 | 307 | |
paulb@145 | 308 | def __iadd__(self, other): |
paulb@145 | 309 | if isinstance(other, int): |
paulb@145 | 310 | return int() |
paulb@145 | 311 | else: |
paulb@145 | 312 | raise TypeError |
paulb@145 | 313 | |
paulb@145 | 314 | def __isub__(self, other): |
paulb@145 | 315 | if isinstance(other, int): |
paulb@145 | 316 | return int() |
paulb@145 | 317 | else: |
paulb@145 | 318 | raise TypeError |
paulb@128 | 319 | |
paulb@22 | 320 | def __add__(self, other): |
paulb@145 | 321 | if isinstance(other, int): |
paulb@145 | 322 | return int() |
paulb@22 | 323 | else: |
paulb@55 | 324 | raise TypeError |
paulb@22 | 325 | |
paulb@22 | 326 | def __radd__(self, other): |
paulb@145 | 327 | if isinstance(other, int): |
paulb@145 | 328 | return int() |
paulb@145 | 329 | else: |
paulb@145 | 330 | raise TypeError |
paulb@145 | 331 | |
paulb@145 | 332 | def __sub__(self, other): |
paulb@145 | 333 | if isinstance(other, int): |
paulb@145 | 334 | return int() |
paulb@145 | 335 | else: |
paulb@145 | 336 | raise TypeError |
paulb@145 | 337 | |
paulb@145 | 338 | def __rsub__(self, other): |
paulb@145 | 339 | if isinstance(other, int): |
paulb@145 | 340 | return int() |
paulb@145 | 341 | else: |
paulb@145 | 342 | raise TypeError |
paulb@145 | 343 | |
paulb@145 | 344 | def __mul__(self, other): |
paulb@145 | 345 | if isinstance(other, int): |
paulb@145 | 346 | return int() |
paulb@145 | 347 | else: |
paulb@145 | 348 | raise TypeError |
paulb@145 | 349 | |
paulb@145 | 350 | def __rmul__(self, other): |
paulb@145 | 351 | if isinstance(other, int): |
paulb@145 | 352 | return int() |
paulb@145 | 353 | else: |
paulb@145 | 354 | raise TypeError |
paulb@145 | 355 | |
paulb@145 | 356 | def __div__(self, other): |
paulb@145 | 357 | if isinstance(other, int): |
paulb@145 | 358 | return int() |
paulb@145 | 359 | else: |
paulb@145 | 360 | raise TypeError |
paulb@145 | 361 | |
paulb@145 | 362 | def __rdiv__(self, other): |
paulb@145 | 363 | if isinstance(other, int): |
paulb@145 | 364 | return int() |
paulb@145 | 365 | else: |
paulb@145 | 366 | raise TypeError |
paulb@145 | 367 | |
paulb@145 | 368 | def __floordiv__(self, other): |
paulb@145 | 369 | if isinstance(other, int): |
paulb@145 | 370 | return int() |
paulb@145 | 371 | else: |
paulb@145 | 372 | raise TypeError |
paulb@145 | 373 | |
paulb@145 | 374 | def __rfloordiv__(self, other): |
paulb@145 | 375 | if isinstance(other, int): |
paulb@145 | 376 | return int() |
paulb@22 | 377 | else: |
paulb@55 | 378 | raise TypeError |
paulb@22 | 379 | |
paulb@238 | 380 | def __mod__(self, other): |
paulb@238 | 381 | if isinstance(other, int): |
paulb@238 | 382 | return int() |
paulb@238 | 383 | else: |
paulb@238 | 384 | raise TypeError |
paulb@238 | 385 | |
paulb@145 | 386 | def __pow__(self, other): |
paulb@145 | 387 | if isinstance(other, int): |
paulb@145 | 388 | return int() |
paulb@145 | 389 | else: |
paulb@145 | 390 | raise TypeError |
paulb@145 | 391 | |
paulb@203 | 392 | def __and__(self, other): |
paulb@203 | 393 | if isinstance(other, int): |
paulb@203 | 394 | return int() |
paulb@203 | 395 | else: |
paulb@203 | 396 | raise TypeError |
paulb@203 | 397 | |
paulb@203 | 398 | def __rand__(self, other): |
paulb@203 | 399 | if isinstance(other, int): |
paulb@203 | 400 | return int() |
paulb@203 | 401 | else: |
paulb@203 | 402 | raise TypeError |
paulb@203 | 403 | |
paulb@203 | 404 | def __or__(self, other): |
paulb@203 | 405 | if isinstance(other, int): |
paulb@203 | 406 | return int() |
paulb@203 | 407 | else: |
paulb@203 | 408 | raise TypeError |
paulb@203 | 409 | |
paulb@203 | 410 | def __ror__(self, other): |
paulb@203 | 411 | if isinstance(other, int): |
paulb@203 | 412 | return int() |
paulb@203 | 413 | else: |
paulb@203 | 414 | raise TypeError |
paulb@203 | 415 | |
paulb@203 | 416 | def __xor__(self, other): |
paulb@203 | 417 | if isinstance(other, int): |
paulb@203 | 418 | return int() |
paulb@203 | 419 | else: |
paulb@203 | 420 | raise TypeError |
paulb@203 | 421 | |
paulb@203 | 422 | def __rxor__(self, other): |
paulb@203 | 423 | if isinstance(other, int): |
paulb@203 | 424 | return int() |
paulb@203 | 425 | else: |
paulb@203 | 426 | raise TypeError |
paulb@203 | 427 | |
paulb@145 | 428 | def __lt__(self, other): |
paulb@145 | 429 | if isinstance(other, int): |
paulb@145 | 430 | return bool() |
paulb@145 | 431 | else: |
paulb@186 | 432 | return NotImplemented |
paulb@145 | 433 | |
paulb@145 | 434 | def __gt__(self, other): |
paulb@145 | 435 | if isinstance(other, int): |
paulb@145 | 436 | return bool() |
paulb@145 | 437 | else: |
paulb@186 | 438 | return NotImplemented |
paulb@145 | 439 | |
paulb@145 | 440 | def __le__(self, other): |
paulb@145 | 441 | if isinstance(other, int): |
paulb@145 | 442 | return bool() |
paulb@145 | 443 | else: |
paulb@186 | 444 | return NotImplemented |
paulb@145 | 445 | |
paulb@145 | 446 | def __ge__(self, other): |
paulb@145 | 447 | if isinstance(other, int): |
paulb@145 | 448 | return bool() |
paulb@145 | 449 | else: |
paulb@186 | 450 | return NotImplemented |
paulb@145 | 451 | |
paulb@145 | 452 | def __eq__(self, other): |
paulb@145 | 453 | if isinstance(other, int): |
paulb@145 | 454 | return bool() |
paulb@145 | 455 | else: |
paulb@186 | 456 | return NotImplemented |
paulb@145 | 457 | |
paulb@145 | 458 | def __ne__(self, other): |
paulb@145 | 459 | if isinstance(other, int): |
paulb@145 | 460 | return bool() |
paulb@145 | 461 | else: |
paulb@186 | 462 | return NotImplemented |
paulb@145 | 463 | |
paulb@145 | 464 | def __neg__(self): |
paulb@22 | 465 | return int() |
paulb@22 | 466 | |
paulb@145 | 467 | def __pos__(self): |
paulb@145 | 468 | return self |
paulb@145 | 469 | |
paulb@22 | 470 | def __str__(self): |
paulb@145 | 471 | return str() |
paulb@22 | 472 | |
paulb@130 | 473 | def __bool__(self): |
paulb@145 | 474 | return self != 0 |
paulb@22 | 475 | |
paulb@22 | 476 | class list: |
paulb@251 | 477 | def __init__(self, args=()): |
paulb@251 | 478 | for arg in args: |
paulb@251 | 479 | self.append(arg) |
paulb@22 | 480 | |
paulb@22 | 481 | def __getitem__(self, index): |
paulb@167 | 482 | if -len(self) <= index < len(self): |
paulb@167 | 483 | return self.value |
paulb@22 | 484 | else: |
paulb@167 | 485 | raise IndexError, index |
paulb@22 | 486 | |
paulb@22 | 487 | def __setitem__(self, index, value): |
paulb@167 | 488 | if -len(self) <= index < len(self): |
paulb@167 | 489 | self.value = value |
paulb@22 | 490 | else: |
paulb@167 | 491 | raise IndexError, index |
paulb@22 | 492 | |
paulb@22 | 493 | def __getslice__(self, start, end=None): |
paulb@167 | 494 | if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end: |
paulb@228 | 495 | return [self.value] |
paulb@167 | 496 | else: |
paulb@228 | 497 | return [] |
paulb@22 | 498 | |
paulb@22 | 499 | def __setslice__(self, start, end, slice): |
paulb@228 | 500 | #if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end: |
paulb@228 | 501 | for value in slice: |
paulb@228 | 502 | self.value = value |
paulb@22 | 503 | |
paulb@22 | 504 | def append(self, value): |
paulb@167 | 505 | self.value = value |
paulb@22 | 506 | |
paulb@22 | 507 | def __len__(self): |
paulb@167 | 508 | return int() |
paulb@22 | 509 | |
paulb@22 | 510 | def __add__(self, other): |
paulb@22 | 511 | for value in other: |
paulb@167 | 512 | self.value = value |
paulb@167 | 513 | return self |
paulb@22 | 514 | |
paulb@167 | 515 | __iadd__ = __add__ |
paulb@97 | 516 | |
paulb@22 | 517 | def __str__(self): |
paulb@167 | 518 | return str() |
paulb@22 | 519 | |
paulb@22 | 520 | def __iter__(self): |
paulb@22 | 521 | return listiterator(self) |
paulb@22 | 522 | |
paulb@130 | 523 | def __bool__(self): |
paulb@22 | 524 | return self.__len__() != 0 |
paulb@22 | 525 | |
paulb@22 | 526 | class listiterator: |
paulb@22 | 527 | def __init__(self, l): |
paulb@22 | 528 | self.l = l |
paulb@167 | 529 | self.index = 0 |
paulb@22 | 530 | |
paulb@22 | 531 | def next(self): |
paulb@167 | 532 | if self.index < len(self.l): |
paulb@167 | 533 | self.index += 1 |
paulb@167 | 534 | return self.l[self.index] |
paulb@22 | 535 | else: |
paulb@55 | 536 | raise StopIteration |
paulb@22 | 537 | |
paulb@130 | 538 | def __bool__(self): |
paulb@130 | 539 | return bool() |
paulb@22 | 540 | |
paulb@145 | 541 | class long: |
paulb@212 | 542 | __atomic__ = object |
paulb@198 | 543 | |
paulb@200 | 544 | def __init__(self, number_or_string=None): |
paulb@200 | 545 | pass |
paulb@200 | 546 | |
paulb@145 | 547 | def __iadd__(self, other): |
paulb@145 | 548 | if isinstance(other, int): |
paulb@145 | 549 | return long() |
paulb@145 | 550 | elif isinstance(other, long): |
paulb@145 | 551 | return long() |
paulb@145 | 552 | else: |
paulb@145 | 553 | raise TypeError |
paulb@145 | 554 | |
paulb@145 | 555 | def __isub__(self, other): |
paulb@145 | 556 | if isinstance(other, int): |
paulb@145 | 557 | return long() |
paulb@145 | 558 | elif isinstance(other, long): |
paulb@145 | 559 | return long() |
paulb@145 | 560 | else: |
paulb@145 | 561 | raise TypeError |
paulb@145 | 562 | |
paulb@145 | 563 | def __add__(self, other): |
paulb@145 | 564 | if isinstance(other, int): |
paulb@145 | 565 | return long() |
paulb@145 | 566 | elif isinstance(other, long): |
paulb@145 | 567 | return long() |
paulb@145 | 568 | else: |
paulb@145 | 569 | raise TypeError |
paulb@145 | 570 | |
paulb@145 | 571 | def __radd__(self, other): |
paulb@145 | 572 | if isinstance(other, int): |
paulb@145 | 573 | return long() |
paulb@145 | 574 | elif isinstance(other, long): |
paulb@145 | 575 | return long() |
paulb@145 | 576 | else: |
paulb@145 | 577 | raise TypeError |
paulb@145 | 578 | |
paulb@145 | 579 | def __sub__(self, other): |
paulb@145 | 580 | if isinstance(other, int): |
paulb@145 | 581 | return long() |
paulb@145 | 582 | elif isinstance(other, long): |
paulb@145 | 583 | return long() |
paulb@145 | 584 | else: |
paulb@145 | 585 | raise TypeError |
paulb@145 | 586 | |
paulb@145 | 587 | def __rsub__(self, other): |
paulb@145 | 588 | if isinstance(other, int): |
paulb@145 | 589 | return long() |
paulb@145 | 590 | elif isinstance(other, long): |
paulb@145 | 591 | return long() |
paulb@145 | 592 | else: |
paulb@145 | 593 | raise TypeError |
paulb@145 | 594 | |
paulb@238 | 595 | def __mul__(self, other): |
paulb@238 | 596 | if isinstance(other, int): |
paulb@238 | 597 | return long() |
paulb@238 | 598 | elif isinstance(other, long): |
paulb@238 | 599 | return long() |
paulb@238 | 600 | else: |
paulb@238 | 601 | raise TypeError |
paulb@238 | 602 | |
paulb@238 | 603 | def __rmul__(self, other): |
paulb@238 | 604 | if isinstance(other, int): |
paulb@238 | 605 | return long() |
paulb@238 | 606 | elif isinstance(other, long): |
paulb@238 | 607 | return long() |
paulb@238 | 608 | else: |
paulb@238 | 609 | raise TypeError |
paulb@238 | 610 | |
paulb@238 | 611 | def __div__(self, other): |
paulb@238 | 612 | if isinstance(other, int): |
paulb@238 | 613 | return long() |
paulb@238 | 614 | elif isinstance(other, long): |
paulb@238 | 615 | return long() |
paulb@238 | 616 | else: |
paulb@238 | 617 | raise TypeError |
paulb@238 | 618 | |
paulb@238 | 619 | def __rdiv__(self, other): |
paulb@238 | 620 | if isinstance(other, int): |
paulb@238 | 621 | return long() |
paulb@238 | 622 | elif isinstance(other, long): |
paulb@238 | 623 | return long() |
paulb@238 | 624 | else: |
paulb@238 | 625 | raise TypeError |
paulb@238 | 626 | |
paulb@238 | 627 | def __floordiv__(self, other): |
paulb@238 | 628 | if isinstance(other, int): |
paulb@238 | 629 | return long() |
paulb@238 | 630 | elif isinstance(other, long): |
paulb@238 | 631 | return long() |
paulb@238 | 632 | else: |
paulb@238 | 633 | raise TypeError |
paulb@238 | 634 | |
paulb@238 | 635 | def __rfloordiv__(self, other): |
paulb@238 | 636 | if isinstance(other, int): |
paulb@238 | 637 | return long() |
paulb@238 | 638 | elif isinstance(other, long): |
paulb@238 | 639 | return long() |
paulb@238 | 640 | else: |
paulb@238 | 641 | raise TypeError |
paulb@238 | 642 | |
paulb@203 | 643 | def __and__(self, other): |
paulb@203 | 644 | if isinstance(other, int): |
paulb@203 | 645 | return long() |
paulb@203 | 646 | elif isinstance(other, long): |
paulb@203 | 647 | return long() |
paulb@203 | 648 | else: |
paulb@203 | 649 | raise TypeError |
paulb@203 | 650 | |
paulb@203 | 651 | def __rand__(self, other): |
paulb@203 | 652 | if isinstance(other, int): |
paulb@203 | 653 | return long() |
paulb@203 | 654 | elif isinstance(other, long): |
paulb@203 | 655 | return long() |
paulb@203 | 656 | else: |
paulb@203 | 657 | raise TypeError |
paulb@203 | 658 | |
paulb@203 | 659 | def __or__(self, other): |
paulb@203 | 660 | if isinstance(other, int): |
paulb@203 | 661 | return long() |
paulb@203 | 662 | elif isinstance(other, long): |
paulb@203 | 663 | return long() |
paulb@203 | 664 | else: |
paulb@203 | 665 | raise TypeError |
paulb@203 | 666 | |
paulb@203 | 667 | def __ror__(self, other): |
paulb@203 | 668 | if isinstance(other, int): |
paulb@203 | 669 | return long() |
paulb@203 | 670 | elif isinstance(other, long): |
paulb@203 | 671 | return long() |
paulb@203 | 672 | else: |
paulb@203 | 673 | raise TypeError |
paulb@203 | 674 | |
paulb@203 | 675 | def __xor__(self, other): |
paulb@203 | 676 | if isinstance(other, int): |
paulb@203 | 677 | return long() |
paulb@203 | 678 | elif isinstance(other, long): |
paulb@203 | 679 | return long() |
paulb@203 | 680 | else: |
paulb@203 | 681 | raise TypeError |
paulb@203 | 682 | |
paulb@203 | 683 | def __rxor__(self, other): |
paulb@203 | 684 | if isinstance(other, int): |
paulb@203 | 685 | return long() |
paulb@203 | 686 | elif isinstance(other, long): |
paulb@203 | 687 | return long() |
paulb@203 | 688 | else: |
paulb@203 | 689 | raise TypeError |
paulb@203 | 690 | |
paulb@145 | 691 | def __lt__(self, other): |
paulb@145 | 692 | if isinstance(other, int): |
paulb@145 | 693 | return bool() |
paulb@145 | 694 | elif isinstance(other, long): |
paulb@145 | 695 | return bool() |
paulb@145 | 696 | else: |
paulb@186 | 697 | return NotImplemented |
paulb@145 | 698 | |
paulb@145 | 699 | def __gt__(self, other): |
paulb@145 | 700 | if isinstance(other, int): |
paulb@145 | 701 | return bool() |
paulb@145 | 702 | elif isinstance(other, long): |
paulb@145 | 703 | return bool() |
paulb@145 | 704 | else: |
paulb@186 | 705 | return NotImplemented |
paulb@145 | 706 | |
paulb@145 | 707 | def __le__(self, other): |
paulb@145 | 708 | if isinstance(other, int): |
paulb@145 | 709 | return bool() |
paulb@145 | 710 | elif isinstance(other, long): |
paulb@145 | 711 | return bool() |
paulb@145 | 712 | else: |
paulb@186 | 713 | return NotImplemented |
paulb@145 | 714 | |
paulb@145 | 715 | def __ge__(self, other): |
paulb@145 | 716 | if isinstance(other, int): |
paulb@145 | 717 | return bool() |
paulb@145 | 718 | elif isinstance(other, long): |
paulb@145 | 719 | return bool() |
paulb@145 | 720 | else: |
paulb@186 | 721 | return NotImplemented |
paulb@145 | 722 | |
paulb@145 | 723 | def __eq__(self, other): |
paulb@145 | 724 | if isinstance(other, int): |
paulb@145 | 725 | return bool() |
paulb@145 | 726 | elif isinstance(other, long): |
paulb@145 | 727 | return bool() |
paulb@145 | 728 | else: |
paulb@186 | 729 | return NotImplemented |
paulb@145 | 730 | |
paulb@145 | 731 | def __ne__(self, other): |
paulb@145 | 732 | if isinstance(other, int): |
paulb@145 | 733 | return bool() |
paulb@145 | 734 | elif isinstance(other, long): |
paulb@145 | 735 | return bool() |
paulb@145 | 736 | else: |
paulb@186 | 737 | return NotImplemented |
paulb@145 | 738 | |
paulb@145 | 739 | def __neg__(self): |
paulb@145 | 740 | return long() |
paulb@145 | 741 | |
paulb@145 | 742 | def __pos__(self): |
paulb@145 | 743 | return self |
paulb@145 | 744 | |
paulb@145 | 745 | def __str__(self): |
paulb@145 | 746 | return str() |
paulb@145 | 747 | |
paulb@145 | 748 | def __bool__(self): |
paulb@145 | 749 | return self != 0 |
paulb@145 | 750 | |
paulb@145 | 751 | class none: |
paulb@212 | 752 | __atomic__ = object |
paulb@198 | 753 | |
paulb@145 | 754 | def __bool__(self): |
paulb@145 | 755 | return False |
paulb@145 | 756 | |
paulb@145 | 757 | def __str__(self): |
paulb@145 | 758 | return "None" |
paulb@145 | 759 | |
paulb@243 | 760 | NoneType = none |
paulb@243 | 761 | |
paulb@201 | 762 | class slice: |
paulb@201 | 763 | def __init__(self, start_or_end, end=None, step=None): |
paulb@201 | 764 | if end is None: |
paulb@201 | 765 | self.start = 0 |
paulb@201 | 766 | self.end = start_or_end |
paulb@201 | 767 | else: |
paulb@201 | 768 | self.start = start_or_end |
paulb@201 | 769 | self.end = end |
paulb@201 | 770 | self.step = step |
paulb@201 | 771 | |
paulb@145 | 772 | class str: |
paulb@212 | 773 | __atomic__ = object |
paulb@198 | 774 | |
paulb@145 | 775 | def __init__(self, x=None): |
paulb@145 | 776 | x.__str__() |
paulb@145 | 777 | |
paulb@251 | 778 | def __getitem__(self, index): |
paulb@251 | 779 | if -len(self) <= index < len(self): |
paulb@251 | 780 | return str() |
paulb@251 | 781 | else: |
paulb@251 | 782 | raise IndexError, index |
paulb@251 | 783 | |
paulb@251 | 784 | def __getslice__(self, start, end=None): |
paulb@251 | 785 | return str() |
paulb@251 | 786 | |
paulb@238 | 787 | def __iadd__(self, other): |
paulb@238 | 788 | if isinstance(other, str): |
paulb@238 | 789 | return str() |
paulb@238 | 790 | else: |
paulb@238 | 791 | raise TypeError |
paulb@238 | 792 | |
paulb@145 | 793 | def __add__(self, other): |
paulb@145 | 794 | if isinstance(other, str): |
paulb@145 | 795 | return str() |
paulb@145 | 796 | else: |
paulb@145 | 797 | raise TypeError |
paulb@145 | 798 | |
paulb@145 | 799 | def __radd__(self, other): |
paulb@145 | 800 | if isinstance(other, str): |
paulb@145 | 801 | return str() |
paulb@145 | 802 | else: |
paulb@145 | 803 | raise TypeError |
paulb@145 | 804 | |
paulb@238 | 805 | def __mul__(self, other): |
paulb@238 | 806 | if isinstance(other, int) or isinstance(other, long): |
paulb@238 | 807 | return str() |
paulb@238 | 808 | else: |
paulb@238 | 809 | raise TypeError |
paulb@238 | 810 | |
paulb@238 | 811 | def __radd__(self, other): |
paulb@238 | 812 | if isinstance(other, int) or isinstance(other, long): |
paulb@238 | 813 | return str() |
paulb@238 | 814 | else: |
paulb@238 | 815 | raise TypeError |
paulb@238 | 816 | |
paulb@200 | 817 | def __mod__(self, other): |
paulb@200 | 818 | |
paulb@200 | 819 | "The format operator for strings." |
paulb@200 | 820 | |
paulb@200 | 821 | return str() |
paulb@200 | 822 | |
paulb@206 | 823 | def __lt__(self, other): |
paulb@206 | 824 | if isinstance(other, str): |
paulb@206 | 825 | return bool() |
paulb@206 | 826 | else: |
paulb@206 | 827 | return NotImplemented |
paulb@206 | 828 | |
paulb@206 | 829 | def __gt__(self, other): |
paulb@206 | 830 | if isinstance(other, str): |
paulb@206 | 831 | return bool() |
paulb@206 | 832 | else: |
paulb@206 | 833 | return NotImplemented |
paulb@206 | 834 | |
paulb@206 | 835 | def __le__(self, other): |
paulb@206 | 836 | if isinstance(other, str): |
paulb@206 | 837 | return bool() |
paulb@206 | 838 | else: |
paulb@206 | 839 | return NotImplemented |
paulb@206 | 840 | |
paulb@206 | 841 | def __ge__(self, other): |
paulb@206 | 842 | if isinstance(other, str): |
paulb@206 | 843 | return bool() |
paulb@206 | 844 | else: |
paulb@206 | 845 | return NotImplemented |
paulb@206 | 846 | |
paulb@206 | 847 | def __eq__(self, other): |
paulb@206 | 848 | if isinstance(other, str): |
paulb@206 | 849 | return bool() |
paulb@206 | 850 | else: |
paulb@206 | 851 | return NotImplemented |
paulb@206 | 852 | |
paulb@206 | 853 | def __ne__(self, other): |
paulb@206 | 854 | if isinstance(other, str): |
paulb@206 | 855 | return bool() |
paulb@206 | 856 | else: |
paulb@206 | 857 | return NotImplemented |
paulb@206 | 858 | |
paulb@145 | 859 | def __len__(self): |
paulb@145 | 860 | return int() |
paulb@145 | 861 | |
paulb@145 | 862 | def __str__(self): |
paulb@145 | 863 | return self |
paulb@145 | 864 | |
paulb@145 | 865 | def __bool__(self): |
paulb@145 | 866 | return self.__len__() != 0 |
paulb@145 | 867 | |
paulb@145 | 868 | def join(self, l): |
paulb@145 | 869 | total = 0 |
paulb@145 | 870 | first = 1 |
paulb@145 | 871 | self_len = self.__len__() |
paulb@145 | 872 | for i in l: |
paulb@145 | 873 | if not first: |
paulb@145 | 874 | total += self_len |
paulb@145 | 875 | total += len(str(i)) |
paulb@145 | 876 | first = 0 |
paulb@145 | 877 | b = buffer(total) |
paulb@145 | 878 | first = 1 |
paulb@145 | 879 | for i in l: |
paulb@145 | 880 | if not first: |
paulb@145 | 881 | b.append(self) |
paulb@145 | 882 | b.append(str(i)) |
paulb@145 | 883 | first = 0 |
paulb@145 | 884 | s = str(b) |
paulb@145 | 885 | return s |
paulb@145 | 886 | |
paulb@22 | 887 | class tuple: |
paulb@225 | 888 | def __init__(self, args): |
paulb@66 | 889 | for arg in args: |
paulb@226 | 890 | self.value = arg |
paulb@22 | 891 | |
paulb@22 | 892 | def __getitem__(self, index): |
paulb@167 | 893 | if -len(self) <= index < len(self): |
paulb@167 | 894 | return self.value |
paulb@22 | 895 | else: |
paulb@167 | 896 | raise IndexError, index |
paulb@167 | 897 | |
paulb@22 | 898 | def __getslice__(self, start, end=None): |
paulb@167 | 899 | if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end: |
paulb@228 | 900 | return (self.value,) |
paulb@167 | 901 | else: |
paulb@228 | 902 | return () |
paulb@22 | 903 | |
paulb@22 | 904 | def __len__(self): |
paulb@167 | 905 | return int() |
paulb@22 | 906 | |
paulb@22 | 907 | def __add__(self, other): |
paulb@225 | 908 | result = list(self) |
paulb@22 | 909 | for value in other: |
paulb@216 | 910 | result.append(value) |
paulb@225 | 911 | return tuple(result) |
paulb@22 | 912 | |
paulb@22 | 913 | def __str__(self): |
paulb@167 | 914 | return str() |
paulb@22 | 915 | |
paulb@22 | 916 | def __iter__(self): |
paulb@22 | 917 | return tupleiterator(self) |
paulb@22 | 918 | |
paulb@130 | 919 | def __bool__(self): |
paulb@22 | 920 | return self.__len__() != 0 |
paulb@22 | 921 | |
paulb@22 | 922 | class tupleiterator: |
paulb@22 | 923 | def __init__(self, l): |
paulb@22 | 924 | self.l = l |
paulb@167 | 925 | self.index = 0 |
paulb@22 | 926 | |
paulb@22 | 927 | def next(self): |
paulb@167 | 928 | if self.index < len(self.l): |
paulb@167 | 929 | self.index += 1 |
paulb@167 | 930 | return self.l[self.index] |
paulb@22 | 931 | else: |
paulb@55 | 932 | raise StopIteration |
paulb@22 | 933 | |
paulb@130 | 934 | def __bool__(self): |
paulb@130 | 935 | return bool() |
paulb@22 | 936 | |
paulb@105 | 937 | class xrange: |
paulb@200 | 938 | def __init__(self, start_or_end, end=None, step=1): |
paulb@200 | 939 | if end is None: |
paulb@200 | 940 | self.start = 0 |
paulb@200 | 941 | self.end = start_or_end |
paulb@200 | 942 | else: |
paulb@200 | 943 | self.start = start_or_end |
paulb@200 | 944 | self.end = end |
paulb@105 | 945 | self.step = step |
paulb@200 | 946 | self.current = self.start |
paulb@105 | 947 | |
paulb@105 | 948 | def __iter__(self): |
paulb@105 | 949 | return self |
paulb@105 | 950 | |
paulb@105 | 951 | def next(self): |
paulb@105 | 952 | if self.current >= self.end: |
paulb@105 | 953 | raise StopIteration |
paulb@105 | 954 | current = self.current |
paulb@105 | 955 | self.current += self.step |
paulb@105 | 956 | return current |
paulb@105 | 957 | |
paulb@145 | 958 | class Exception: |
paulb@167 | 959 | def __init__(self, *args): |
paulb@167 | 960 | pass |
paulb@145 | 961 | |
paulb@202 | 962 | class AssertionError(Exception): |
paulb@212 | 963 | __atomic__ = object |
paulb@202 | 964 | |
paulb@145 | 965 | class AttributeError(Exception): |
paulb@212 | 966 | __atomic__ = object |
paulb@145 | 967 | |
paulb@145 | 968 | class IndexError(Exception): |
paulb@212 | 969 | __atomic__ = object |
paulb@145 | 970 | |
paulb@145 | 971 | class StopIteration(Exception): |
paulb@212 | 972 | __atomic__ = object |
paulb@145 | 973 | |
paulb@145 | 974 | class TypeError(Exception): |
paulb@212 | 975 | __atomic__ = object |
paulb@145 | 976 | |
paulb@186 | 977 | class NotImplementedType: |
paulb@212 | 978 | __atomic__ = object |
paulb@186 | 979 | |
paulb@105 | 980 | # General functions. |
paulb@105 | 981 | |
paulb@22 | 982 | def isinstance(obj, cls): |
paulb@130 | 983 | return bool() |
paulb@22 | 984 | |
paulb@22 | 985 | def issubclass(cls1, cls2): |
paulb@130 | 986 | return bool() |
paulb@22 | 987 | |
paulb@22 | 988 | def len(x): |
paulb@22 | 989 | return x.__len__() |
paulb@22 | 990 | |
paulb@22 | 991 | def max(*l): |
paulb@22 | 992 | max_so_far = l[0] |
paulb@22 | 993 | for i in l[1:]: |
paulb@22 | 994 | if i > max_so_far: |
paulb@22 | 995 | max_so_far = i |
paulb@22 | 996 | return max_so_far |
paulb@22 | 997 | |
paulb@208 | 998 | def range(start_or_end, end=None, step=None): |
paulb@208 | 999 | if end is None: |
paulb@208 | 1000 | start = 0 |
paulb@208 | 1001 | end = start_or_end |
paulb@208 | 1002 | else: |
paulb@208 | 1003 | start = start_or_end |
paulb@105 | 1004 | if start == end: |
paulb@105 | 1005 | return [] |
paulb@105 | 1006 | elif start > end: |
paulb@105 | 1007 | step = step or 1 |
paulb@105 | 1008 | i = start |
paulb@105 | 1009 | result = [] |
paulb@105 | 1010 | while i < end: |
paulb@105 | 1011 | result.append(i) |
paulb@105 | 1012 | i += 1 |
paulb@105 | 1013 | return result |
paulb@105 | 1014 | else: |
paulb@105 | 1015 | step = step or -1 |
paulb@105 | 1016 | i = start |
paulb@105 | 1017 | result = [] |
paulb@105 | 1018 | while i > end: |
paulb@105 | 1019 | result.append(i) |
paulb@105 | 1020 | i -= 1 |
paulb@105 | 1021 | return result |
paulb@105 | 1022 | |
paulb@129 | 1023 | # Special values. |
paulb@22 | 1024 | |
paulb@130 | 1025 | True = bool() |
paulb@130 | 1026 | False = bool() |
paulb@22 | 1027 | None = none() |
paulb@145 | 1028 | stdin = file() |
paulb@145 | 1029 | stdout = file() |
paulb@145 | 1030 | stderr = file() |
paulb@186 | 1031 | NotImplemented = NotImplementedType() |
paulb@22 | 1032 | |
paulb@22 | 1033 | # Special functions. These all operate on references at run-time. |
paulb@22 | 1034 | |
paulb@22 | 1035 | def __is__(a, b): |
paulb@130 | 1036 | return bool() |
paulb@22 | 1037 | |
paulb@22 | 1038 | def __not__(a): |
paulb@130 | 1039 | return bool() |
paulb@22 | 1040 | |
paulb@22 | 1041 | # vim: tabstop=4 expandtab shiftwidth=4 |