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@145 | 376 | def __lt__(self, other): |
paulb@145 | 377 | if isinstance(other, int): |
paulb@145 | 378 | return bool() |
paulb@145 | 379 | else: |
paulb@186 | 380 | return NotImplemented |
paulb@145 | 381 | |
paulb@145 | 382 | def __gt__(self, other): |
paulb@145 | 383 | if isinstance(other, int): |
paulb@145 | 384 | return bool() |
paulb@145 | 385 | else: |
paulb@186 | 386 | return NotImplemented |
paulb@145 | 387 | |
paulb@145 | 388 | def __le__(self, other): |
paulb@145 | 389 | if isinstance(other, int): |
paulb@145 | 390 | return bool() |
paulb@145 | 391 | else: |
paulb@186 | 392 | return NotImplemented |
paulb@145 | 393 | |
paulb@145 | 394 | def __ge__(self, other): |
paulb@145 | 395 | if isinstance(other, int): |
paulb@145 | 396 | return bool() |
paulb@145 | 397 | else: |
paulb@186 | 398 | return NotImplemented |
paulb@145 | 399 | |
paulb@145 | 400 | def __eq__(self, other): |
paulb@145 | 401 | if isinstance(other, int): |
paulb@145 | 402 | return bool() |
paulb@145 | 403 | else: |
paulb@186 | 404 | return NotImplemented |
paulb@145 | 405 | |
paulb@145 | 406 | def __ne__(self, other): |
paulb@145 | 407 | if isinstance(other, int): |
paulb@145 | 408 | return bool() |
paulb@145 | 409 | else: |
paulb@186 | 410 | return NotImplemented |
paulb@145 | 411 | |
paulb@145 | 412 | def __neg__(self): |
paulb@22 | 413 | return int() |
paulb@22 | 414 | |
paulb@145 | 415 | def __pos__(self): |
paulb@145 | 416 | return self |
paulb@145 | 417 | |
paulb@22 | 418 | def __str__(self): |
paulb@145 | 419 | return str() |
paulb@22 | 420 | |
paulb@130 | 421 | def __bool__(self): |
paulb@145 | 422 | return self != 0 |
paulb@22 | 423 | |
paulb@22 | 424 | class list: |
paulb@89 | 425 | def __init__(self, *args): |
paulb@89 | 426 | for arg in args: |
paulb@89 | 427 | self.append(arg) |
paulb@22 | 428 | |
paulb@22 | 429 | def __getitem__(self, index): |
paulb@167 | 430 | if -len(self) <= index < len(self): |
paulb@167 | 431 | return self.value |
paulb@22 | 432 | else: |
paulb@167 | 433 | raise IndexError, index |
paulb@22 | 434 | |
paulb@22 | 435 | def __setitem__(self, index, value): |
paulb@167 | 436 | if -len(self) <= index < len(self): |
paulb@167 | 437 | self.value = value |
paulb@22 | 438 | else: |
paulb@167 | 439 | raise IndexError, index |
paulb@22 | 440 | |
paulb@22 | 441 | def __getslice__(self, start, end=None): |
paulb@167 | 442 | if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end: |
paulb@167 | 443 | return list(self.value) |
paulb@167 | 444 | else: |
paulb@167 | 445 | raise IndexError, index |
paulb@22 | 446 | |
paulb@22 | 447 | def __setslice__(self, start, end, slice): |
paulb@167 | 448 | if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end: |
paulb@167 | 449 | for value in slice: |
paulb@167 | 450 | self.value = value |
paulb@167 | 451 | else: |
paulb@167 | 452 | raise IndexError, index |
paulb@22 | 453 | |
paulb@22 | 454 | def append(self, value): |
paulb@167 | 455 | self.value = value |
paulb@22 | 456 | |
paulb@22 | 457 | def __len__(self): |
paulb@167 | 458 | return int() |
paulb@22 | 459 | |
paulb@22 | 460 | def __add__(self, other): |
paulb@22 | 461 | for value in other: |
paulb@167 | 462 | self.value = value |
paulb@167 | 463 | return self |
paulb@22 | 464 | |
paulb@167 | 465 | __iadd__ = __add__ |
paulb@97 | 466 | |
paulb@22 | 467 | def __str__(self): |
paulb@167 | 468 | return str() |
paulb@22 | 469 | |
paulb@22 | 470 | def __iter__(self): |
paulb@22 | 471 | return listiterator(self) |
paulb@22 | 472 | |
paulb@130 | 473 | def __bool__(self): |
paulb@22 | 474 | return self.__len__() != 0 |
paulb@22 | 475 | |
paulb@22 | 476 | class listiterator: |
paulb@22 | 477 | def __init__(self, l): |
paulb@22 | 478 | self.l = l |
paulb@167 | 479 | self.index = 0 |
paulb@22 | 480 | |
paulb@22 | 481 | def next(self): |
paulb@167 | 482 | if self.index < len(self.l): |
paulb@167 | 483 | self.index += 1 |
paulb@167 | 484 | return self.l[self.index] |
paulb@22 | 485 | else: |
paulb@55 | 486 | raise StopIteration |
paulb@22 | 487 | |
paulb@130 | 488 | def __bool__(self): |
paulb@130 | 489 | return bool() |
paulb@22 | 490 | |
paulb@145 | 491 | class long: |
paulb@198 | 492 | __atomic__ = 1 |
paulb@198 | 493 | |
paulb@200 | 494 | def __init__(self, number_or_string=None): |
paulb@200 | 495 | pass |
paulb@200 | 496 | |
paulb@145 | 497 | def __iadd__(self, other): |
paulb@145 | 498 | if isinstance(other, int): |
paulb@145 | 499 | return long() |
paulb@145 | 500 | elif isinstance(other, long): |
paulb@145 | 501 | return long() |
paulb@145 | 502 | else: |
paulb@145 | 503 | raise TypeError |
paulb@145 | 504 | |
paulb@145 | 505 | def __isub__(self, other): |
paulb@145 | 506 | if isinstance(other, int): |
paulb@145 | 507 | return long() |
paulb@145 | 508 | elif isinstance(other, long): |
paulb@145 | 509 | return long() |
paulb@145 | 510 | else: |
paulb@145 | 511 | raise TypeError |
paulb@145 | 512 | |
paulb@145 | 513 | def __add__(self, other): |
paulb@145 | 514 | if isinstance(other, int): |
paulb@145 | 515 | return long() |
paulb@145 | 516 | elif isinstance(other, long): |
paulb@145 | 517 | return long() |
paulb@145 | 518 | else: |
paulb@145 | 519 | raise TypeError |
paulb@145 | 520 | |
paulb@145 | 521 | def __radd__(self, other): |
paulb@145 | 522 | if isinstance(other, int): |
paulb@145 | 523 | return long() |
paulb@145 | 524 | elif isinstance(other, long): |
paulb@145 | 525 | return long() |
paulb@145 | 526 | else: |
paulb@145 | 527 | raise TypeError |
paulb@145 | 528 | |
paulb@145 | 529 | def __sub__(self, other): |
paulb@145 | 530 | if isinstance(other, int): |
paulb@145 | 531 | return long() |
paulb@145 | 532 | elif isinstance(other, long): |
paulb@145 | 533 | return long() |
paulb@145 | 534 | else: |
paulb@145 | 535 | raise TypeError |
paulb@145 | 536 | |
paulb@145 | 537 | def __rsub__(self, other): |
paulb@145 | 538 | if isinstance(other, int): |
paulb@145 | 539 | return long() |
paulb@145 | 540 | elif isinstance(other, long): |
paulb@145 | 541 | return long() |
paulb@145 | 542 | else: |
paulb@145 | 543 | raise TypeError |
paulb@145 | 544 | |
paulb@145 | 545 | def __lt__(self, other): |
paulb@145 | 546 | if isinstance(other, int): |
paulb@145 | 547 | return bool() |
paulb@145 | 548 | elif isinstance(other, long): |
paulb@145 | 549 | return bool() |
paulb@145 | 550 | else: |
paulb@186 | 551 | return NotImplemented |
paulb@145 | 552 | |
paulb@145 | 553 | def __gt__(self, other): |
paulb@145 | 554 | if isinstance(other, int): |
paulb@145 | 555 | return bool() |
paulb@145 | 556 | elif isinstance(other, long): |
paulb@145 | 557 | return bool() |
paulb@145 | 558 | else: |
paulb@186 | 559 | return NotImplemented |
paulb@145 | 560 | |
paulb@145 | 561 | def __le__(self, other): |
paulb@145 | 562 | if isinstance(other, int): |
paulb@145 | 563 | return bool() |
paulb@145 | 564 | elif isinstance(other, long): |
paulb@145 | 565 | return bool() |
paulb@145 | 566 | else: |
paulb@186 | 567 | return NotImplemented |
paulb@145 | 568 | |
paulb@145 | 569 | def __ge__(self, other): |
paulb@145 | 570 | if isinstance(other, int): |
paulb@145 | 571 | return bool() |
paulb@145 | 572 | elif isinstance(other, long): |
paulb@145 | 573 | return bool() |
paulb@145 | 574 | else: |
paulb@186 | 575 | return NotImplemented |
paulb@145 | 576 | |
paulb@145 | 577 | def __eq__(self, other): |
paulb@145 | 578 | if isinstance(other, int): |
paulb@145 | 579 | return bool() |
paulb@145 | 580 | elif isinstance(other, long): |
paulb@145 | 581 | return bool() |
paulb@145 | 582 | else: |
paulb@186 | 583 | return NotImplemented |
paulb@145 | 584 | |
paulb@145 | 585 | def __ne__(self, other): |
paulb@145 | 586 | if isinstance(other, int): |
paulb@145 | 587 | return bool() |
paulb@145 | 588 | elif isinstance(other, long): |
paulb@145 | 589 | return bool() |
paulb@145 | 590 | else: |
paulb@186 | 591 | return NotImplemented |
paulb@145 | 592 | |
paulb@145 | 593 | def __neg__(self): |
paulb@145 | 594 | return long() |
paulb@145 | 595 | |
paulb@145 | 596 | def __pos__(self): |
paulb@145 | 597 | return self |
paulb@145 | 598 | |
paulb@145 | 599 | def __str__(self): |
paulb@145 | 600 | return str() |
paulb@145 | 601 | |
paulb@145 | 602 | def __bool__(self): |
paulb@145 | 603 | return self != 0 |
paulb@145 | 604 | |
paulb@145 | 605 | class none: |
paulb@198 | 606 | __atomic__ = 1 |
paulb@198 | 607 | |
paulb@145 | 608 | def __bool__(self): |
paulb@145 | 609 | return False |
paulb@145 | 610 | |
paulb@145 | 611 | def __str__(self): |
paulb@145 | 612 | return "None" |
paulb@145 | 613 | |
paulb@201 | 614 | class slice: |
paulb@201 | 615 | def __init__(self, start_or_end, end=None, step=None): |
paulb@201 | 616 | if end is None: |
paulb@201 | 617 | self.start = 0 |
paulb@201 | 618 | self.end = start_or_end |
paulb@201 | 619 | else: |
paulb@201 | 620 | self.start = start_or_end |
paulb@201 | 621 | self.end = end |
paulb@201 | 622 | self.step = step |
paulb@201 | 623 | |
paulb@145 | 624 | class str: |
paulb@198 | 625 | __atomic__ = 1 |
paulb@198 | 626 | |
paulb@145 | 627 | def __init__(self, x=None): |
paulb@145 | 628 | x.__str__() |
paulb@145 | 629 | |
paulb@145 | 630 | def __add__(self, other): |
paulb@145 | 631 | if isinstance(other, str): |
paulb@145 | 632 | return str() |
paulb@145 | 633 | else: |
paulb@145 | 634 | raise TypeError |
paulb@145 | 635 | |
paulb@145 | 636 | def __radd__(self, other): |
paulb@145 | 637 | if isinstance(other, str): |
paulb@145 | 638 | return str() |
paulb@145 | 639 | else: |
paulb@145 | 640 | raise TypeError |
paulb@145 | 641 | |
paulb@200 | 642 | def __mod__(self, other): |
paulb@200 | 643 | |
paulb@200 | 644 | "The format operator for strings." |
paulb@200 | 645 | |
paulb@200 | 646 | return str() |
paulb@200 | 647 | |
paulb@145 | 648 | def __len__(self): |
paulb@145 | 649 | return int() |
paulb@145 | 650 | |
paulb@145 | 651 | def __str__(self): |
paulb@145 | 652 | return self |
paulb@145 | 653 | |
paulb@145 | 654 | def __bool__(self): |
paulb@145 | 655 | return self.__len__() != 0 |
paulb@145 | 656 | |
paulb@145 | 657 | def join(self, l): |
paulb@145 | 658 | total = 0 |
paulb@145 | 659 | first = 1 |
paulb@145 | 660 | self_len = self.__len__() |
paulb@145 | 661 | for i in l: |
paulb@145 | 662 | if not first: |
paulb@145 | 663 | total += self_len |
paulb@145 | 664 | total += len(str(i)) |
paulb@145 | 665 | first = 0 |
paulb@145 | 666 | b = buffer(total) |
paulb@145 | 667 | first = 1 |
paulb@145 | 668 | for i in l: |
paulb@145 | 669 | if not first: |
paulb@145 | 670 | b.append(self) |
paulb@145 | 671 | b.append(str(i)) |
paulb@145 | 672 | first = 0 |
paulb@145 | 673 | s = str(b) |
paulb@145 | 674 | return s |
paulb@145 | 675 | |
paulb@22 | 676 | class tuple: |
paulb@66 | 677 | def __init__(self, *args): |
paulb@66 | 678 | for arg in args: |
paulb@66 | 679 | self.append(arg) |
paulb@22 | 680 | |
paulb@22 | 681 | def __getitem__(self, index): |
paulb@167 | 682 | if -len(self) <= index < len(self): |
paulb@167 | 683 | return self.value |
paulb@22 | 684 | else: |
paulb@167 | 685 | raise IndexError, index |
paulb@167 | 686 | |
paulb@167 | 687 | def __setitem__(self, index, value): |
paulb@167 | 688 | if -len(self) <= index < len(self): |
paulb@167 | 689 | self.value = value |
paulb@167 | 690 | else: |
paulb@167 | 691 | raise IndexError, index |
paulb@22 | 692 | |
paulb@22 | 693 | def __getslice__(self, start, end=None): |
paulb@167 | 694 | if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end: |
paulb@167 | 695 | return list(self.value) |
paulb@167 | 696 | else: |
paulb@167 | 697 | raise IndexError, index |
paulb@167 | 698 | |
paulb@167 | 699 | def __setslice__(self, start, end, slice): |
paulb@167 | 700 | if -len(self) <= start < len(self) and -len(self) <= end < len(self) and start <= end: |
paulb@167 | 701 | for value in slice: |
paulb@167 | 702 | self.value = value |
paulb@167 | 703 | else: |
paulb@167 | 704 | raise IndexError, index |
paulb@22 | 705 | |
paulb@22 | 706 | # NOTE: The append method should be internal at most. |
paulb@22 | 707 | |
paulb@22 | 708 | def append(self, value): |
paulb@167 | 709 | self.value = value |
paulb@22 | 710 | |
paulb@22 | 711 | def __len__(self): |
paulb@167 | 712 | return int() |
paulb@22 | 713 | |
paulb@22 | 714 | def __add__(self, other): |
paulb@22 | 715 | for value in other: |
paulb@167 | 716 | self.value = value |
paulb@167 | 717 | return self |
paulb@22 | 718 | |
paulb@22 | 719 | def __str__(self): |
paulb@167 | 720 | return str() |
paulb@22 | 721 | |
paulb@22 | 722 | def __iter__(self): |
paulb@22 | 723 | return tupleiterator(self) |
paulb@22 | 724 | |
paulb@130 | 725 | def __bool__(self): |
paulb@22 | 726 | return self.__len__() != 0 |
paulb@22 | 727 | |
paulb@22 | 728 | class tupleiterator: |
paulb@22 | 729 | def __init__(self, l): |
paulb@22 | 730 | self.l = l |
paulb@167 | 731 | self.index = 0 |
paulb@22 | 732 | |
paulb@22 | 733 | def next(self): |
paulb@167 | 734 | if self.index < len(self.l): |
paulb@167 | 735 | self.index += 1 |
paulb@167 | 736 | return self.l[self.index] |
paulb@22 | 737 | else: |
paulb@55 | 738 | raise StopIteration |
paulb@22 | 739 | |
paulb@130 | 740 | def __bool__(self): |
paulb@130 | 741 | return bool() |
paulb@22 | 742 | |
paulb@105 | 743 | class xrange: |
paulb@200 | 744 | def __init__(self, start_or_end, end=None, step=1): |
paulb@200 | 745 | if end is None: |
paulb@200 | 746 | self.start = 0 |
paulb@200 | 747 | self.end = start_or_end |
paulb@200 | 748 | else: |
paulb@200 | 749 | self.start = start_or_end |
paulb@200 | 750 | self.end = end |
paulb@105 | 751 | self.step = step |
paulb@200 | 752 | self.current = self.start |
paulb@105 | 753 | |
paulb@105 | 754 | def __iter__(self): |
paulb@105 | 755 | return self |
paulb@105 | 756 | |
paulb@105 | 757 | def next(self): |
paulb@105 | 758 | if self.current >= self.end: |
paulb@105 | 759 | raise StopIteration |
paulb@105 | 760 | current = self.current |
paulb@105 | 761 | self.current += self.step |
paulb@105 | 762 | return current |
paulb@105 | 763 | |
paulb@145 | 764 | class Exception: |
paulb@167 | 765 | def __init__(self, *args): |
paulb@167 | 766 | pass |
paulb@145 | 767 | |
paulb@145 | 768 | class AttributeError(Exception): |
paulb@198 | 769 | __atomic__ = 1 |
paulb@145 | 770 | |
paulb@145 | 771 | class IndexError(Exception): |
paulb@198 | 772 | __atomic__ = 1 |
paulb@145 | 773 | |
paulb@145 | 774 | class StopIteration(Exception): |
paulb@198 | 775 | __atomic__ = 1 |
paulb@145 | 776 | |
paulb@145 | 777 | class TypeError(Exception): |
paulb@198 | 778 | __atomic__ = 1 |
paulb@145 | 779 | |
paulb@186 | 780 | class NotImplementedType: |
paulb@198 | 781 | __atomic__ = 1 |
paulb@186 | 782 | |
paulb@105 | 783 | # General functions. |
paulb@105 | 784 | |
paulb@22 | 785 | def isinstance(obj, cls): |
paulb@130 | 786 | return bool() |
paulb@22 | 787 | |
paulb@22 | 788 | def issubclass(cls1, cls2): |
paulb@130 | 789 | return bool() |
paulb@22 | 790 | |
paulb@22 | 791 | def len(x): |
paulb@22 | 792 | return x.__len__() |
paulb@22 | 793 | |
paulb@22 | 794 | def max(*l): |
paulb@22 | 795 | max_so_far = l[0] |
paulb@22 | 796 | for i in l[1:]: |
paulb@22 | 797 | if i > max_so_far: |
paulb@22 | 798 | max_so_far = i |
paulb@22 | 799 | return max_so_far |
paulb@22 | 800 | |
paulb@105 | 801 | def range(start, end, step=None): |
paulb@105 | 802 | if start == end: |
paulb@105 | 803 | return [] |
paulb@105 | 804 | elif start > end: |
paulb@105 | 805 | step = step or 1 |
paulb@105 | 806 | i = start |
paulb@105 | 807 | result = [] |
paulb@105 | 808 | while i < end: |
paulb@105 | 809 | result.append(i) |
paulb@105 | 810 | i += 1 |
paulb@105 | 811 | return result |
paulb@105 | 812 | else: |
paulb@105 | 813 | step = step or -1 |
paulb@105 | 814 | i = start |
paulb@105 | 815 | result = [] |
paulb@105 | 816 | while i > end: |
paulb@105 | 817 | result.append(i) |
paulb@105 | 818 | i -= 1 |
paulb@105 | 819 | return result |
paulb@105 | 820 | |
paulb@129 | 821 | # Special values. |
paulb@22 | 822 | |
paulb@130 | 823 | True = bool() |
paulb@130 | 824 | False = bool() |
paulb@22 | 825 | None = none() |
paulb@145 | 826 | stdin = file() |
paulb@145 | 827 | stdout = file() |
paulb@145 | 828 | stderr = file() |
paulb@186 | 829 | NotImplemented = NotImplementedType() |
paulb@22 | 830 | |
paulb@22 | 831 | # Special functions. These all operate on references at run-time. |
paulb@22 | 832 | |
paulb@22 | 833 | def __is__(a, b): |
paulb@130 | 834 | return bool() |
paulb@22 | 835 | |
paulb@22 | 836 | def __not__(a): |
paulb@130 | 837 | return bool() |
paulb@22 | 838 | |
paulb@22 | 839 | # vim: tabstop=4 expandtab shiftwidth=4 |