我需要一次读取最多N行读取一个大文件,直到EOF.在Python中最有效的方法是什么?就像是:
with open(filename, 'r') as infile:
while not EOF:
lines = [get next N lines]
process(lines)
Run Code Online (Sandbox Code Playgroud) 我希望将一系列函数应用于一个对象(每个函数可以返回相同或修改的对象)并获得最后一个函数返回的最终结果.
是否有惯用的Scala方法来转换它(伪代码):
val pipeline = ListMap(("a" -> obj1), ("b" -> obj2), ("c" -> obj3))
Run Code Online (Sandbox Code Playgroud)
进入这个?
val initial_value = Something("foo", "bar")
val result = obj3.func(obj2.func(obj1.func(initial_value)))
Run Code Online (Sandbox Code Playgroud)
它pipeline在运行时初始化,包含未确定数量的"manglers".
我试过foreach但它需要一个中间var来存储结果,并且foldLeft只适用于类型ListMap,而初始值和结果是类型Something.
谢谢
我陷入了相对复杂的芹菜链配置,试图实现以下目标。假设有如下一系列任务:
chain1 = chain(
DownloadFile.s("http://someserver/file.gz"), # downloads file, returns temp file name
UnpackFile.s(), # unpacks the gzip comp'd file, returns temp file name
ParseFile.s(), # parses file, returns list URLs to download
)
Run Code Online (Sandbox Code Playgroud)
现在我想并行下载每个 URL,所以我所做的是:
urls = chain1.get()
download_tasks = map(lambda x: DownloadFile.s(x), urls)
res1 = celery.group(download_tasks)()
res1_data = res1.get()
Run Code Online (Sandbox Code Playgroud)
最后,我想获取每个下载的文件(从 中返回一个临时文件名DownloadFile)ParseFile,并通过另一个任务链并行运行它(例如,它将是 a groupof chains):
chains = []
for tmpfile in res:
chains.append(celery.chain(
foo.s(tmpfile),
bar.s(),
baz.s()
))
res2 = celery.group(*chains)()
res2_data = res2.get()
Run Code Online (Sandbox Code Playgroud)
如果我在正常的 …