【发布时间】:2017-11-06 20:14:42
【问题描述】:
我正在尝试使用 Luigi 批量处理几个 Jupyter 笔记本,但遇到了问题。
我有两节课。第一个,transform.py:
import nbformat
import nbconvert
import luigi
from nbconvert.preprocessors.execute import CellExecutionError
class Transform(luigi.Task):
"""Foo."""
notebook = luigi.Parameter()
requirements = luigi.ListParameter()
def requires(self):
return self.requirements
def run(self):
nb = nbformat.read(self.notebook, nbformat.current_nbformat)
# https://nbconvert.readthedocs.io/en/latest/execute_api.html
ep = nbconvert.preprocessors.ExecutePreprocessor(timeout=600, kernel_name='python3')
try:
ep.preprocess(nb, {'metadata': {'path': "/".join(self.notebook.split("/")[:-1])}})
with self.output().open('w') as f:
nbformat.write(nb, f)
except CellExecutionError:
pass # TODO
def output(self):
return luigi.LocalTarget(self.notebook)
这定义了一个将笔记本作为输入的 Luigi 任务(以及运行此任务的可能的先前要求),并且应该运行该笔记本并报告成功或失败作为输出。
要运行 Transform 任务,我有一个很小的 Runner 类:
import luigi
class Runner(luigi.Task):
requirements = luigi.ListParameter()
def requires(self):
return self.requirements
为了完成我的小工作,我会这样做:
from transform Transform
trans = Transform("../tests/fixtures/empty_valid_errorless_notebook.ipynb", [])
from runner import Runner
run_things = Runner([trans])
但这引发了TypeError: Object of type 'Transform' is not JSON serializable!
我的luigi 任务格式是否正确?如果是这样,run 中的哪个组件使整个类不可序列化是否很明显?如果没有,我应该如何调试?
【问题讨论】:
标签: python json serialization jupyter luigi