33
44'''This module defines miscellaneous tools.'''
55
6-
7-
86import operator
97import os .path
108import re
119import math
1210import types
11+ import functools
1312
1413from 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+
120129def 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
242251def 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
252261def is_legal_email_address (email_address_candidate ):
0 commit comments