Skip to content

Commit ecc8177

Browse files
committed
-
1 parent 002adf8 commit ecc8177

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

source_py3/python_toolbox/misc_tools/misc_tools.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,18 @@ def decimal_number_from_string(string):
345345

346346

347347
class AlternativeLengthMixin:
348+
'''
349+
Mixin for sized types that makes it easy to return non-standard lengths.
350+
351+
Due to CPython limitation, Python's built-in `__len__` (and its counterpart
352+
`len`) can't return really big values or floating point numbers.
353+
354+
Classes which need to return such lengths can use this mixin. They'll have
355+
to define a property `length` where they return their length, and if
356+
someone tries to call `len` on it, then if the length happens to be a
357+
number that `len` supports, it'll return that, otherwise it'll show a
358+
helpful error message.
359+
'''
348360
def __len__(self):
349361
length = self.length
350362
if (length <= sys.maxsize) and isinstance(length, int):

source_py3/python_toolbox/sequence_tools/misc.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -278,21 +278,24 @@ def ensure_iterable_is_sequence(iterable, default_type=tuple,
278278
class CanonicalSlice:
279279
def __init__(self, slice_, iterable_or_length=None, offset=0):
280280
'''
281-
blocktododoc iterable_or_length, also note result, if not given iterable/length, can't really be used as slice (only as canonical object) because of `infinity`
281+
Parse a `slice` object into a `CanonicalSlice` with start, stop, step.
282282
283-
Parse a `slice` object into a canonical `(start, stop, step)`.
284-
285283
This is helpful because `slice`'s own `.start`, `.stop` and `.step` are
286284
sometimes specified as `None` for convenience, so Python will infer
287-
them automatically. Here we make them explicit.
288-
289-
if `start` is `None`, it will be set to `0` (if the `step` is positive)
290-
or `infinity` (if the `step` is negative.)
285+
them automatically. Here we make them explicit. If we're given an
286+
iterable (or the length of one) in `iterable_or_length`, we'll give a
287+
canoncial slice for that length, otherwise we'll do a generic one,
288+
which is rarely usable for actual slicing because it often has
289+
`infinity` in it, so it's useful only for canonalization. (e.g.
290+
checking whether two different slices are actually equal.)
291291
292-
if `stop` is `None`, it will be set to `infinity` (if the `step` is
292+
When doing a generic canonical slice (without giving an iterable or
293+
length):
294+
If `start` is `None`, it will be set to `0` (if the `step` is
295+
positive) or `infinity` (if the `step` is negative.)
296+
If `stop` is `None`, it will be set to `infinity` (if the `step` is
293297
positive) or `0` (if the `step` is negative.)
294-
295-
If `step` is `None`, it will be changed to the default `1`.
298+
If `step` is `None`, it will be changed to the default `1`.
296299
'''
297300
from . import math_tools
298301

@@ -401,6 +404,7 @@ def __init__(self, slice_, iterable_or_length=None, offset=0):
401404

402405
class CuteSequenceMixin(misc_tools.AlternativeLengthMixin):
403406
def take_random(self):
407+
'''Take a random item from the sequence.'''
404408
return self[random.randint(0, get_length(self) - 1)]
405409
def __contains__(self, item):
406410
try: self.index(item)

0 commit comments

Comments
 (0)