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@204 | 477 | def __init__(self, args=None): |
paulb@204 | 478 | if args is not None: |
paulb@204 | 479 | for arg in args: |
paulb@204 | 480 | self.append(arg) |
paulb@22 | 481 | |
paulb@22 | 482 | def __getitem__(self, index): |
paulb@167 | 483 | if -len(self) <= index < len(self): |
paulb@167 | 484 | return self.value |
paulb@22 | 485 | else: |
paulb@167 | 486 | raise IndexError, index |
paulb@22 | 487 | |
paulb@22 | 488 | def __setitem__(self, index, value): |
paulb@167 | 489 | if -len(self) <= index < len(self): |
paulb@167 | 490 | self.value = value |
paulb@22 | 491 | else: |
paulb@167 | 492 | raise IndexError, index |
paulb@22 | 493 | |
paulb@22 | 494 | def __getslice__(self, start, end=None): |
paulb@167 | 495 | if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end: |
paulb@228 | 496 | return [self.value] |
paulb@167 | 497 | else: |
paulb@228 | 498 | return [] |
paulb@22 | 499 | |
paulb@22 | 500 | def __setslice__(self, start, end, slice): |
paulb@228 | 501 | #if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end: |
paulb@228 | 502 | for value in slice: |
paulb@228 | 503 | self.value = value |
paulb@22 | 504 | |
paulb@22 | 505 | def append(self, value): |
paulb@167 | 506 | self.value = value |
paulb@22 | 507 | |
paulb@22 | 508 | def __len__(self): |
paulb@167 | 509 | return int() |
paulb@22 | 510 | |
paulb@22 | 511 | def __add__(self, other): |
paulb@22 | 512 | for value in other: |
paulb@167 | 513 | self.value = value |
paulb@167 | 514 | return self |
paulb@22 | 515 | |
paulb@167 | 516 | __iadd__ = __add__ |
paulb@97 | 517 | |
paulb@22 | 518 | def __str__(self): |
paulb@167 | 519 | return str() |
paulb@22 | 520 | |
paulb@22 | 521 | def __iter__(self): |
paulb@22 | 522 | return listiterator(self) |
paulb@22 | 523 | |
paulb@130 | 524 | def __bool__(self): |
paulb@22 | 525 | return self.__len__() != 0 |
paulb@22 | 526 | |
paulb@22 | 527 | class listiterator: |
paulb@22 | 528 | def __init__(self, l): |
paulb@22 | 529 | self.l = l |
paulb@167 | 530 | self.index = 0 |
paulb@22 | 531 | |
paulb@22 | 532 | def next(self): |
paulb@167 | 533 | if self.index < len(self.l): |
paulb@167 | 534 | self.index += 1 |
paulb@167 | 535 | return self.l[self.index] |
paulb@22 | 536 | else: |
paulb@55 | 537 | raise StopIteration |
paulb@22 | 538 | |
paulb@130 | 539 | def __bool__(self): |
paulb@130 | 540 | return bool() |
paulb@22 | 541 | |
paulb@145 | 542 | class long: |
paulb@212 | 543 | __atomic__ = object |
paulb@198 | 544 | |
paulb@200 | 545 | def __init__(self, number_or_string=None): |
paulb@200 | 546 | pass |
paulb@200 | 547 | |
paulb@145 | 548 | def __iadd__(self, other): |
paulb@145 | 549 | if isinstance(other, int): |
paulb@145 | 550 | return long() |
paulb@145 | 551 | elif isinstance(other, long): |
paulb@145 | 552 | return long() |
paulb@145 | 553 | else: |
paulb@145 | 554 | raise TypeError |
paulb@145 | 555 | |
paulb@145 | 556 | def __isub__(self, other): |
paulb@145 | 557 | if isinstance(other, int): |
paulb@145 | 558 | return long() |
paulb@145 | 559 | elif isinstance(other, long): |
paulb@145 | 560 | return long() |
paulb@145 | 561 | else: |
paulb@145 | 562 | raise TypeError |
paulb@145 | 563 | |
paulb@145 | 564 | def __add__(self, other): |
paulb@145 | 565 | if isinstance(other, int): |
paulb@145 | 566 | return long() |
paulb@145 | 567 | elif isinstance(other, long): |
paulb@145 | 568 | return long() |
paulb@145 | 569 | else: |
paulb@145 | 570 | raise TypeError |
paulb@145 | 571 | |
paulb@145 | 572 | def __radd__(self, other): |
paulb@145 | 573 | if isinstance(other, int): |
paulb@145 | 574 | return long() |
paulb@145 | 575 | elif isinstance(other, long): |
paulb@145 | 576 | return long() |
paulb@145 | 577 | else: |
paulb@145 | 578 | raise TypeError |
paulb@145 | 579 | |
paulb@145 | 580 | def __sub__(self, other): |
paulb@145 | 581 | if isinstance(other, int): |
paulb@145 | 582 | return long() |
paulb@145 | 583 | elif isinstance(other, long): |
paulb@145 | 584 | return long() |
paulb@145 | 585 | else: |
paulb@145 | 586 | raise TypeError |
paulb@145 | 587 | |
paulb@145 | 588 | def __rsub__(self, other): |
paulb@145 | 589 | if isinstance(other, int): |
paulb@145 | 590 | return long() |
paulb@145 | 591 | elif isinstance(other, long): |
paulb@145 | 592 | return long() |
paulb@145 | 593 | else: |
paulb@145 | 594 | raise TypeError |
paulb@145 | 595 | |
paulb@238 | 596 | def __mul__(self, other): |
paulb@238 | 597 | if isinstance(other, int): |
paulb@238 | 598 | return long() |
paulb@238 | 599 | elif isinstance(other, long): |
paulb@238 | 600 | return long() |
paulb@238 | 601 | else: |
paulb@238 | 602 | raise TypeError |
paulb@238 | 603 | |
paulb@238 | 604 | def __rmul__(self, other): |
paulb@238 | 605 | if isinstance(other, int): |
paulb@238 | 606 | return long() |
paulb@238 | 607 | elif isinstance(other, long): |
paulb@238 | 608 | return long() |
paulb@238 | 609 | else: |
paulb@238 | 610 | raise TypeError |
paulb@238 | 611 | |
paulb@238 | 612 | def __div__(self, other): |
paulb@238 | 613 | if isinstance(other, int): |
paulb@238 | 614 | return long() |
paulb@238 | 615 | elif isinstance(other, long): |
paulb@238 | 616 | return long() |
paulb@238 | 617 | else: |
paulb@238 | 618 | raise TypeError |
paulb@238 | 619 | |
paulb@238 | 620 | def __rdiv__(self, other): |
paulb@238 | 621 | if isinstance(other, int): |
paulb@238 | 622 | return long() |
paulb@238 | 623 | elif isinstance(other, long): |
paulb@238 | 624 | return long() |
paulb@238 | 625 | else: |
paulb@238 | 626 | raise TypeError |
paulb@238 | 627 | |
paulb@238 | 628 | def __floordiv__(self, other): |
paulb@238 | 629 | if isinstance(other, int): |
paulb@238 | 630 | return long() |
paulb@238 | 631 | elif isinstance(other, long): |
paulb@238 | 632 | return long() |
paulb@238 | 633 | else: |
paulb@238 | 634 | raise TypeError |
paulb@238 | 635 | |
paulb@238 | 636 | def __rfloordiv__(self, other): |
paulb@238 | 637 | if isinstance(other, int): |
paulb@238 | 638 | return long() |
paulb@238 | 639 | elif isinstance(other, long): |
paulb@238 | 640 | return long() |
paulb@238 | 641 | else: |
paulb@238 | 642 | raise TypeError |
paulb@238 | 643 | |
paulb@203 | 644 | def __and__(self, other): |
paulb@203 | 645 | if isinstance(other, int): |
paulb@203 | 646 | return long() |
paulb@203 | 647 | elif isinstance(other, long): |
paulb@203 | 648 | return long() |
paulb@203 | 649 | else: |
paulb@203 | 650 | raise TypeError |
paulb@203 | 651 | |
paulb@203 | 652 | def __rand__(self, other): |
paulb@203 | 653 | if isinstance(other, int): |
paulb@203 | 654 | return long() |
paulb@203 | 655 | elif isinstance(other, long): |
paulb@203 | 656 | return long() |
paulb@203 | 657 | else: |
paulb@203 | 658 | raise TypeError |
paulb@203 | 659 | |
paulb@203 | 660 | def __or__(self, other): |
paulb@203 | 661 | if isinstance(other, int): |
paulb@203 | 662 | return long() |
paulb@203 | 663 | elif isinstance(other, long): |
paulb@203 | 664 | return long() |
paulb@203 | 665 | else: |
paulb@203 | 666 | raise TypeError |
paulb@203 | 667 | |
paulb@203 | 668 | def __ror__(self, other): |
paulb@203 | 669 | if isinstance(other, int): |
paulb@203 | 670 | return long() |
paulb@203 | 671 | elif isinstance(other, long): |
paulb@203 | 672 | return long() |
paulb@203 | 673 | else: |
paulb@203 | 674 | raise TypeError |
paulb@203 | 675 | |
paulb@203 | 676 | def __xor__(self, other): |
paulb@203 | 677 | if isinstance(other, int): |
paulb@203 | 678 | return long() |
paulb@203 | 679 | elif isinstance(other, long): |
paulb@203 | 680 | return long() |
paulb@203 | 681 | else: |
paulb@203 | 682 | raise TypeError |
paulb@203 | 683 | |
paulb@203 | 684 | def __rxor__(self, other): |
paulb@203 | 685 | if isinstance(other, int): |
paulb@203 | 686 | return long() |
paulb@203 | 687 | elif isinstance(other, long): |
paulb@203 | 688 | return long() |
paulb@203 | 689 | else: |
paulb@203 | 690 | raise TypeError |
paulb@203 | 691 | |
paulb@145 | 692 | def __lt__(self, other): |
paulb@145 | 693 | if isinstance(other, int): |
paulb@145 | 694 | return bool() |
paulb@145 | 695 | elif isinstance(other, long): |
paulb@145 | 696 | return bool() |
paulb@145 | 697 | else: |
paulb@186 | 698 | return NotImplemented |
paulb@145 | 699 | |
paulb@145 | 700 | def __gt__(self, other): |
paulb@145 | 701 | if isinstance(other, int): |
paulb@145 | 702 | return bool() |
paulb@145 | 703 | elif isinstance(other, long): |
paulb@145 | 704 | return bool() |
paulb@145 | 705 | else: |
paulb@186 | 706 | return NotImplemented |
paulb@145 | 707 | |
paulb@145 | 708 | def __le__(self, other): |
paulb@145 | 709 | if isinstance(other, int): |
paulb@145 | 710 | return bool() |
paulb@145 | 711 | elif isinstance(other, long): |
paulb@145 | 712 | return bool() |
paulb@145 | 713 | else: |
paulb@186 | 714 | return NotImplemented |
paulb@145 | 715 | |
paulb@145 | 716 | def __ge__(self, other): |
paulb@145 | 717 | if isinstance(other, int): |
paulb@145 | 718 | return bool() |
paulb@145 | 719 | elif isinstance(other, long): |
paulb@145 | 720 | return bool() |
paulb@145 | 721 | else: |
paulb@186 | 722 | return NotImplemented |
paulb@145 | 723 | |
paulb@145 | 724 | def __eq__(self, other): |
paulb@145 | 725 | if isinstance(other, int): |
paulb@145 | 726 | return bool() |
paulb@145 | 727 | elif isinstance(other, long): |
paulb@145 | 728 | return bool() |
paulb@145 | 729 | else: |
paulb@186 | 730 | return NotImplemented |
paulb@145 | 731 | |
paulb@145 | 732 | def __ne__(self, other): |
paulb@145 | 733 | if isinstance(other, int): |
paulb@145 | 734 | return bool() |
paulb@145 | 735 | elif isinstance(other, long): |
paulb@145 | 736 | return bool() |
paulb@145 | 737 | else: |
paulb@186 | 738 | return NotImplemented |
paulb@145 | 739 | |
paulb@145 | 740 | def __neg__(self): |
paulb@145 | 741 | return long() |
paulb@145 | 742 | |
paulb@145 | 743 | def __pos__(self): |
paulb@145 | 744 | return self |
paulb@145 | 745 | |
paulb@145 | 746 | def __str__(self): |
paulb@145 | 747 | return str() |
paulb@145 | 748 | |
paulb@145 | 749 | def __bool__(self): |
paulb@145 | 750 | return self != 0 |
paulb@145 | 751 | |
paulb@145 | 752 | class none: |
paulb@212 | 753 | __atomic__ = object |
paulb@198 | 754 | |
paulb@145 | 755 | def __bool__(self): |
paulb@145 | 756 | return False |
paulb@145 | 757 | |
paulb@145 | 758 | def __str__(self): |
paulb@145 | 759 | return "None" |
paulb@145 | 760 | |
paulb@201 | 761 | class slice: |
paulb@201 | 762 | def __init__(self, start_or_end, end=None, step=None): |
paulb@201 | 763 | if end is None: |
paulb@201 | 764 | self.start = 0 |
paulb@201 | 765 | self.end = start_or_end |
paulb@201 | 766 | else: |
paulb@201 | 767 | self.start = start_or_end |
paulb@201 | 768 | self.end = end |
paulb@201 | 769 | self.step = step |
paulb@201 | 770 | |
paulb@145 | 771 | class str: |
paulb@212 | 772 | __atomic__ = object |
paulb@198 | 773 | |
paulb@145 | 774 | def __init__(self, x=None): |
paulb@145 | 775 | x.__str__() |
paulb@145 | 776 | |
paulb@238 | 777 | def __iadd__(self, other): |
paulb@238 | 778 | if isinstance(other, str): |
paulb@238 | 779 | return str() |
paulb@238 | 780 | else: |
paulb@238 | 781 | raise TypeError |
paulb@238 | 782 | |
paulb@145 | 783 | def __add__(self, other): |
paulb@145 | 784 | if isinstance(other, str): |
paulb@145 | 785 | return str() |
paulb@145 | 786 | else: |
paulb@145 | 787 | raise TypeError |
paulb@145 | 788 | |
paulb@145 | 789 | def __radd__(self, other): |
paulb@145 | 790 | if isinstance(other, str): |
paulb@145 | 791 | return str() |
paulb@145 | 792 | else: |
paulb@145 | 793 | raise TypeError |
paulb@145 | 794 | |
paulb@238 | 795 | def __mul__(self, other): |
paulb@238 | 796 | if isinstance(other, int) or isinstance(other, long): |
paulb@238 | 797 | return str() |
paulb@238 | 798 | else: |
paulb@238 | 799 | raise TypeError |
paulb@238 | 800 | |
paulb@238 | 801 | def __radd__(self, other): |
paulb@238 | 802 | if isinstance(other, int) or isinstance(other, long): |
paulb@238 | 803 | return str() |
paulb@238 | 804 | else: |
paulb@238 | 805 | raise TypeError |
paulb@238 | 806 | |
paulb@200 | 807 | def __mod__(self, other): |
paulb@200 | 808 | |
paulb@200 | 809 | "The format operator for strings." |
paulb@200 | 810 | |
paulb@200 | 811 | return str() |
paulb@200 | 812 | |
paulb@206 | 813 | def __lt__(self, other): |
paulb@206 | 814 | if isinstance(other, str): |
paulb@206 | 815 | return bool() |
paulb@206 | 816 | else: |
paulb@206 | 817 | return NotImplemented |
paulb@206 | 818 | |
paulb@206 | 819 | def __gt__(self, other): |
paulb@206 | 820 | if isinstance(other, str): |
paulb@206 | 821 | return bool() |
paulb@206 | 822 | else: |
paulb@206 | 823 | return NotImplemented |
paulb@206 | 824 | |
paulb@206 | 825 | def __le__(self, other): |
paulb@206 | 826 | if isinstance(other, str): |
paulb@206 | 827 | return bool() |
paulb@206 | 828 | else: |
paulb@206 | 829 | return NotImplemented |
paulb@206 | 830 | |
paulb@206 | 831 | def __ge__(self, other): |
paulb@206 | 832 | if isinstance(other, str): |
paulb@206 | 833 | return bool() |
paulb@206 | 834 | else: |
paulb@206 | 835 | return NotImplemented |
paulb@206 | 836 | |
paulb@206 | 837 | def __eq__(self, other): |
paulb@206 | 838 | if isinstance(other, str): |
paulb@206 | 839 | return bool() |
paulb@206 | 840 | else: |
paulb@206 | 841 | return NotImplemented |
paulb@206 | 842 | |
paulb@206 | 843 | def __ne__(self, other): |
paulb@206 | 844 | if isinstance(other, str): |
paulb@206 | 845 | return bool() |
paulb@206 | 846 | else: |
paulb@206 | 847 | return NotImplemented |
paulb@206 | 848 | |
paulb@145 | 849 | def __len__(self): |
paulb@145 | 850 | return int() |
paulb@145 | 851 | |
paulb@145 | 852 | def __str__(self): |
paulb@145 | 853 | return self |
paulb@145 | 854 | |
paulb@145 | 855 | def __bool__(self): |
paulb@145 | 856 | return self.__len__() != 0 |
paulb@145 | 857 | |
paulb@145 | 858 | def join(self, l): |
paulb@145 | 859 | total = 0 |
paulb@145 | 860 | first = 1 |
paulb@145 | 861 | self_len = self.__len__() |
paulb@145 | 862 | for i in l: |
paulb@145 | 863 | if not first: |
paulb@145 | 864 | total += self_len |
paulb@145 | 865 | total += len(str(i)) |
paulb@145 | 866 | first = 0 |
paulb@145 | 867 | b = buffer(total) |
paulb@145 | 868 | first = 1 |
paulb@145 | 869 | for i in l: |
paulb@145 | 870 | if not first: |
paulb@145 | 871 | b.append(self) |
paulb@145 | 872 | b.append(str(i)) |
paulb@145 | 873 | first = 0 |
paulb@145 | 874 | s = str(b) |
paulb@145 | 875 | return s |
paulb@145 | 876 | |
paulb@22 | 877 | class tuple: |
paulb@225 | 878 | def __init__(self, args): |
paulb@66 | 879 | for arg in args: |
paulb@226 | 880 | self.value = arg |
paulb@22 | 881 | |
paulb@22 | 882 | def __getitem__(self, index): |
paulb@167 | 883 | if -len(self) <= index < len(self): |
paulb@167 | 884 | return self.value |
paulb@22 | 885 | else: |
paulb@167 | 886 | raise IndexError, index |
paulb@167 | 887 | |
paulb@22 | 888 | def __getslice__(self, start, end=None): |
paulb@167 | 889 | if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end: |
paulb@228 | 890 | return (self.value,) |
paulb@167 | 891 | else: |
paulb@228 | 892 | return () |
paulb@22 | 893 | |
paulb@22 | 894 | def __len__(self): |
paulb@167 | 895 | return int() |
paulb@22 | 896 | |
paulb@22 | 897 | def __add__(self, other): |
paulb@225 | 898 | result = list(self) |
paulb@22 | 899 | for value in other: |
paulb@216 | 900 | result.append(value) |
paulb@225 | 901 | return tuple(result) |
paulb@22 | 902 | |
paulb@22 | 903 | def __str__(self): |
paulb@167 | 904 | return str() |
paulb@22 | 905 | |
paulb@22 | 906 | def __iter__(self): |
paulb@22 | 907 | return tupleiterator(self) |
paulb@22 | 908 | |
paulb@130 | 909 | def __bool__(self): |
paulb@22 | 910 | return self.__len__() != 0 |
paulb@22 | 911 | |
paulb@22 | 912 | class tupleiterator: |
paulb@22 | 913 | def __init__(self, l): |
paulb@22 | 914 | self.l = l |
paulb@167 | 915 | self.index = 0 |
paulb@22 | 916 | |
paulb@22 | 917 | def next(self): |
paulb@167 | 918 | if self.index < len(self.l): |
paulb@167 | 919 | self.index += 1 |
paulb@167 | 920 | return self.l[self.index] |
paulb@22 | 921 | else: |
paulb@55 | 922 | raise StopIteration |
paulb@22 | 923 | |
paulb@130 | 924 | def __bool__(self): |
paulb@130 | 925 | return bool() |
paulb@22 | 926 | |
paulb@105 | 927 | class xrange: |
paulb@200 | 928 | def __init__(self, start_or_end, end=None, step=1): |
paulb@200 | 929 | if end is None: |
paulb@200 | 930 | self.start = 0 |
paulb@200 | 931 | self.end = start_or_end |
paulb@200 | 932 | else: |
paulb@200 | 933 | self.start = start_or_end |
paulb@200 | 934 | self.end = end |
paulb@105 | 935 | self.step = step |
paulb@200 | 936 | self.current = self.start |
paulb@105 | 937 | |
paulb@105 | 938 | def __iter__(self): |
paulb@105 | 939 | return self |
paulb@105 | 940 | |
paulb@105 | 941 | def next(self): |
paulb@105 | 942 | if self.current >= self.end: |
paulb@105 | 943 | raise StopIteration |
paulb@105 | 944 | current = self.current |
paulb@105 | 945 | self.current += self.step |
paulb@105 | 946 | return current |
paulb@105 | 947 | |
paulb@145 | 948 | class Exception: |
paulb@167 | 949 | def __init__(self, *args): |
paulb@167 | 950 | pass |
paulb@145 | 951 | |
paulb@202 | 952 | class AssertionError(Exception): |
paulb@212 | 953 | __atomic__ = object |
paulb@202 | 954 | |
paulb@145 | 955 | class AttributeError(Exception): |
paulb@212 | 956 | __atomic__ = object |
paulb@145 | 957 | |
paulb@145 | 958 | class IndexError(Exception): |
paulb@212 | 959 | __atomic__ = object |
paulb@145 | 960 | |
paulb@145 | 961 | class StopIteration(Exception): |
paulb@212 | 962 | __atomic__ = object |
paulb@145 | 963 | |
paulb@145 | 964 | class TypeError(Exception): |
paulb@212 | 965 | __atomic__ = object |
paulb@145 | 966 | |
paulb@186 | 967 | class NotImplementedType: |
paulb@212 | 968 | __atomic__ = object |
paulb@186 | 969 | |
paulb@105 | 970 | # General functions. |
paulb@105 | 971 | |
paulb@22 | 972 | def isinstance(obj, cls): |
paulb@130 | 973 | return bool() |
paulb@22 | 974 | |
paulb@22 | 975 | def issubclass(cls1, cls2): |
paulb@130 | 976 | return bool() |
paulb@22 | 977 | |
paulb@22 | 978 | def len(x): |
paulb@22 | 979 | return x.__len__() |
paulb@22 | 980 | |
paulb@22 | 981 | def max(*l): |
paulb@22 | 982 | max_so_far = l[0] |
paulb@22 | 983 | for i in l[1:]: |
paulb@22 | 984 | if i > max_so_far: |
paulb@22 | 985 | max_so_far = i |
paulb@22 | 986 | return max_so_far |
paulb@22 | 987 | |
paulb@208 | 988 | def range(start_or_end, end=None, step=None): |
paulb@208 | 989 | if end is None: |
paulb@208 | 990 | start = 0 |
paulb@208 | 991 | end = start_or_end |
paulb@208 | 992 | else: |
paulb@208 | 993 | start = start_or_end |
paulb@105 | 994 | if start == end: |
paulb@105 | 995 | return [] |
paulb@105 | 996 | elif start > end: |
paulb@105 | 997 | step = step or 1 |
paulb@105 | 998 | i = start |
paulb@105 | 999 | result = [] |
paulb@105 | 1000 | while i < end: |
paulb@105 | 1001 | result.append(i) |
paulb@105 | 1002 | i += 1 |
paulb@105 | 1003 | return result |
paulb@105 | 1004 | else: |
paulb@105 | 1005 | step = step or -1 |
paulb@105 | 1006 | i = start |
paulb@105 | 1007 | result = [] |
paulb@105 | 1008 | while i > end: |
paulb@105 | 1009 | result.append(i) |
paulb@105 | 1010 | i -= 1 |
paulb@105 | 1011 | return result |
paulb@105 | 1012 | |
paulb@129 | 1013 | # Special values. |
paulb@22 | 1014 | |
paulb@130 | 1015 | True = bool() |
paulb@130 | 1016 | False = bool() |
paulb@22 | 1017 | None = none() |
paulb@145 | 1018 | stdin = file() |
paulb@145 | 1019 | stdout = file() |
paulb@145 | 1020 | stderr = file() |
paulb@186 | 1021 | NotImplemented = NotImplementedType() |
paulb@22 | 1022 | |
paulb@22 | 1023 | # Special functions. These all operate on references at run-time. |
paulb@22 | 1024 | |
paulb@22 | 1025 | def __is__(a, b): |
paulb@130 | 1026 | return bool() |
paulb@22 | 1027 | |
paulb@22 | 1028 | def __not__(a): |
paulb@130 | 1029 | return bool() |
paulb@22 | 1030 | |
paulb@22 | 1031 | # vim: tabstop=4 expandtab shiftwidth=4 |