Skip to content

Commit 4205295

Browse files
committed
-
1 parent c9b5b7f commit 4205295

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

python_toolbox/cute_iter_tools.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,17 +202,27 @@ def sentinel(counter=([fill_value] * (len(iterables) - 1)).pop):
202202
raise StopIteration
203203

204204

205-
def get_items(iterable, n, container=tuple):
205+
def get_items(iterable, n, container_type=tuple):
206206
'''
207207
Get the next `n` items from the iterable as a `tuple`.
208208
209209
If there are less than `n` items, no exception will be raised. Whatever
210210
items are there will be returned.
211+
212+
If you pass in a different kind of container than `tuple` as
213+
`container_type`, it'll be used to wrap the results.
211214
'''
212-
return container(shorten(iterable, n))
215+
return container_type(shorten(iterable, n))
213216

214217

215218
def double_filter(filter_function, iterable):
219+
'''
220+
Filter an `iterable` into two lists according to a `filter_function`.
221+
222+
This is similar to the builtin `filter`, except it returns a tuple of two
223+
iterators, the first iterating on items that passed the filter function,
224+
and the second iterating on items that didn't.
225+
'''
216226
iterator = iter(iterable)
217227

218228
true_deque = collections.deque()

python_toolbox/math_tools.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ def ceil_div(x, y):
4242

4343

4444
def convert_to_base_in_tuple(number, base):
45+
'''
46+
Convert a number to any base, returning result in tuple.
47+
48+
For example, `convert_to_base_in_tuple(32, base=10)` will be `(3, 2)` while
49+
`convert_to_base_in_tuple(32, base=16)` will be `(2, 0)`.
50+
'''
4551
assert isinstance(number, numbers.Integral)
4652
assert isinstance(base, numbers.Integral)
4753
assert base >= 2
@@ -60,6 +66,7 @@ def convert_to_base_in_tuple(number, base):
6066

6167

6268
def get_median(iterable):
69+
'''Get the median of an iterable of numbers.'''
6370
sorted_values = sorted(iterable)
6471

6572
if len(iterable) % 2 == 0:
@@ -73,6 +80,7 @@ def get_median(iterable):
7380

7481

7582
def get_mean(iterable):
83+
'''Get the mean (average) of an iterable of numbers.'''
7684
sum_ = 0
7785
for i, value in enumerate(iterable):
7886
sum_ += value

0 commit comments

Comments
 (0)