10
10
from collections import OrderedDict
11
11
from math import isnan
12
12
13
- import six
14
-
15
13
from .service import Service
16
14
17
15
@@ -46,8 +44,9 @@ def is_uuid(cls, id_):
46
44
# MAS id.
47
45
return re .match ('^[_a-z][_a-z0-9]+$' , id_ ) is not None
48
46
49
- list_modules , get_module , update_module , \
50
- delete_module = Service ._crud_funcs ('/modules' , 'module' )
47
+ list_modules , get_module , update_module , delete_module = Service ._crud_funcs (
48
+ '/modules' , 'module'
49
+ )
51
50
52
51
def get_module_step (self , module , step ):
53
52
"""Details of a single step in a given module.
@@ -129,8 +128,7 @@ def execute_module_step(self, module, step, return_dict=True, **kwargs):
129
128
elif type_name == 'int64' :
130
129
kwargs [k ] = int (kwargs [k ])
131
130
132
- body = {'inputs' : [{'name' : k , 'value' : v }
133
- for k , v in six .iteritems (kwargs )]}
131
+ body = {'inputs' : [{'name' : k , 'value' : v } for k , v in kwargs .items ()]}
134
132
135
133
# Convert NaN to None (null) before calling MAS
136
134
for i in body ['inputs' ]:
@@ -163,8 +161,14 @@ def execute_module_step(self, module, step, return_dict=True, **kwargs):
163
161
return outputs [0 ]
164
162
return outputs
165
163
166
- def create_module (self , name = None , description = None , source = None ,
167
- language = 'python' , scope = 'public' ):
164
+ def create_module (
165
+ self ,
166
+ name = None ,
167
+ description = None ,
168
+ source = None ,
169
+ language = 'python' ,
170
+ scope = 'public' ,
171
+ ):
168
172
"""Create a new module in MAS.
169
173
170
174
Parameters
@@ -189,14 +193,15 @@ def create_module(self, name=None, description=None, source=None,
189
193
elif language == 'ds2' :
190
194
t = 'text/vnd.sas.source.ds2'
191
195
else :
192
- raise ValueError ('Unrecognized source code language `%s`.'
193
- % language )
196
+ raise ValueError ('Unrecognized source code language `%s`.' % language )
194
197
195
- data = {'id' : name ,
196
- 'type' : t ,
197
- 'description' : description ,
198
- 'source' : source ,
199
- 'scope' : scope }
198
+ data = {
199
+ 'id' : name ,
200
+ 'type' : t ,
201
+ 'description' : description ,
202
+ 'source' : source ,
203
+ 'scope' : scope ,
204
+ }
200
205
201
206
r = self .post ('/modules' , json = data )
202
207
return r
@@ -240,8 +245,7 @@ def define_steps(self, module):
240
245
# Default all params to None to allow method(DataFrame) execution
241
246
input_params = [a for a in arguments ] + ['**kwargs' ]
242
247
method_name = '_%s_%s' % (module .id , step .id )
243
- signature = 'def %s(%s):' \
244
- % (method_name , ', ' .join (input_params ))
248
+ signature = 'def %s(%s):' % (method_name , ', ' .join (input_params ))
245
249
246
250
# If the module step takes arguments, then we perform a number
247
251
# of additional checks to make the allowed input as wide as possible
@@ -266,20 +270,22 @@ def define_steps(self, module):
266
270
' is_numpy = False' ,
267
271
'if is_pandas:' ,
268
272
' assert len(first_input.shape) == 1' ,
269
- ' for k in first_input.keys():'
273
+ ' for k in first_input.keys():' ,
270
274
]
271
275
272
276
for arg in arguments :
273
277
arg_checks .append (" if k.lower() == '%s':" % arg .lower ())
274
278
arg_checks .append (" %s = first_input[k]" % arg )
275
279
arg_checks .append (" continue" )
276
280
277
- arg_checks .extend ([
278
- 'elif is_numpy:' ,
279
- ' if len(first_input.shape) > 1:' ,
280
- ' assert first_input.shape[0] == 1' ,
281
- ' first_input = first_input.ravel()'
282
- ])
281
+ arg_checks .extend (
282
+ [
283
+ 'elif is_numpy:' ,
284
+ ' if len(first_input.shape) > 1:' ,
285
+ ' assert first_input.shape[0] == 1' ,
286
+ ' first_input = first_input.ravel()' ,
287
+ ]
288
+ )
283
289
284
290
for i , arg in enumerate (arguments ):
285
291
arg_checks .append (' %s = first_input[%d]' % (arg , i ))
@@ -307,31 +313,38 @@ def define_steps(self, module):
307
313
308
314
# Full method source code
309
315
# Drops 'rc' and 'msg' from return values
310
- code = (signature ,
311
- type_string ,
312
- ' """Execute step \' %s\' of module \' %s\' ."""' % (step , module ),
313
- '\n ' .join (' %s' % a for a in arg_checks ),
314
- ' r = execute_module_step(%s)' % ', ' .join (['module' , 'step' ] + call_params ),
315
- ' r.pop("rc", None)' ,
316
- ' r.pop("msg", None)' ,
317
- ' if len(r) == 1:' ,
318
- ' return r.popitem()[1]' ,
319
- ' return tuple(v for v in r.values())'
320
- )
316
+ code = (
317
+ signature ,
318
+ type_string ,
319
+ ' """Execute step \' %s\' of module \' %s\' ."""' % (step , module ),
320
+ '\n ' .join (' %s' % a for a in arg_checks ),
321
+ ' r = execute_module_step(%s)'
322
+ % ', ' .join (['module' , 'step' ] + call_params ),
323
+ ' r.pop("rc", None)' ,
324
+ ' r.pop("msg", None)' ,
325
+ ' if len(r) == 1:' ,
326
+ ' return r.popitem()[1]' ,
327
+ ' return tuple(v for v in r.values())' ,
328
+ )
321
329
322
330
code = '\n ' .join (code )
323
- self .log .debug ("Generated code for step '%s' of module '%s':\n %s" ,
324
- id_ , module , code )
331
+ self .log .debug (
332
+ "Generated code for step '%s' of module '%s':\n %s" , id_ , module , code
333
+ )
325
334
compiled = compile (code , '<string>' , 'exec' )
326
335
327
336
env = globals ().copy ()
328
- env .update ({'execute_module_step' : self .execute_module_step ,
329
- 'module' : module ,
330
- 'step' : step })
331
-
332
- func = types .FunctionType (compiled .co_consts [0 ],
333
- env ,
334
- argdefs = tuple (None for x in arguments ))
337
+ env .update (
338
+ {
339
+ 'execute_module_step' : self .execute_module_step ,
340
+ 'module' : module ,
341
+ 'step' : step ,
342
+ }
343
+ )
344
+
345
+ func = types .FunctionType (
346
+ compiled .co_consts [0 ], env , argdefs = tuple (None for x in arguments )
347
+ )
335
348
336
349
setattr (module , step .id , func )
337
350
0 commit comments