Skip to content

Commit 235f267

Browse files
committed
-
1 parent a9d170b commit 235f267

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

python_toolbox/misc_tools/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
is_subclass, get_mro_depth_of_method, frange, getted_vars,
88
_ascii_variable_pattern, is_legal_ascii_variable_name,
99
is_magic_variable_name, get_actual_type, is_number, identity_function,
10-
do_nothing, OwnNameDiscoveringDescriptor, find_clear_place_on_circle
10+
do_nothing, OwnNameDiscoveringDescriptor, find_clear_place_on_circle,
11+
general_sum, general_product
1112
)
1213
from . import name_mangling

python_toolbox/misc_tools/misc_tools.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from __future__ import division
77

8+
import operator
89
import os.path
910
import re
1011
import math
@@ -238,4 +239,19 @@ def add_extension_if_plain(path, extension):
238239
return path
239240
else: # not existing_extension
240241
return without_extension + extension
242+
243+
244+
def general_sum(things, start=None):
245+
if start is None:
246+
return reduce(operator.add, things)
247+
else:
248+
return reduce(operator.add, things, start)
249+
250+
251+
def general_product(things, start=None):
252+
if start is None:
253+
return reduce(operator.mul, things)
254+
else:
255+
return reduce(operator.mul, things, start)
256+
241257

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright 2009-2013 Ram Rachum.
2+
# This program is distributed under the MIT license.
3+
4+
from python_toolbox.misc_tools import general_product
5+
6+
7+
def test():
8+
assert general_product((1, 2, 3)) == 6
9+
assert general_product((1, 2, 3, 4)) == 24
10+
assert general_product(((0, 1), 2, 3)) == (0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
11+
1)
12+
assert general_product((2, 3), start=(0, 1)) == (0, 1, 0, 1, 0, 1, 0, 1, 0,
13+
1, 0, 1)
14+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright 2009-2013 Ram Rachum.
2+
# This program is distributed under the MIT license.
3+
4+
from python_toolbox.misc_tools import general_sum
5+
6+
7+
def test():
8+
assert general_sum((1, 2, 3)) == 6
9+
assert general_sum((1, 2, 3, 4)) == 10
10+
assert general_sum(('abra', 'ca', 'dabra')) == 'abracadabra'
11+
assert general_sum(((0, 1), (0, 2), (0, 3))) == (0, 1, 0, 2, 0, 3)
12+
13+
assert general_sum(((0, 1), (0, 2), (0, 3)), start=(9,)) == (9, 0, 1, 0,
14+
2, 0, 3)

0 commit comments

Comments
 (0)