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