@@ -58,8 +58,8 @@ class Function(IOBase):
58
58
input_spec = FunctionInputSpec
59
59
output_spec = DynamicTraitedSpec
60
60
61
- def __init__ (self , input_names , output_names , function = None , imports = None ,
62
- ** inputs ):
61
+ def __init__ (self , input_names = None , output_names = 'out' , function = None ,
62
+ imports = None , ** inputs ):
63
63
"""
64
64
65
65
Parameters
@@ -88,10 +88,18 @@ def __init__(self, input_names, output_names, function=None, imports=None,
88
88
raise Exception ('Interface Function does not accept '
89
89
'function objects defined interactively '
90
90
'in a python session' )
91
- elif isinstance (function , (str , bytes )):
91
+ else :
92
+ if inputs is None :
93
+ fninfo = function .func_code
94
+ elif isinstance (function , string_types ):
92
95
self .inputs .function_str = function
96
+ if inputs is None :
97
+ fninfo = create_function_from_source (
98
+ function , imports ).func_code
93
99
else :
94
100
raise Exception ('Unknown type of function' )
101
+ if inputs is None :
102
+ inputs = fninfo .co_varnames [:fninfo .co_argcount ]
95
103
self .inputs .on_trait_change (self ._set_function_string ,
96
104
'function_str' )
97
105
self ._input_names = filename_to_list (input_names )
@@ -106,10 +114,18 @@ def _set_function_string(self, obj, name, old, new):
106
114
if name == 'function_str' :
107
115
if hasattr (new , '__call__' ):
108
116
function_source = getsource (new )
109
- elif isinstance (new , (str , bytes )):
117
+ fninfo = new .func_code
118
+ elif isinstance (new , string_types ):
110
119
function_source = new
120
+ fninfo = create_function_from_source (
121
+ new , self .imports ).func_code
111
122
self .inputs .trait_set (trait_change_notify = False ,
112
123
** {'%s' % name : function_source })
124
+ # Update input traits
125
+ input_names = fninfo .co_varnames [:fninfo .co_argcount ]
126
+ new_names = set (input_names ) - set (self ._input_names )
127
+ add_traits (self .inputs , list (new_names ))
128
+ self ._input_names = new_names
113
129
114
130
def _add_output_traits (self , base ):
115
131
undefined_traits = {}
0 commit comments