99
1010
1111class ADK (object ):
12-
1312 def __init__ (self , apply_func , load_func = None ):
1413 """
1514 Creates the adk object
1615 :param apply_func: A required function that can have an arity of 1-2, depending on if loading occurs
1716 :param load_func: An optional supplier function used if load time events are required, has an arity of 0.
1817 """
1918 self .FIFO_PATH = "/tmp/algoout"
20- apply_args , _ , _ , apply_defaults , _ , _ , _ = inspect .getfullargspec (apply_func )
21- # apply_args, _, _, apply_defaults = inspect.getargspec(apply_func)
22- # j = inspect.getfullargspec(apply_func)
19+ apply_args , _ , _ , _ , _ , _ , _ = inspect .getfullargspec (apply_func )
2320 if load_func :
2421 load_args , _ , _ , _ , _ , _ , _ = inspect .getfullargspec (load_func )
2522 if len (load_args ) > 0 :
@@ -38,16 +35,16 @@ def load(self):
3835 if self .is_local :
3936 print ("loading complete" )
4037 else :
41- print (' PIPE_INIT_COMPLETE' )
38+ print (" PIPE_INIT_COMPLETE" )
4239 sys .stdout .flush ()
4340
4441 def format_data (self , request ):
45- if request [' content_type' ] in [' text' , ' json' ]:
46- data = request [' data' ]
47- elif request [' content_type' ] == ' binary' :
48- data = self .wrap_binary_data (request [' data' ])
42+ if request [" content_type" ] in [" text" , " json" ]:
43+ data = request [" data" ]
44+ elif request [" content_type" ] == " binary" :
45+ data = self .wrap_binary_data (request [" data" ])
4946 else :
50- raise Exception ("Invalid content_type: {}" .format (request [' content_type' ]))
47+ raise Exception ("Invalid content_type: {}" .format (request [" content_type" ]))
5148 return data
5249
5350 def is_binary (self , arg ):
@@ -64,20 +61,20 @@ def wrap_binary_data(self, data):
6461
6562 def format_response (self , response ):
6663 if self .is_binary (response ):
67- content_type = 'binary'
68- response = base64 .b64encode (response )
69- if not isinstance (response , six .string_types ):
70- response = str (response , 'utf-8' )
64+ content_type = "binary"
65+ response = str (base64 .b64encode (response ), "utf-8" )
7166 elif isinstance (response , six .string_types ) or isinstance (response , six .text_type ):
72- content_type = ' text'
67+ content_type = " text"
7368 else :
74- content_type = 'json'
75- response_string = json .dumps ({
76- 'result' : response ,
77- 'metadata' : {
78- 'content_type' : content_type
69+ content_type = "json"
70+ response_string = json .dumps (
71+ {
72+ "result" : response ,
73+ "metadata" : {
74+ "content_type" : content_type
75+ }
7976 }
80- } )
77+ )
8178 return response_string
8279
8380 def write_to_pipe (self , payload ):
@@ -88,23 +85,23 @@ def write_to_pipe(self, payload):
8885 print (payload )
8986 else :
9087 if os .name == "posix" :
91- with open (self .FIFO_PATH , 'w' ) as f :
88+ with open (self .FIFO_PATH , "w" ) as f :
9289 f .write (payload )
93- f .write (' \n ' )
90+ f .write (" \n " )
9491 sys .stdout .flush ()
9592 if os .name == "nt" :
9693 sys .stdin = payload
9794
9895 def create_exception (self , exception ):
99- if hasattr (exception , ' error_type' ):
96+ if hasattr (exception , " error_type" ):
10097 error_type = exception .error_type
10198 else :
102- error_type = ' AlgorithmError'
99+ error_type = " AlgorithmError"
103100 response = {
104- ' error' : {
105- ' message' : str (exception ),
106- ' stacktrace' : traceback .format_exc (),
107- ' error_type' : error_type
101+ " error" : {
102+ " message" : str (exception ),
103+ " stacktrace" : traceback .format_exc (),
104+ " error_type" : error_type ,
108105 }
109106 }
110107 return response
0 commit comments