paul@6 | 1 | #!/usr/bin/env python |
paul@6 | 2 | |
paul@6 | 3 | """ |
paul@6 | 4 | String objects. |
paul@6 | 5 | |
paul@514 | 6 | Copyright (C) 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk> |
paul@6 | 7 | |
paul@6 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@6 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@6 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@6 | 11 | version. |
paul@6 | 12 | |
paul@6 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@6 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@6 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@6 | 16 | details. |
paul@6 | 17 | |
paul@6 | 18 | You should have received a copy of the GNU General Public License along with |
paul@6 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@6 | 20 | """ |
paul@6 | 21 | |
paul@303 | 22 | from __builtins__.operator import _negate |
paul@459 | 23 | from __builtins__.sequence import hashable, itemaccess |
paul@384 | 24 | from __builtins__.types import check_int |
paul@534 | 25 | from native import str_add, str_lt, str_gt, str_eq, str_len, str_ord, \ |
paul@534 | 26 | str_nonempty, str_substr |
paul@6 | 27 | |
paul@515 | 28 | WHITESPACE = (" ", "\f", "\n", "\r", "\t") |
paul@515 | 29 | |
paul@459 | 30 | class basestring(hashable): |
paul@289 | 31 | |
paul@289 | 32 | "The base class for all strings." |
paul@289 | 33 | |
paul@390 | 34 | def __init__(self, other=None): |
paul@6 | 35 | |
paul@390 | 36 | "Initialise the string, perhaps from 'other'." |
paul@6 | 37 | |
paul@289 | 38 | # Note the __data__ member. Since strings are either initialised from |
paul@289 | 39 | # literals or converted using routines defined for other types, no form |
paul@289 | 40 | # of actual initialisation is performed here. |
paul@6 | 41 | |
paul@390 | 42 | # NOTE: Cannot perform "other and other.__data__ or None" since the |
paul@390 | 43 | # NOTE: __data__ attribute is not a normal attribute. |
paul@390 | 44 | |
paul@390 | 45 | if other: |
paul@390 | 46 | self.__data__ = other.__data__ |
paul@390 | 47 | else: |
paul@390 | 48 | self.__data__ = None |
paul@6 | 49 | |
paul@360 | 50 | # Note the __key__ member. This is also initialised statically. Where |
paul@360 | 51 | # a string is the same as an attribute name, the __key__ member contains |
paul@360 | 52 | # attribute position and code details. |
paul@360 | 53 | |
paul@390 | 54 | if other: |
paul@390 | 55 | self.__key__ = other.__key__ |
paul@390 | 56 | else: |
paul@390 | 57 | self.__key__ = None |
paul@360 | 58 | |
paul@518 | 59 | # Internal methods. |
paul@300 | 60 | |
paul@303 | 61 | def _binary_op(self, op, other): |
paul@303 | 62 | |
paul@396 | 63 | "Perform 'op' on this object and 'other' if appropriate." |
paul@396 | 64 | |
paul@396 | 65 | # Refuse to operate on specialisations of this class. |
paul@396 | 66 | |
paul@396 | 67 | if self.__class__ is not other.__class__: |
paul@396 | 68 | return NotImplemented |
paul@396 | 69 | |
paul@396 | 70 | # Otherwise, perform the operation on the operands' data. |
paul@396 | 71 | |
paul@396 | 72 | else: |
paul@396 | 73 | return op(self.__data__, other.__data__) |
paul@303 | 74 | |
paul@396 | 75 | def _binary_op_rev(self, op, other): |
paul@396 | 76 | |
paul@396 | 77 | "Perform 'op' on 'other' and this object if appropriate." |
paul@396 | 78 | |
paul@396 | 79 | # Refuse to operate on specialisations of this class. |
paul@396 | 80 | |
paul@396 | 81 | if self.__class__ is not other.__class__: |
paul@396 | 82 | return NotImplemented |
paul@396 | 83 | |
paul@396 | 84 | # Otherwise, perform the operation on the operands' data. |
paul@396 | 85 | |
paul@303 | 86 | else: |
paul@396 | 87 | return op(other.__data__, self.__data__) |
paul@303 | 88 | |
paul@518 | 89 | def _quote(self, quote): |
paul@518 | 90 | |
paul@518 | 91 | "Return a quoted representation of this string." |
paul@518 | 92 | |
paul@518 | 93 | b = buffer([quote]) |
paul@518 | 94 | i = last = 0 |
paul@518 | 95 | end = self.__len__() |
paul@518 | 96 | |
paul@518 | 97 | while i < end: |
paul@518 | 98 | c = self[i] |
paul@519 | 99 | |
paul@519 | 100 | # Handle quotes before anything else. |
paul@519 | 101 | |
paul@519 | 102 | if c == quote: |
paul@519 | 103 | b.append("\\") |
paul@519 | 104 | b.append(quote) |
paul@519 | 105 | i += 1 |
paul@519 | 106 | last = i |
paul@519 | 107 | continue |
paul@518 | 108 | |
paul@518 | 109 | # Extended unquoted text. |
paul@518 | 110 | |
paul@519 | 111 | n = ord(c) |
paul@519 | 112 | |
paul@518 | 113 | if 32 <= n < 128: |
paul@518 | 114 | i += 1 |
paul@518 | 115 | continue |
paul@518 | 116 | |
paul@518 | 117 | # Before quoting, emit unquoted text. |
paul@518 | 118 | |
paul@518 | 119 | b.append(self[last:i]) |
paul@518 | 120 | |
paul@518 | 121 | # Add quoted value. |
paul@518 | 122 | |
paul@519 | 123 | if c == "\t": |
paul@518 | 124 | b.append("\\t") |
paul@518 | 125 | elif c == "\n": |
paul@518 | 126 | b.append("\\n") |
paul@518 | 127 | elif c == "\r": |
paul@518 | 128 | b.append("\\r") |
paul@518 | 129 | else: |
paul@518 | 130 | if n < 0: |
paul@518 | 131 | n += 256 |
paul@518 | 132 | b.append("\\x") |
paul@518 | 133 | x = hex(n, "") |
paul@518 | 134 | if len(x) < 2: |
paul@518 | 135 | b.append("0") |
paul@518 | 136 | b.append(x) |
paul@518 | 137 | |
paul@518 | 138 | i += 1 |
paul@518 | 139 | last = i |
paul@518 | 140 | |
paul@518 | 141 | # Emit remaining unquoted text. |
paul@518 | 142 | |
paul@518 | 143 | b.append(self[last:]) |
paul@518 | 144 | b.append(quote) |
paul@518 | 145 | return str(b) |
paul@518 | 146 | |
paul@518 | 147 | def bytelength(self): |
paul@518 | 148 | |
paul@518 | 149 | "Return the number of bytes in this string." |
paul@518 | 150 | |
paul@518 | 151 | return str_len(self.__data__) |
paul@518 | 152 | |
paul@518 | 153 | # General type methods. |
paul@518 | 154 | |
paul@518 | 155 | def __bool__(self): |
paul@518 | 156 | |
paul@518 | 157 | "Return whether the string provides any data." |
paul@518 | 158 | |
paul@518 | 159 | return str_nonempty(self.__data__) |
paul@518 | 160 | |
paul@518 | 161 | def __contains__(self, value): |
paul@518 | 162 | |
paul@518 | 163 | "Return whether this string contains 'value'." |
paul@518 | 164 | |
paul@518 | 165 | return self.find(value) != -1 |
paul@518 | 166 | |
paul@518 | 167 | def __hash__(self): |
paul@518 | 168 | |
paul@518 | 169 | "Return a value for hashing purposes." |
paul@518 | 170 | |
paul@518 | 171 | return self._hashvalue(ord) |
paul@518 | 172 | |
paul@518 | 173 | __len__ = bytelength |
paul@518 | 174 | |
paul@518 | 175 | def __repr__(self): |
paul@518 | 176 | |
paul@518 | 177 | "Return a program representation." |
paul@518 | 178 | |
paul@518 | 179 | return self._quote('"') |
paul@518 | 180 | |
paul@518 | 181 | def __str__(self): |
paul@518 | 182 | |
paul@518 | 183 | "Return a string representation." |
paul@518 | 184 | |
paul@518 | 185 | return self |
paul@518 | 186 | |
paul@518 | 187 | # Operator methods. |
paul@518 | 188 | |
paul@6 | 189 | def __iadd__(self, other): |
paul@289 | 190 | |
paul@303 | 191 | "Return a string combining this string with 'other'." |
paul@289 | 192 | |
paul@356 | 193 | return self._binary_op(str_add, other) |
paul@6 | 194 | |
paul@396 | 195 | __add__ = __iadd__ |
paul@396 | 196 | |
paul@396 | 197 | def __radd__(self, other): |
paul@396 | 198 | |
paul@396 | 199 | "Return a string combining this string with 'other'." |
paul@396 | 200 | |
paul@396 | 201 | return self._binary_op_rev(str_add, other) |
paul@6 | 202 | |
paul@518 | 203 | def __mod__(self, other): pass |
paul@518 | 204 | def __rmod__(self, other): pass |
paul@518 | 205 | |
paul@514 | 206 | def __mul__(self, other): |
paul@514 | 207 | |
paul@514 | 208 | "Multiply the string by 'other'." |
paul@514 | 209 | |
paul@514 | 210 | b = buffer() |
paul@514 | 211 | |
paul@514 | 212 | while other > 0: |
paul@514 | 213 | b.append(self) |
paul@514 | 214 | other -= 1 |
paul@514 | 215 | |
paul@514 | 216 | return str(b) |
paul@514 | 217 | |
paul@514 | 218 | __rmul__ = __mul__ |
paul@514 | 219 | |
paul@518 | 220 | # Comparison methods. |
paul@518 | 221 | |
paul@518 | 222 | def __eq__(self, other): |
paul@518 | 223 | |
paul@518 | 224 | "Return whether this string is equal to 'other'." |
paul@6 | 225 | |
paul@518 | 226 | return self._binary_op(str_eq, other) |
paul@518 | 227 | |
paul@518 | 228 | def __ge__(self, other): |
paul@289 | 229 | |
paul@518 | 230 | "Return whether this string is greater than or equal to 'other'." |
paul@289 | 231 | |
paul@518 | 232 | return _negate(self.__lt__(other)) |
paul@6 | 233 | |
paul@6 | 234 | def __gt__(self, other): |
paul@289 | 235 | |
paul@303 | 236 | "Return whether this string is greater than 'other'." |
paul@289 | 237 | |
paul@356 | 238 | return self._binary_op(str_gt, other) |
paul@6 | 239 | |
paul@6 | 240 | def __le__(self, other): |
paul@289 | 241 | |
paul@303 | 242 | "Return whether this string is less than or equal to 'other'." |
paul@289 | 243 | |
paul@6 | 244 | return _negate(self.__gt__(other)) |
paul@6 | 245 | |
paul@518 | 246 | def __lt__(self, other): |
paul@6 | 247 | |
paul@518 | 248 | "Return whether this string is less than 'other'." |
paul@289 | 249 | |
paul@518 | 250 | return self._binary_op(str_lt, other) |
paul@6 | 251 | |
paul@6 | 252 | def __ne__(self, other): |
paul@289 | 253 | |
paul@303 | 254 | "Return whether this string is not equal to 'other'." |
paul@289 | 255 | |
paul@6 | 256 | return _negate(self.__eq__(other)) |
paul@6 | 257 | |
paul@518 | 258 | # String-specific methods. |
paul@435 | 259 | |
paul@534 | 260 | def __ord__(self): |
paul@534 | 261 | |
paul@534 | 262 | "Return the value of the string, if only a single character." |
paul@534 | 263 | |
paul@534 | 264 | if self.__len__() == 1: |
paul@534 | 265 | return str_ord(self.__data__) |
paul@534 | 266 | else: |
paul@534 | 267 | raise ValueError, self |
paul@534 | 268 | |
paul@413 | 269 | def endswith(self, s): |
paul@413 | 270 | |
paul@413 | 271 | "Return whether this string ends with 's'." |
paul@413 | 272 | |
paul@413 | 273 | return self[-s.__len__():] == s |
paul@413 | 274 | |
paul@413 | 275 | def find(self, sub, start=None, end=None): |
paul@413 | 276 | |
paul@413 | 277 | """ |
paul@515 | 278 | Find 'sub' in the string if it occurs from or after the 'start' position |
paul@515 | 279 | (or 0, if omitted) and before the 'end' position (or the end of the |
paul@515 | 280 | string, if omitted), returning the earliest occurrence or -1 if 'sub' is |
paul@515 | 281 | not present. |
paul@413 | 282 | """ |
paul@413 | 283 | |
paul@413 | 284 | sublen = sub.__len__() |
paul@413 | 285 | |
paul@515 | 286 | if end is None: |
paul@515 | 287 | end = self.__len__() |
paul@413 | 288 | |
paul@517 | 289 | end -= sublen |
paul@517 | 290 | |
paul@517 | 291 | i = start or 0 |
paul@517 | 292 | |
paul@517 | 293 | while i <= end: |
paul@413 | 294 | if sub == self[i:i+sublen]: |
paul@413 | 295 | return i |
paul@413 | 296 | i += 1 |
paul@413 | 297 | |
paul@413 | 298 | return -1 |
paul@413 | 299 | |
paul@413 | 300 | def index(self, sub, start=None, end=None): |
paul@413 | 301 | |
paul@413 | 302 | """ |
paul@413 | 303 | Find 'sub' in the string, starting at 'start' (or 0, if omitted), ending |
paul@413 | 304 | at 'end' (or the end of the string, if omitted), raising ValueError if |
paul@413 | 305 | 'sub' is not present. |
paul@413 | 306 | """ |
paul@413 | 307 | |
paul@413 | 308 | i = self.find(sub, start, end) |
paul@413 | 309 | |
paul@413 | 310 | if i == -1: |
paul@413 | 311 | raise ValueError(sub) |
paul@413 | 312 | else: |
paul@413 | 313 | return i |
paul@342 | 314 | |
paul@342 | 315 | def join(self, l): |
paul@342 | 316 | |
paul@342 | 317 | "Join the elements in 'l' with this string." |
paul@342 | 318 | |
paul@342 | 319 | # Empty strings just cause the list elements to be concatenated. |
paul@342 | 320 | |
paul@342 | 321 | if not self.__bool__(): |
paul@342 | 322 | return str(buffer(l)) |
paul@342 | 323 | |
paul@342 | 324 | # Non-empty strings join the elements together in a buffer. |
paul@342 | 325 | |
paul@342 | 326 | b = buffer() |
paul@342 | 327 | first = True |
paul@342 | 328 | |
paul@342 | 329 | for s in l: |
paul@342 | 330 | if first: |
paul@342 | 331 | first = False |
paul@342 | 332 | else: |
paul@342 | 333 | b.append(self) |
paul@342 | 334 | b.append(s) |
paul@342 | 335 | |
paul@342 | 336 | return str(b) |
paul@342 | 337 | |
paul@6 | 338 | def lower(self): pass |
paul@517 | 339 | |
paul@517 | 340 | def lstrip(self, chars=None): |
paul@517 | 341 | |
paul@517 | 342 | """ |
paul@517 | 343 | Strip any of the given 'chars' from the start of the string, or strip |
paul@517 | 344 | whitespace characters is 'chars' is omitted or None. |
paul@517 | 345 | """ |
paul@517 | 346 | |
paul@517 | 347 | if chars is not None and not chars: |
paul@517 | 348 | return self |
paul@517 | 349 | |
paul@517 | 350 | i = 0 |
paul@517 | 351 | end = self.__len__() |
paul@517 | 352 | |
paul@517 | 353 | while i < end and self[i] in (chars or WHITESPACE): |
paul@517 | 354 | i += 1 |
paul@517 | 355 | |
paul@517 | 356 | return self[i:] |
paul@517 | 357 | |
paul@6 | 358 | def replace(self, old, new, count=None): pass |
paul@517 | 359 | |
paul@515 | 360 | def rfind(self, sub, start=None, end=None): |
paul@515 | 361 | |
paul@515 | 362 | """ |
paul@515 | 363 | Find 'sub' in the string if it occurs from or after the 'start' position |
paul@515 | 364 | (or 0, if omitted) and before the 'end' position (or the end of the |
paul@515 | 365 | string, if omitted), returning the latest occurrence or -1 if 'sub' is |
paul@515 | 366 | not present. |
paul@515 | 367 | """ |
paul@515 | 368 | |
paul@515 | 369 | sublen = sub.__len__() |
paul@515 | 370 | |
paul@515 | 371 | start = start or 0 |
paul@515 | 372 | |
paul@515 | 373 | if end is None: |
paul@515 | 374 | end = self.__len__() |
paul@515 | 375 | |
paul@515 | 376 | i = end - sublen |
paul@515 | 377 | |
paul@515 | 378 | while i >= start: |
paul@515 | 379 | if sub == self[i:i+sublen]: |
paul@515 | 380 | return i |
paul@515 | 381 | i -= 1 |
paul@515 | 382 | |
paul@515 | 383 | return -1 |
paul@515 | 384 | |
paul@517 | 385 | def rsplit(self, sep=None, maxsplit=None): |
paul@517 | 386 | |
paul@517 | 387 | """ |
paul@517 | 388 | Split the string using the given 'sep' as separator (or any whitespace |
paul@517 | 389 | character if omitted or specified as None), splitting at most 'maxsplit' |
paul@517 | 390 | times (or as many times as is possible if omitted or specified as None). |
paul@517 | 391 | Where 'maxsplit' is given, the number of split points is counted from |
paul@517 | 392 | the end of the string. |
paul@517 | 393 | """ |
paul@517 | 394 | |
paul@517 | 395 | if not maxsplit: |
paul@517 | 396 | return self.split(sep, maxsplit) |
paul@517 | 397 | |
paul@517 | 398 | if sep is not None and not sep: |
paul@517 | 399 | raise ValueError, sep |
paul@517 | 400 | |
paul@517 | 401 | seplen = sep and len(sep) or 1 |
paul@517 | 402 | start = seplen |
paul@517 | 403 | splits = 0 |
paul@517 | 404 | |
paul@517 | 405 | l = [] |
paul@517 | 406 | i = last = self.__len__() |
paul@517 | 407 | |
paul@517 | 408 | while i >= start and (maxsplit is None or splits < maxsplit): |
paul@517 | 409 | |
paul@517 | 410 | # Find any specified separator. |
paul@517 | 411 | |
paul@517 | 412 | if sep and self[i-seplen:i] == sep: |
paul@517 | 413 | l.insert(0, self[i:last]) |
paul@517 | 414 | i -= seplen |
paul@517 | 415 | last = i |
paul@517 | 416 | splits += 1 |
paul@517 | 417 | |
paul@517 | 418 | # Find any whitespace character and skip adjacent characters. |
paul@517 | 419 | |
paul@517 | 420 | elif not sep and self[i-1] in WHITESPACE: |
paul@517 | 421 | l.insert(0, self[i:last]) |
paul@517 | 422 | while i > start: |
paul@517 | 423 | i -= 1 |
paul@517 | 424 | if self[i-1] not in WHITESPACE: |
paul@517 | 425 | break |
paul@517 | 426 | else: |
paul@517 | 427 | break |
paul@517 | 428 | last = i |
paul@517 | 429 | splits += 1 |
paul@517 | 430 | |
paul@517 | 431 | # Check the next character. |
paul@517 | 432 | |
paul@517 | 433 | else: |
paul@517 | 434 | i -= 1 |
paul@517 | 435 | |
paul@517 | 436 | l.insert(0, self[:last]) |
paul@517 | 437 | return l |
paul@517 | 438 | |
paul@517 | 439 | def rstrip(self, chars=None): |
paul@517 | 440 | |
paul@517 | 441 | """ |
paul@517 | 442 | Strip any of the given 'chars' from the end of the string, or strip |
paul@517 | 443 | whitespace characters is 'chars' is omitted or None. |
paul@517 | 444 | """ |
paul@517 | 445 | |
paul@517 | 446 | if chars is not None and not chars: |
paul@517 | 447 | return self |
paul@517 | 448 | |
paul@517 | 449 | i = self.__len__() - 1 |
paul@517 | 450 | |
paul@517 | 451 | while i >= 0 and self[i] in (chars or WHITESPACE): |
paul@517 | 452 | i -= 1 |
paul@517 | 453 | |
paul@517 | 454 | return self[:i+1] |
paul@515 | 455 | |
paul@515 | 456 | def split(self, sep=None, maxsplit=None): |
paul@515 | 457 | |
paul@515 | 458 | """ |
paul@515 | 459 | Split the string using the given 'sep' as separator (or any whitespace |
paul@515 | 460 | character if omitted or specified as None), splitting at most 'maxsplit' |
paul@515 | 461 | times (or as many times as is possible if omitted or specified as None). |
paul@517 | 462 | Where 'maxsplit' is given, the number of split points is counted from |
paul@517 | 463 | the start of the string. |
paul@515 | 464 | """ |
paul@515 | 465 | |
paul@515 | 466 | if sep is not None and not sep: |
paul@515 | 467 | raise ValueError, sep |
paul@515 | 468 | |
paul@517 | 469 | if maxsplit is not None and not maxsplit: |
paul@517 | 470 | return [self] |
paul@517 | 471 | |
paul@517 | 472 | seplen = sep and len(sep) or 1 |
paul@517 | 473 | end = self.__len__() - seplen |
paul@515 | 474 | splits = 0 |
paul@515 | 475 | |
paul@515 | 476 | l = [] |
paul@515 | 477 | i = last = 0 |
paul@515 | 478 | |
paul@517 | 479 | while i <= end and (maxsplit is None or splits < maxsplit): |
paul@515 | 480 | |
paul@515 | 481 | # Find any specified separator. |
paul@515 | 482 | |
paul@515 | 483 | if sep and self[i:i+seplen] == sep: |
paul@515 | 484 | l.append(self[last:i]) |
paul@515 | 485 | i += seplen |
paul@515 | 486 | last = i |
paul@515 | 487 | splits += 1 |
paul@515 | 488 | |
paul@515 | 489 | # Find any whitespace character and skip adjacent characters. |
paul@515 | 490 | |
paul@515 | 491 | elif not sep and self[i] in WHITESPACE: |
paul@515 | 492 | l.append(self[last:i]) |
paul@515 | 493 | while i < end: |
paul@515 | 494 | i += 1 |
paul@515 | 495 | if self[i] not in WHITESPACE: |
paul@515 | 496 | break |
paul@515 | 497 | else: |
paul@515 | 498 | break |
paul@515 | 499 | last = i |
paul@515 | 500 | splits += 1 |
paul@515 | 501 | |
paul@515 | 502 | # Check the next character. |
paul@515 | 503 | |
paul@515 | 504 | else: |
paul@515 | 505 | i += 1 |
paul@515 | 506 | |
paul@515 | 507 | l.append(self[last:]) |
paul@515 | 508 | return l |
paul@515 | 509 | |
paul@6 | 510 | def splitlines(self, keepends=False): pass |
paul@413 | 511 | |
paul@413 | 512 | def startswith(self, s): |
paul@413 | 513 | |
paul@413 | 514 | "Return whether this string starts with 's'." |
paul@413 | 515 | |
paul@413 | 516 | return self[:s.__len__()] == s |
paul@413 | 517 | |
paul@517 | 518 | def strip(self, chars=None): |
paul@517 | 519 | |
paul@517 | 520 | """ |
paul@517 | 521 | Strip any of the given 'chars' from the start and end of the string, or |
paul@517 | 522 | strip whitespace characters is 'chars' is omitted or None. |
paul@517 | 523 | """ |
paul@517 | 524 | |
paul@517 | 525 | return self.lstrip(chars).rstrip(chars) |
paul@517 | 526 | |
paul@6 | 527 | def upper(self): pass |
paul@6 | 528 | |
paul@431 | 529 | class string(basestring): |
paul@431 | 530 | |
paul@431 | 531 | "A plain string of bytes." |
paul@431 | 532 | |
paul@292 | 533 | # Special implementation methods. |
paul@292 | 534 | |
paul@292 | 535 | def __get_single_item__(self, index): |
paul@292 | 536 | |
paul@292 | 537 | "Return the item at the normalised (positive) 'index'." |
paul@292 | 538 | |
paul@292 | 539 | self._check_index(index) |
paul@384 | 540 | return str_substr(self.__data__, index, index + 1, 1) |
paul@384 | 541 | |
paul@384 | 542 | def __get_multiple_items__(self, start, end, step): |
paul@384 | 543 | |
paul@384 | 544 | """ |
paul@384 | 545 | Return items from 'start' until (but excluding) 'end', at 'step' |
paul@384 | 546 | intervals. |
paul@384 | 547 | """ |
paul@384 | 548 | |
paul@520 | 549 | if start == end: |
paul@520 | 550 | return "" |
paul@520 | 551 | |
paul@384 | 552 | check_int(step) |
paul@384 | 553 | |
paul@384 | 554 | if step == 0: |
paul@384 | 555 | raise ValueError(step) |
paul@384 | 556 | |
paul@520 | 557 | l = get_using(basestring.__get_multiple_items__, self)(start, end, step) |
paul@520 | 558 | return "".join(l) |
paul@292 | 559 | |
paul@188 | 560 | def str(obj): |
paul@188 | 561 | |
paul@188 | 562 | "Return the string representation of 'obj'." |
paul@188 | 563 | |
paul@248 | 564 | # Class attributes of instances provide __str__. |
paul@248 | 565 | |
paul@274 | 566 | return obj.__str__() |
paul@188 | 567 | |
paul@6 | 568 | # vim: tabstop=4 expandtab shiftwidth=4 |