9
9
10
10
11
11
class ADK (object ):
12
-
13
12
def __init__ (self , apply_func , load_func = None ):
14
13
"""
15
14
Creates the adk object
16
15
:param apply_func: A required function that can have an arity of 1-2, depending on if loading occurs
17
16
:param load_func: An optional supplier function used if load time events are required, has an arity of 0.
18
17
"""
19
18
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 )
23
20
if load_func :
24
21
load_args , _ , _ , _ , _ , _ , _ = inspect .getfullargspec (load_func )
25
22
if len (load_args ) > 0 :
@@ -38,16 +35,16 @@ def load(self):
38
35
if self .is_local :
39
36
print ("loading complete" )
40
37
else :
41
- print (' PIPE_INIT_COMPLETE' )
38
+ print (" PIPE_INIT_COMPLETE" )
42
39
sys .stdout .flush ()
43
40
44
41
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" ])
49
46
else :
50
- raise Exception ("Invalid content_type: {}" .format (request [' content_type' ]))
47
+ raise Exception ("Invalid content_type: {}" .format (request [" content_type" ]))
51
48
return data
52
49
53
50
def is_binary (self , arg ):
@@ -64,20 +61,20 @@ def wrap_binary_data(self, data):
64
61
65
62
def format_response (self , response ):
66
63
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" )
71
66
elif isinstance (response , six .string_types ) or isinstance (response , six .text_type ):
72
- content_type = ' text'
67
+ content_type = " text"
73
68
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
+ }
79
76
}
80
- } )
77
+ )
81
78
return response_string
82
79
83
80
def write_to_pipe (self , payload ):
@@ -88,23 +85,23 @@ def write_to_pipe(self, payload):
88
85
print (payload )
89
86
else :
90
87
if os .name == "posix" :
91
- with open (self .FIFO_PATH , 'w' ) as f :
88
+ with open (self .FIFO_PATH , "w" ) as f :
92
89
f .write (payload )
93
- f .write (' \n ' )
90
+ f .write (" \n " )
94
91
sys .stdout .flush ()
95
92
if os .name == "nt" :
96
93
sys .stdin = payload
97
94
98
95
def create_exception (self , exception ):
99
- if hasattr (exception , ' error_type' ):
96
+ if hasattr (exception , " error_type" ):
100
97
error_type = exception .error_type
101
98
else :
102
- error_type = ' AlgorithmError'
99
+ error_type = " AlgorithmError"
103
100
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 ,
108
105
}
109
106
}
110
107
return response
0 commit comments