Skip to content

Commit 5b3ce03

Browse files
committed
-
1 parent d332c9b commit 5b3ce03

File tree

6 files changed

+70
-3
lines changed

6 files changed

+70
-3
lines changed

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[nosetests]
2-
where=source_py3/test_python_toolbox
2+
where=source_py2/test_python_toolbox
33

44
verbosity=3
55
detailed-errors=1

source_py2/python_toolbox/cute_iter_tools.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# todo: make something like `filter` except it returns first found, or raises
66
# exception
77

8+
from __future__ import division
89
from __future__ import with_statement
910

1011
import collections

source_py3/python_toolbox/cute_iter_tools.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,4 +259,28 @@ def make_false_iterator():
259259

260260

261261

262+
def get_ratio(filter_function, iterable):
263+
if isinstance(filter_function, str):
264+
attribute_name = filter_function
265+
filter_function = lambda item: getattr(item, attribute_name, None)
266+
n_total_items = 0
267+
n_passed_items = 0
268+
for item in iterable:
269+
n_total_items += 1
270+
if filter_function(item):
271+
n_passed_items += 1
272+
return n_passed_items / n_total_items
273+
274+
275+
276+
277+
278+
279+
280+
281+
282+
283+
284+
285+
262286

source_py3/python_toolbox/math_tools.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
'''This module defines math-related tools.'''
55

6-
7-
86
import numbers
97

8+
infinity = float('inf')
9+
1010

1111
def get_sign(x):
1212
'''Get the sign of a number.'''
@@ -85,4 +85,19 @@ def get_mean(iterable):
8585
for i, value in enumerate(iterable):
8686
sum_ += value
8787
return sum_ / (i + 1)
88+
89+
90+
def restrict_number_to_range(number, low_cutoff=-infinity,
91+
high_cutoff=infinity):
92+
'''
93+
If `number` is not in the range between cutoffs, return closest cutoff.
94+
95+
If the number is in range, simply return it.
96+
'''
97+
if number < low_cutoff:
98+
return low_cutoff
99+
elif number > high_cutoff:
100+
return high_cutoff
101+
else:
102+
return number
88103

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright 2009-2013 Ram Rachum.
2+
# This program is distributed under the MIT license.
3+
4+
from python_toolbox import cute_iter_tools
5+
6+
7+
def test():
8+
ratio = cute_iter_tools.get_ratio('real', [1, 2, 3, 1j, 2j, 3j, 4j])
9+
assert ratio == 3 / 7
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2009-2013 Ram Rachum.
2+
# This program is distributed under the MIT license.
3+
4+
5+
import sys
6+
7+
import nose
8+
9+
from python_toolbox.math_tools import restrict_number_to_range
10+
11+
12+
def test_restrict_number_to_range():
13+
my_restrict = lambda number: restrict_number_to_range(number,
14+
low_cutoff=3.5,
15+
high_cutoff=7.8)
16+
assert list(map(my_restrict, range(10))) == [
17+
3.5, 3.5, 3.5, 3.5, 4, 5, 6, 7, 7.8, 7.8
18+
]

0 commit comments

Comments
 (0)