1- import collections
21import math
2+ from collections .abc import Iterable
33from functools import reduce
44from itertools import chain , count , islice , takewhile
55from multiprocessing import Pool , cpu_count
@@ -49,7 +49,7 @@ def is_namedtuple(val):
4949 bases = val_type .__bases__
5050 if len (bases ) != 1 or bases [0 ] != tuple :
5151 return False
52- fields = getattr (val_type , "_fields" , None )
52+ fields = getattr (val_type , "_fields" )
5353 return all (isinstance (n , str ) for n in fields )
5454
5555
@@ -69,7 +69,7 @@ def identity(arg):
6969
7070def is_iterable (val ):
7171 """
72- Check if val is not a list, but is a collections. Iterable type. This is used to determine
72+ Check if val is not a list, but is a Iterable type. This is used to determine
7373 when list() should be called on val
7474
7575 >>> l = [1, 2]
@@ -79,19 +79,15 @@ def is_iterable(val):
7979 True
8080
8181 :param val: value to check
82- :return: True if it is not a list, but is a collections. Iterable
82+ :return: True if it is not a list, but is a Iterable
8383 """
84- if isinstance (val , list ):
85- return False
86- return isinstance (val , collections .abc .Iterable )
84+ return not isinstance (val , list ) and isinstance (val , Iterable )
8785
8886
89- def is_tabulatable (val ):
90- if is_primitive (val ):
91- return False
92- if is_iterable (val ) or is_namedtuple (val ) or isinstance (val , list ):
93- return True
94- return False
87+ def is_tabulatable (val : object ) -> bool :
88+ return not is_primitive (val ) and (
89+ is_iterable (val ) or is_namedtuple (val ) or isinstance (val , list )
90+ )
9591
9692
9793def split_every (parts , iterable ):
@@ -117,7 +113,7 @@ def unpack(packed):
117113 """
118114 func , args = serializer .loads (packed )
119115 result = func (* args )
120- if isinstance (result , collections . abc . Iterable ):
116+ if isinstance (result , Iterable ):
121117 return list (result )
122118 return None
123119
@@ -164,8 +160,7 @@ def lazy_parallelize(func, result, processes=None, partition_size=None):
164160 with Pool (processes = processes ) as pool :
165161 partitions = split_every (partition_size , iter (result ))
166162 packed_partitions = (pack (func , (partition ,)) for partition in partitions )
167- for pool_result in pool .imap (unpack , packed_partitions ):
168- yield pool_result
163+ yield from pool .imap (unpack , packed_partitions )
169164
170165
171166def compute_partition_size (result , processes ):
0 commit comments