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@198 | 34 | __atomic__ = 1 |
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@198 | 75 | __atomic__ = 1 |
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@22 | 200 | def __pow__(self, other): |
paulb@22 | 201 | if isinstance(other, int): |
paulb@22 | 202 | return float() |
paulb@22 | 203 | elif isinstance(other, long): |
paulb@22 | 204 | return float() |
paulb@22 | 205 | elif isinstance(other, float): |
paulb@22 | 206 | return float() |
paulb@22 | 207 | else: |
paulb@55 | 208 | raise TypeError |
paulb@22 | 209 | |
paulb@22 | 210 | def __rpow__(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 __lt__(self, other): |
paulb@22 | 221 | if isinstance(other, int): |
paulb@130 | 222 | return bool() |
paulb@22 | 223 | elif isinstance(other, long): |
paulb@130 | 224 | return bool() |
paulb@22 | 225 | elif isinstance(other, float): |
paulb@130 | 226 | return bool() |
paulb@22 | 227 | else: |
paulb@186 | 228 | return NotImplemented |
paulb@22 | 229 | |
paulb@22 | 230 | def __gt__(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 __le__(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 __ge__(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 __eq__(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 __ne__(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 __neg__(self): |
paulb@22 | 281 | return float() |
paulb@22 | 282 | |
paulb@22 | 283 | def __pos__(self): |
paulb@22 | 284 | return self |
paulb@22 | 285 | |
paulb@22 | 286 | def __str__(self): |
paulb@145 | 287 | return str() |
paulb@22 | 288 | |
paulb@130 | 289 | def __bool__(self): |
paulb@22 | 290 | return self != 0 |
paulb@22 | 291 | |
paulb@145 | 292 | class int: |
paulb@198 | 293 | __atomic__ = 1 |
paulb@198 | 294 | |
paulb@200 | 295 | def __init__(self, number_or_string=None): |
paulb@200 | 296 | pass |
paulb@200 | 297 | |
paulb@145 | 298 | def __iadd__(self, other): |
paulb@145 | 299 | if isinstance(other, int): |
paulb@145 | 300 | return int() |
paulb@145 | 301 | else: |
paulb@145 | 302 | raise TypeError |
paulb@145 | 303 | |
paulb@145 | 304 | def __isub__(self, other): |
paulb@145 | 305 | if isinstance(other, int): |
paulb@145 | 306 | return int() |
paulb@145 | 307 | else: |
paulb@145 | 308 | raise TypeError |
paulb@128 | 309 | |
paulb@22 | 310 | def __add__(self, other): |
paulb@145 | 311 | if isinstance(other, int): |
paulb@145 | 312 | return int() |
paulb@22 | 313 | else: |
paulb@55 | 314 | raise TypeError |
paulb@22 | 315 | |
paulb@22 | 316 | def __radd__(self, other): |
paulb@145 | 317 | if isinstance(other, int): |
paulb@145 | 318 | return int() |
paulb@145 | 319 | else: |
paulb@145 | 320 | raise TypeError |
paulb@145 | 321 | |
paulb@145 | 322 | def __sub__(self, other): |
paulb@145 | 323 | if isinstance(other, int): |
paulb@145 | 324 | return int() |
paulb@145 | 325 | else: |
paulb@145 | 326 | raise TypeError |
paulb@145 | 327 | |
paulb@145 | 328 | def __rsub__(self, other): |
paulb@145 | 329 | if isinstance(other, int): |
paulb@145 | 330 | return int() |
paulb@145 | 331 | else: |
paulb@145 | 332 | raise TypeError |
paulb@145 | 333 | |
paulb@145 | 334 | def __mul__(self, other): |
paulb@145 | 335 | if isinstance(other, int): |
paulb@145 | 336 | return int() |
paulb@145 | 337 | else: |
paulb@145 | 338 | raise TypeError |
paulb@145 | 339 | |
paulb@145 | 340 | def __rmul__(self, other): |
paulb@145 | 341 | if isinstance(other, int): |
paulb@145 | 342 | return int() |
paulb@145 | 343 | else: |
paulb@145 | 344 | raise TypeError |
paulb@145 | 345 | |
paulb@145 | 346 | def __div__(self, other): |
paulb@145 | 347 | if isinstance(other, int): |
paulb@145 | 348 | return int() |
paulb@145 | 349 | else: |
paulb@145 | 350 | raise TypeError |
paulb@145 | 351 | |
paulb@145 | 352 | def __rdiv__(self, other): |
paulb@145 | 353 | if isinstance(other, int): |
paulb@145 | 354 | return int() |
paulb@145 | 355 | else: |
paulb@145 | 356 | raise TypeError |
paulb@145 | 357 | |
paulb@145 | 358 | def __floordiv__(self, other): |
paulb@145 | 359 | if isinstance(other, int): |
paulb@145 | 360 | return int() |
paulb@145 | 361 | else: |
paulb@145 | 362 | raise TypeError |
paulb@145 | 363 | |
paulb@145 | 364 | def __rfloordiv__(self, other): |
paulb@145 | 365 | if isinstance(other, int): |
paulb@145 | 366 | return int() |
paulb@22 | 367 | else: |
paulb@55 | 368 | raise TypeError |
paulb@22 | 369 | |
paulb@145 | 370 | def __pow__(self, other): |
paulb@145 | 371 | if isinstance(other, int): |
paulb@145 | 372 | return int() |
paulb@145 | 373 | else: |
paulb@145 | 374 | raise TypeError |
paulb@145 | 375 | |
paulb@203 | 376 | def __and__(self, other): |
paulb@203 | 377 | if isinstance(other, int): |
paulb@203 | 378 | return int() |
paulb@203 | 379 | else: |
paulb@203 | 380 | raise TypeError |
paulb@203 | 381 | |
paulb@203 | 382 | def __rand__(self, other): |
paulb@203 | 383 | if isinstance(other, int): |
paulb@203 | 384 | return int() |
paulb@203 | 385 | else: |
paulb@203 | 386 | raise TypeError |
paulb@203 | 387 | |
paulb@203 | 388 | def __or__(self, other): |
paulb@203 | 389 | if isinstance(other, int): |
paulb@203 | 390 | return int() |
paulb@203 | 391 | else: |
paulb@203 | 392 | raise TypeError |
paulb@203 | 393 | |
paulb@203 | 394 | def __ror__(self, other): |
paulb@203 | 395 | if isinstance(other, int): |
paulb@203 | 396 | return int() |
paulb@203 | 397 | else: |
paulb@203 | 398 | raise TypeError |
paulb@203 | 399 | |
paulb@203 | 400 | def __xor__(self, other): |
paulb@203 | 401 | if isinstance(other, int): |
paulb@203 | 402 | return int() |
paulb@203 | 403 | else: |
paulb@203 | 404 | raise TypeError |
paulb@203 | 405 | |
paulb@203 | 406 | def __rxor__(self, other): |
paulb@203 | 407 | if isinstance(other, int): |
paulb@203 | 408 | return int() |
paulb@203 | 409 | else: |
paulb@203 | 410 | raise TypeError |
paulb@203 | 411 | |
paulb@145 | 412 | def __lt__(self, other): |
paulb@145 | 413 | if isinstance(other, int): |
paulb@145 | 414 | return bool() |
paulb@145 | 415 | else: |
paulb@186 | 416 | return NotImplemented |
paulb@145 | 417 | |
paulb@145 | 418 | def __gt__(self, other): |
paulb@145 | 419 | if isinstance(other, int): |
paulb@145 | 420 | return bool() |
paulb@145 | 421 | else: |
paulb@186 | 422 | return NotImplemented |
paulb@145 | 423 | |
paulb@145 | 424 | def __le__(self, other): |
paulb@145 | 425 | if isinstance(other, int): |
paulb@145 | 426 | return bool() |
paulb@145 | 427 | else: |
paulb@186 | 428 | return NotImplemented |
paulb@145 | 429 | |
paulb@145 | 430 | def __ge__(self, other): |
paulb@145 | 431 | if isinstance(other, int): |
paulb@145 | 432 | return bool() |
paulb@145 | 433 | else: |
paulb@186 | 434 | return NotImplemented |
paulb@145 | 435 | |
paulb@145 | 436 | def __eq__(self, other): |
paulb@145 | 437 | if isinstance(other, int): |
paulb@145 | 438 | return bool() |
paulb@145 | 439 | else: |
paulb@186 | 440 | return NotImplemented |
paulb@145 | 441 | |
paulb@145 | 442 | def __ne__(self, other): |
paulb@145 | 443 | if isinstance(other, int): |
paulb@145 | 444 | return bool() |
paulb@145 | 445 | else: |
paulb@186 | 446 | return NotImplemented |
paulb@145 | 447 | |
paulb@145 | 448 | def __neg__(self): |
paulb@22 | 449 | return int() |
paulb@22 | 450 | |
paulb@145 | 451 | def __pos__(self): |
paulb@145 | 452 | return self |
paulb@145 | 453 | |
paulb@22 | 454 | def __str__(self): |
paulb@145 | 455 | return str() |
paulb@22 | 456 | |
paulb@130 | 457 | def __bool__(self): |
paulb@145 | 458 | return self != 0 |
paulb@22 | 459 | |
paulb@22 | 460 | class list: |
paulb@204 | 461 | def __init__(self, args=None): |
paulb@204 | 462 | if args is not None: |
paulb@204 | 463 | for arg in args: |
paulb@204 | 464 | self.append(arg) |
paulb@22 | 465 | |
paulb@22 | 466 | def __getitem__(self, index): |
paulb@167 | 467 | if -len(self) <= index < len(self): |
paulb@167 | 468 | return self.value |
paulb@22 | 469 | else: |
paulb@167 | 470 | raise IndexError, index |
paulb@22 | 471 | |
paulb@22 | 472 | def __setitem__(self, index, value): |
paulb@167 | 473 | if -len(self) <= index < len(self): |
paulb@167 | 474 | self.value = value |
paulb@22 | 475 | else: |
paulb@167 | 476 | raise IndexError, index |
paulb@22 | 477 | |
paulb@22 | 478 | def __getslice__(self, start, end=None): |
paulb@167 | 479 | if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end: |
paulb@167 | 480 | return list(self.value) |
paulb@167 | 481 | else: |
paulb@167 | 482 | raise IndexError, index |
paulb@22 | 483 | |
paulb@22 | 484 | def __setslice__(self, start, end, slice): |
paulb@167 | 485 | if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end: |
paulb@167 | 486 | for value in slice: |
paulb@167 | 487 | self.value = value |
paulb@167 | 488 | else: |
paulb@167 | 489 | raise IndexError, index |
paulb@22 | 490 | |
paulb@22 | 491 | def append(self, value): |
paulb@167 | 492 | self.value = value |
paulb@22 | 493 | |
paulb@22 | 494 | def __len__(self): |
paulb@167 | 495 | return int() |
paulb@22 | 496 | |
paulb@22 | 497 | def __add__(self, other): |
paulb@22 | 498 | for value in other: |
paulb@167 | 499 | self.value = value |
paulb@167 | 500 | return self |
paulb@22 | 501 | |
paulb@167 | 502 | __iadd__ = __add__ |
paulb@97 | 503 | |
paulb@22 | 504 | def __str__(self): |
paulb@167 | 505 | return str() |
paulb@22 | 506 | |
paulb@22 | 507 | def __iter__(self): |
paulb@22 | 508 | return listiterator(self) |
paulb@22 | 509 | |
paulb@130 | 510 | def __bool__(self): |
paulb@22 | 511 | return self.__len__() != 0 |
paulb@22 | 512 | |
paulb@22 | 513 | class listiterator: |
paulb@22 | 514 | def __init__(self, l): |
paulb@22 | 515 | self.l = l |
paulb@167 | 516 | self.index = 0 |
paulb@22 | 517 | |
paulb@22 | 518 | def next(self): |
paulb@167 | 519 | if self.index < len(self.l): |
paulb@167 | 520 | self.index += 1 |
paulb@167 | 521 | return self.l[self.index] |
paulb@22 | 522 | else: |
paulb@55 | 523 | raise StopIteration |
paulb@22 | 524 | |
paulb@130 | 525 | def __bool__(self): |
paulb@130 | 526 | return bool() |
paulb@22 | 527 | |
paulb@145 | 528 | class long: |
paulb@198 | 529 | __atomic__ = 1 |
paulb@198 | 530 | |
paulb@200 | 531 | def __init__(self, number_or_string=None): |
paulb@200 | 532 | pass |
paulb@200 | 533 | |
paulb@145 | 534 | def __iadd__(self, other): |
paulb@145 | 535 | if isinstance(other, int): |
paulb@145 | 536 | return long() |
paulb@145 | 537 | elif isinstance(other, long): |
paulb@145 | 538 | return long() |
paulb@145 | 539 | else: |
paulb@145 | 540 | raise TypeError |
paulb@145 | 541 | |
paulb@145 | 542 | def __isub__(self, other): |
paulb@145 | 543 | if isinstance(other, int): |
paulb@145 | 544 | return long() |
paulb@145 | 545 | elif isinstance(other, long): |
paulb@145 | 546 | return long() |
paulb@145 | 547 | else: |
paulb@145 | 548 | raise TypeError |
paulb@145 | 549 | |
paulb@145 | 550 | def __add__(self, other): |
paulb@145 | 551 | if isinstance(other, int): |
paulb@145 | 552 | return long() |
paulb@145 | 553 | elif isinstance(other, long): |
paulb@145 | 554 | return long() |
paulb@145 | 555 | else: |
paulb@145 | 556 | raise TypeError |
paulb@145 | 557 | |
paulb@145 | 558 | def __radd__(self, other): |
paulb@145 | 559 | if isinstance(other, int): |
paulb@145 | 560 | return long() |
paulb@145 | 561 | elif isinstance(other, long): |
paulb@145 | 562 | return long() |
paulb@145 | 563 | else: |
paulb@145 | 564 | raise TypeError |
paulb@145 | 565 | |
paulb@145 | 566 | def __sub__(self, other): |
paulb@145 | 567 | if isinstance(other, int): |
paulb@145 | 568 | return long() |
paulb@145 | 569 | elif isinstance(other, long): |
paulb@145 | 570 | return long() |
paulb@145 | 571 | else: |
paulb@145 | 572 | raise TypeError |
paulb@145 | 573 | |
paulb@145 | 574 | def __rsub__(self, other): |
paulb@145 | 575 | if isinstance(other, int): |
paulb@145 | 576 | return long() |
paulb@145 | 577 | elif isinstance(other, long): |
paulb@145 | 578 | return long() |
paulb@145 | 579 | else: |
paulb@145 | 580 | raise TypeError |
paulb@145 | 581 | |
paulb@203 | 582 | def __and__(self, other): |
paulb@203 | 583 | if isinstance(other, int): |
paulb@203 | 584 | return long() |
paulb@203 | 585 | elif isinstance(other, long): |
paulb@203 | 586 | return long() |
paulb@203 | 587 | else: |
paulb@203 | 588 | raise TypeError |
paulb@203 | 589 | |
paulb@203 | 590 | def __rand__(self, other): |
paulb@203 | 591 | if isinstance(other, int): |
paulb@203 | 592 | return long() |
paulb@203 | 593 | elif isinstance(other, long): |
paulb@203 | 594 | return long() |
paulb@203 | 595 | else: |
paulb@203 | 596 | raise TypeError |
paulb@203 | 597 | |
paulb@203 | 598 | def __or__(self, other): |
paulb@203 | 599 | if isinstance(other, int): |
paulb@203 | 600 | return long() |
paulb@203 | 601 | elif isinstance(other, long): |
paulb@203 | 602 | return long() |
paulb@203 | 603 | else: |
paulb@203 | 604 | raise TypeError |
paulb@203 | 605 | |
paulb@203 | 606 | def __ror__(self, other): |
paulb@203 | 607 | if isinstance(other, int): |
paulb@203 | 608 | return long() |
paulb@203 | 609 | elif isinstance(other, long): |
paulb@203 | 610 | return long() |
paulb@203 | 611 | else: |
paulb@203 | 612 | raise TypeError |
paulb@203 | 613 | |
paulb@203 | 614 | def __xor__(self, other): |
paulb@203 | 615 | if isinstance(other, int): |
paulb@203 | 616 | return long() |
paulb@203 | 617 | elif isinstance(other, long): |
paulb@203 | 618 | return long() |
paulb@203 | 619 | else: |
paulb@203 | 620 | raise TypeError |
paulb@203 | 621 | |
paulb@203 | 622 | def __rxor__(self, other): |
paulb@203 | 623 | if isinstance(other, int): |
paulb@203 | 624 | return long() |
paulb@203 | 625 | elif isinstance(other, long): |
paulb@203 | 626 | return long() |
paulb@203 | 627 | else: |
paulb@203 | 628 | raise TypeError |
paulb@203 | 629 | |
paulb@145 | 630 | def __lt__(self, other): |
paulb@145 | 631 | if isinstance(other, int): |
paulb@145 | 632 | return bool() |
paulb@145 | 633 | elif isinstance(other, long): |
paulb@145 | 634 | return bool() |
paulb@145 | 635 | else: |
paulb@186 | 636 | return NotImplemented |
paulb@145 | 637 | |
paulb@145 | 638 | def __gt__(self, other): |
paulb@145 | 639 | if isinstance(other, int): |
paulb@145 | 640 | return bool() |
paulb@145 | 641 | elif isinstance(other, long): |
paulb@145 | 642 | return bool() |
paulb@145 | 643 | else: |
paulb@186 | 644 | return NotImplemented |
paulb@145 | 645 | |
paulb@145 | 646 | def __le__(self, other): |
paulb@145 | 647 | if isinstance(other, int): |
paulb@145 | 648 | return bool() |
paulb@145 | 649 | elif isinstance(other, long): |
paulb@145 | 650 | return bool() |
paulb@145 | 651 | else: |
paulb@186 | 652 | return NotImplemented |
paulb@145 | 653 | |
paulb@145 | 654 | def __ge__(self, other): |
paulb@145 | 655 | if isinstance(other, int): |
paulb@145 | 656 | return bool() |
paulb@145 | 657 | elif isinstance(other, long): |
paulb@145 | 658 | return bool() |
paulb@145 | 659 | else: |
paulb@186 | 660 | return NotImplemented |
paulb@145 | 661 | |
paulb@145 | 662 | def __eq__(self, other): |
paulb@145 | 663 | if isinstance(other, int): |
paulb@145 | 664 | return bool() |
paulb@145 | 665 | elif isinstance(other, long): |
paulb@145 | 666 | return bool() |
paulb@145 | 667 | else: |
paulb@186 | 668 | return NotImplemented |
paulb@145 | 669 | |
paulb@145 | 670 | def __ne__(self, other): |
paulb@145 | 671 | if isinstance(other, int): |
paulb@145 | 672 | return bool() |
paulb@145 | 673 | elif isinstance(other, long): |
paulb@145 | 674 | return bool() |
paulb@145 | 675 | else: |
paulb@186 | 676 | return NotImplemented |
paulb@145 | 677 | |
paulb@145 | 678 | def __neg__(self): |
paulb@145 | 679 | return long() |
paulb@145 | 680 | |
paulb@145 | 681 | def __pos__(self): |
paulb@145 | 682 | return self |
paulb@145 | 683 | |
paulb@145 | 684 | def __str__(self): |
paulb@145 | 685 | return str() |
paulb@145 | 686 | |
paulb@145 | 687 | def __bool__(self): |
paulb@145 | 688 | return self != 0 |
paulb@145 | 689 | |
paulb@145 | 690 | class none: |
paulb@198 | 691 | __atomic__ = 1 |
paulb@198 | 692 | |
paulb@145 | 693 | def __bool__(self): |
paulb@145 | 694 | return False |
paulb@145 | 695 | |
paulb@145 | 696 | def __str__(self): |
paulb@145 | 697 | return "None" |
paulb@145 | 698 | |
paulb@201 | 699 | class slice: |
paulb@201 | 700 | def __init__(self, start_or_end, end=None, step=None): |
paulb@201 | 701 | if end is None: |
paulb@201 | 702 | self.start = 0 |
paulb@201 | 703 | self.end = start_or_end |
paulb@201 | 704 | else: |
paulb@201 | 705 | self.start = start_or_end |
paulb@201 | 706 | self.end = end |
paulb@201 | 707 | self.step = step |
paulb@201 | 708 | |
paulb@145 | 709 | class str: |
paulb@198 | 710 | __atomic__ = 1 |
paulb@198 | 711 | |
paulb@145 | 712 | def __init__(self, x=None): |
paulb@145 | 713 | x.__str__() |
paulb@145 | 714 | |
paulb@145 | 715 | def __add__(self, other): |
paulb@145 | 716 | if isinstance(other, str): |
paulb@145 | 717 | return str() |
paulb@145 | 718 | else: |
paulb@145 | 719 | raise TypeError |
paulb@145 | 720 | |
paulb@145 | 721 | def __radd__(self, other): |
paulb@145 | 722 | if isinstance(other, str): |
paulb@145 | 723 | return str() |
paulb@145 | 724 | else: |
paulb@145 | 725 | raise TypeError |
paulb@145 | 726 | |
paulb@200 | 727 | def __mod__(self, other): |
paulb@200 | 728 | |
paulb@200 | 729 | "The format operator for strings." |
paulb@200 | 730 | |
paulb@200 | 731 | return str() |
paulb@200 | 732 | |
paulb@206 | 733 | def __lt__(self, other): |
paulb@206 | 734 | if isinstance(other, str): |
paulb@206 | 735 | return bool() |
paulb@206 | 736 | else: |
paulb@206 | 737 | return NotImplemented |
paulb@206 | 738 | |
paulb@206 | 739 | def __gt__(self, other): |
paulb@206 | 740 | if isinstance(other, str): |
paulb@206 | 741 | return bool() |
paulb@206 | 742 | else: |
paulb@206 | 743 | return NotImplemented |
paulb@206 | 744 | |
paulb@206 | 745 | def __le__(self, other): |
paulb@206 | 746 | if isinstance(other, str): |
paulb@206 | 747 | return bool() |
paulb@206 | 748 | else: |
paulb@206 | 749 | return NotImplemented |
paulb@206 | 750 | |
paulb@206 | 751 | def __ge__(self, other): |
paulb@206 | 752 | if isinstance(other, str): |
paulb@206 | 753 | return bool() |
paulb@206 | 754 | else: |
paulb@206 | 755 | return NotImplemented |
paulb@206 | 756 | |
paulb@206 | 757 | def __eq__(self, other): |
paulb@206 | 758 | if isinstance(other, str): |
paulb@206 | 759 | return bool() |
paulb@206 | 760 | else: |
paulb@206 | 761 | return NotImplemented |
paulb@206 | 762 | |
paulb@206 | 763 | def __ne__(self, other): |
paulb@206 | 764 | if isinstance(other, str): |
paulb@206 | 765 | return bool() |
paulb@206 | 766 | else: |
paulb@206 | 767 | return NotImplemented |
paulb@206 | 768 | |
paulb@145 | 769 | def __len__(self): |
paulb@145 | 770 | return int() |
paulb@145 | 771 | |
paulb@145 | 772 | def __str__(self): |
paulb@145 | 773 | return self |
paulb@145 | 774 | |
paulb@145 | 775 | def __bool__(self): |
paulb@145 | 776 | return self.__len__() != 0 |
paulb@145 | 777 | |
paulb@145 | 778 | def join(self, l): |
paulb@145 | 779 | total = 0 |
paulb@145 | 780 | first = 1 |
paulb@145 | 781 | self_len = self.__len__() |
paulb@145 | 782 | for i in l: |
paulb@145 | 783 | if not first: |
paulb@145 | 784 | total += self_len |
paulb@145 | 785 | total += len(str(i)) |
paulb@145 | 786 | first = 0 |
paulb@145 | 787 | b = buffer(total) |
paulb@145 | 788 | first = 1 |
paulb@145 | 789 | for i in l: |
paulb@145 | 790 | if not first: |
paulb@145 | 791 | b.append(self) |
paulb@145 | 792 | b.append(str(i)) |
paulb@145 | 793 | first = 0 |
paulb@145 | 794 | s = str(b) |
paulb@145 | 795 | return s |
paulb@145 | 796 | |
paulb@22 | 797 | class tuple: |
paulb@66 | 798 | def __init__(self, *args): |
paulb@66 | 799 | for arg in args: |
paulb@66 | 800 | self.append(arg) |
paulb@22 | 801 | |
paulb@22 | 802 | def __getitem__(self, index): |
paulb@167 | 803 | if -len(self) <= index < len(self): |
paulb@167 | 804 | return self.value |
paulb@22 | 805 | else: |
paulb@167 | 806 | raise IndexError, index |
paulb@167 | 807 | |
paulb@167 | 808 | def __setitem__(self, index, value): |
paulb@167 | 809 | if -len(self) <= index < len(self): |
paulb@167 | 810 | self.value = value |
paulb@167 | 811 | else: |
paulb@167 | 812 | raise IndexError, index |
paulb@22 | 813 | |
paulb@22 | 814 | def __getslice__(self, start, end=None): |
paulb@167 | 815 | if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end: |
paulb@167 | 816 | return list(self.value) |
paulb@167 | 817 | else: |
paulb@167 | 818 | raise IndexError, index |
paulb@167 | 819 | |
paulb@167 | 820 | def __setslice__(self, start, end, slice): |
paulb@167 | 821 | if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end: |
paulb@167 | 822 | for value in slice: |
paulb@167 | 823 | self.value = value |
paulb@167 | 824 | else: |
paulb@167 | 825 | raise IndexError, index |
paulb@22 | 826 | |
paulb@22 | 827 | # NOTE: The append method should be internal at most. |
paulb@22 | 828 | |
paulb@22 | 829 | def append(self, value): |
paulb@167 | 830 | self.value = value |
paulb@22 | 831 | |
paulb@22 | 832 | def __len__(self): |
paulb@167 | 833 | return int() |
paulb@22 | 834 | |
paulb@22 | 835 | def __add__(self, other): |
paulb@22 | 836 | for value in other: |
paulb@167 | 837 | self.value = value |
paulb@167 | 838 | return self |
paulb@22 | 839 | |
paulb@22 | 840 | def __str__(self): |
paulb@167 | 841 | return str() |
paulb@22 | 842 | |
paulb@22 | 843 | def __iter__(self): |
paulb@22 | 844 | return tupleiterator(self) |
paulb@22 | 845 | |
paulb@130 | 846 | def __bool__(self): |
paulb@22 | 847 | return self.__len__() != 0 |
paulb@22 | 848 | |
paulb@22 | 849 | class tupleiterator: |
paulb@22 | 850 | def __init__(self, l): |
paulb@22 | 851 | self.l = l |
paulb@167 | 852 | self.index = 0 |
paulb@22 | 853 | |
paulb@22 | 854 | def next(self): |
paulb@167 | 855 | if self.index < len(self.l): |
paulb@167 | 856 | self.index += 1 |
paulb@167 | 857 | return self.l[self.index] |
paulb@22 | 858 | else: |
paulb@55 | 859 | raise StopIteration |
paulb@22 | 860 | |
paulb@130 | 861 | def __bool__(self): |
paulb@130 | 862 | return bool() |
paulb@22 | 863 | |
paulb@105 | 864 | class xrange: |
paulb@200 | 865 | def __init__(self, start_or_end, end=None, step=1): |
paulb@200 | 866 | if end is None: |
paulb@200 | 867 | self.start = 0 |
paulb@200 | 868 | self.end = start_or_end |
paulb@200 | 869 | else: |
paulb@200 | 870 | self.start = start_or_end |
paulb@200 | 871 | self.end = end |
paulb@105 | 872 | self.step = step |
paulb@200 | 873 | self.current = self.start |
paulb@105 | 874 | |
paulb@105 | 875 | def __iter__(self): |
paulb@105 | 876 | return self |
paulb@105 | 877 | |
paulb@105 | 878 | def next(self): |
paulb@105 | 879 | if self.current >= self.end: |
paulb@105 | 880 | raise StopIteration |
paulb@105 | 881 | current = self.current |
paulb@105 | 882 | self.current += self.step |
paulb@105 | 883 | return current |
paulb@105 | 884 | |
paulb@145 | 885 | class Exception: |
paulb@167 | 886 | def __init__(self, *args): |
paulb@167 | 887 | pass |
paulb@145 | 888 | |
paulb@202 | 889 | class AssertionError(Exception): |
paulb@202 | 890 | __atomic__ = 1 |
paulb@202 | 891 | |
paulb@145 | 892 | class AttributeError(Exception): |
paulb@198 | 893 | __atomic__ = 1 |
paulb@145 | 894 | |
paulb@145 | 895 | class IndexError(Exception): |
paulb@198 | 896 | __atomic__ = 1 |
paulb@145 | 897 | |
paulb@145 | 898 | class StopIteration(Exception): |
paulb@198 | 899 | __atomic__ = 1 |
paulb@145 | 900 | |
paulb@145 | 901 | class TypeError(Exception): |
paulb@198 | 902 | __atomic__ = 1 |
paulb@145 | 903 | |
paulb@186 | 904 | class NotImplementedType: |
paulb@198 | 905 | __atomic__ = 1 |
paulb@186 | 906 | |
paulb@105 | 907 | # General functions. |
paulb@105 | 908 | |
paulb@22 | 909 | def isinstance(obj, cls): |
paulb@130 | 910 | return bool() |
paulb@22 | 911 | |
paulb@22 | 912 | def issubclass(cls1, cls2): |
paulb@130 | 913 | return bool() |
paulb@22 | 914 | |
paulb@22 | 915 | def len(x): |
paulb@22 | 916 | return x.__len__() |
paulb@22 | 917 | |
paulb@22 | 918 | def max(*l): |
paulb@22 | 919 | max_so_far = l[0] |
paulb@22 | 920 | for i in l[1:]: |
paulb@22 | 921 | if i > max_so_far: |
paulb@22 | 922 | max_so_far = i |
paulb@22 | 923 | return max_so_far |
paulb@22 | 924 | |
paulb@208 | 925 | def range(start_or_end, end=None, step=None): |
paulb@208 | 926 | if end is None: |
paulb@208 | 927 | start = 0 |
paulb@208 | 928 | end = start_or_end |
paulb@208 | 929 | else: |
paulb@208 | 930 | start = start_or_end |
paulb@105 | 931 | if start == end: |
paulb@105 | 932 | return [] |
paulb@105 | 933 | elif start > end: |
paulb@105 | 934 | step = step or 1 |
paulb@105 | 935 | i = start |
paulb@105 | 936 | result = [] |
paulb@105 | 937 | while i < end: |
paulb@105 | 938 | result.append(i) |
paulb@105 | 939 | i += 1 |
paulb@105 | 940 | return result |
paulb@105 | 941 | else: |
paulb@105 | 942 | step = step or -1 |
paulb@105 | 943 | i = start |
paulb@105 | 944 | result = [] |
paulb@105 | 945 | while i > end: |
paulb@105 | 946 | result.append(i) |
paulb@105 | 947 | i -= 1 |
paulb@105 | 948 | return result |
paulb@105 | 949 | |
paulb@129 | 950 | # Special values. |
paulb@22 | 951 | |
paulb@130 | 952 | True = bool() |
paulb@130 | 953 | False = bool() |
paulb@22 | 954 | None = none() |
paulb@145 | 955 | stdin = file() |
paulb@145 | 956 | stdout = file() |
paulb@145 | 957 | stderr = file() |
paulb@186 | 958 | NotImplemented = NotImplementedType() |
paulb@22 | 959 | |
paulb@22 | 960 | # Special functions. These all operate on references at run-time. |
paulb@22 | 961 | |
paulb@22 | 962 | def __is__(a, b): |
paulb@130 | 963 | return bool() |
paulb@22 | 964 | |
paulb@22 | 965 | def __not__(a): |
paulb@130 | 966 | return bool() |
paulb@22 | 967 | |
paulb@22 | 968 | # vim: tabstop=4 expandtab shiftwidth=4 |