提交 fe80ad1a authored 作者: Olivier Breuleux's avatar Olivier Breuleux

added unpack_single option to Linker.make_function

上级 0ab3226d
...@@ -441,7 +441,7 @@ class CLinker(Linker): ...@@ -441,7 +441,7 @@ class CLinker(Linker):
raise error_storage[0], error_storage[1] + " " + str(self.find_task(failure - 1)) raise error_storage[0], error_storage[1] + " " + str(self.find_task(failure - 1))
return execute, in_results, out_results return execute, in_results, out_results
def make_function(self, inplace = False): def make_function(self, inplace = False, unpack_single = True):
cthunk, in_results, out_results, error_storage = self.__compile__(inplace) cthunk, in_results, out_results, error_storage = self.__compile__(inplace)
# out_storage = [result._data for result in out_results] # out_storage = [result._data for result in out_results]
...@@ -451,7 +451,10 @@ class CLinker(Linker): ...@@ -451,7 +451,10 @@ class CLinker(Linker):
failure = cutils.run_cthunk(cthunk) failure = cutils.run_cthunk(cthunk)
if failure: if failure:
raise error_storage[0], error_storage[1] + " " + str(self.find_task(failure - 1)) raise error_storage[0], error_storage[1] + " " + str(self.find_task(failure - 1))
if unpack_single:
return utils.to_return_values([result.data for result in out_results]) return utils.to_return_values([result.data for result in out_results])
else:
return [result.data for result in out_results]
# return utils.to_return_values([storage[0] for storage in out_storage]) # return utils.to_return_values([storage[0] for storage in out_storage])
return execute return execute
......
...@@ -30,7 +30,7 @@ class Linker: ...@@ -30,7 +30,7 @@ class Linker:
""" """
raise AbstractFunctionError() raise AbstractFunctionError()
def make_function(self, inplace = False): def make_function(self, inplace = False, unpack_single = True):
""" """
Returns a function that takes values corresponding to the inputs of the Returns a function that takes values corresponding to the inputs of the
env used by this Linker and returns values corresponding the the outputs env used by this Linker and returns values corresponding the the outputs
...@@ -44,6 +44,10 @@ class Linker: ...@@ -44,6 +44,10 @@ class Linker:
fn = MyLinker(env).make_function(inplace) fn = MyLinker(env).make_function(inplace)
print fn(1.0, 2.0) # 3.0 print fn(1.0, 2.0) # 3.0
print e.data # 3.0 iff inplace == True (else unknown) print e.data # 3.0 iff inplace == True (else unknown)
If unpack_single is True (default) and that the function has only one
output, then that output will be returned. Else, a list or tuple of
length 1 will be returned.
""" """
thunk, inputs, outputs = self.make_thunk(inplace) thunk, inputs, outputs = self.make_thunk(inplace)
...@@ -51,7 +55,10 @@ class Linker: ...@@ -51,7 +55,10 @@ class Linker:
for arg, result in zip(args, inputs): for arg, result in zip(args, inputs):
result.data = arg result.data = arg
thunk() thunk()
if unpack_single:
return utils.to_return_values([result.data for result in outputs]) return utils.to_return_values([result.data for result in outputs])
else:
return [result.data for result in outputs]
return execute return execute
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论