Skip to content

Commit 5337adc

Browse files
committed
-
1 parent 6ebc0ac commit 5337adc

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

source_py2/python_toolbox/misc_tools/misc_tools.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ def is_subclass(candidate, base_class):
3434
check whether `candidate` is a subclass of any of these base classes.
3535
3636
This has the advantage that it doesn't throw an exception if `candidate` is
37-
not a type. (Python issue 10569.)
38-
37+
not a type. (Python issue 10569.)
3938
'''
4039
# todo: disable ability to use nested iterables.
4140
if cute_iter_tools.is_iterable(base_class):
@@ -100,7 +99,7 @@ def getted_vars(thing, _getattr=getattr):
10099
# todo: can make "fallback" option, to use value from original `vars` if
101100
# get is unsuccessful.
102101
my_vars = vars(thing)
103-
return dict((name, _getattr(thing, name)) for name in my_vars.iterkeys())
102+
return {name: _getattr(thing, name) for name in my_vars.iterkeys()}
104103

105104

106105

source_py3/python_toolbox/misc_tools/misc_tools.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@
33

44
'''This module defines miscellaneous tools.'''
55

6-
7-
86
import operator
97
import os.path
108
import re
119
import math
1210
import types
11+
import functools
1312

1413
from python_toolbox import cute_iter_tools
15-
from functools import reduce
1614

1715

1816
_email_pattern = re.compile(
@@ -100,7 +98,7 @@ def getted_vars(thing, _getattr=getattr):
10098
# todo: can make "fallback" option, to use value from original `vars` if
10199
# get is unsuccessful.
102100
my_vars = vars(thing)
103-
return dict((name, _getattr(thing, name)) for name in my_vars.keys())
101+
return {name: _getattr(thing, name) for name in my_vars.keys()}
104102

105103

106104

@@ -117,6 +115,17 @@ def is_magic_variable_name(name):
117115
name[:2] == name[-2:] == '__'
118116

119117

118+
def get_actual_type(thing):
119+
'''
120+
Get the actual type (or class) of an object.
121+
122+
This used to be needed instead of `type(thing)` in Python 2.x where we had
123+
old-style classes. In Python 3.x we don't have them anymore, but keeping
124+
this function for backward compatibility.
125+
'''
126+
return type(thing)
127+
128+
120129
def is_number(x):
121130
'''Return whether `x` is a number.'''
122131
try:
@@ -234,19 +243,19 @@ def general_sum(things, start=None):
234243
numbers.
235244
'''
236245
if start is None:
237-
return reduce(operator.add, things)
246+
return functools.reduce(operator.add, things)
238247
else:
239-
return reduce(operator.add, things, start)
248+
return functools.reduce(operator.add, things, start)
240249

241250

242251
def general_product(things, start=None):
243252
'''
244253
Multiply a bunch of objects by each other, not necessarily numbers.
245254
'''
246255
if start is None:
247-
return reduce(operator.mul, things)
256+
return functools.reduce(operator.mul, things)
248257
else:
249-
return reduce(operator.mul, things, start)
258+
return functools.reduce(operator.mul, things, start)
250259

251260

252261
def is_legal_email_address(email_address_candidate):

0 commit comments

Comments
 (0)