我知道这是非常基本的,我一直在到处搜索,但我仍然对我所看到的一切感到非常困惑,我不确定这样做的最佳方法,并且我很难把头围绕它。
我有一个脚本,其中有多个功能。我希望第一个函数将它的输出传递给第二个函数,然后第二个函数将它的输出传递给第三个函数,依此类推。在到起始数据集的整个过程中,每个函数都有自己的步骤。
例如,用坏名字非常简化,但这只是为了获得基本结构:
#!/usr/bin/python
# script called process.py
import sys
infile = sys.argv[1]
def function_one():
do things
return function_one_output
def function_two():
take output from function_one, and do more things
return function_two_output
def function_three():
take output from function_two, do more things
return/print function_three_output
Run Code Online (Sandbox Code Playgroud)
我希望它作为一个脚本运行并将输出/写入打印到新文件或我知道如何做的任何事情。只是不清楚如何将每个函数的中间输出传递给下一个等等。
infile -> function_one -> (intermediate1) -> function_two -> (intermediate2) -> function_three -> 最终结果/输出文件
我知道我需要使用 return,但我不确定如何在最后调用它以获得我的最终输出
个人?
function_one(infile)
function_two()
function_three()
Run Code Online (Sandbox Code Playgroud)
还是彼此之间?
function_three(function_two(function_one(infile)))
Run Code Online (Sandbox Code Playgroud)
还是在实际功能中?
def function_one():
do things
return function_one_output
def function_two():
input_for_this_function = function_one()
# etc etc etc …Run Code Online (Sandbox Code Playgroud) 我需要链接promises来发出几个GET请求,并在将数据用于其他地方之前将其合并.我很难解决这两个承诺.我尝试在尝试使用之前返回两个promise的数组,.json()但这也不起作用.
activate() {
// data is from http://jsonplaceholder.typicode.com/photos and
// since there is not a photos2/ endpoint this is conceptual and
// represents batch importng
return Promise.all([
this.http.fetch('photos'),
this.http.fetch('photos2')
]).then(responses => {
console.log(responses); // see block of code below this for output
// how can I combine the two promises here so I can resolve them with 'then' below?
return responses[0].json(); // can return one response
// return responses; // doesn't work
}).then(data => {
console.log(data);
this.photos = …Run Code Online (Sandbox Code Playgroud)