google / google/python-fire

Proper way to do tear-up/tear-down?

未关闭
#242 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Python
星标
28.2k
派生
1.5k
PR 合并指标
30 天内没有已合并 PR

描述

I have a pipeline tool that opens a file in `__init__`, mutates `self.nwbfile` using chained commands, and finally writes to disk & closes file. Right now I'm using `__str__` for this. Is this a reasonable pattern, or is there a better way?

```python
class CLI(object):
"""Facilitate writing a Neurodata without borders (NWB) file."""

def __init__(self, filepath):
self.io = NWBHDF5IO(filepath, 'a')
self.nwbfile = io.read()
self.filepath = filepath

def noop(self):
"add nothing to nwbfile"
return self

def __str__(self):
"""Write data & close io."""
if not self.io is None:
if not self.nwbfile is None:
self.io.write(self.nwbfile)
self.io.close()
return f"Wrote to {self.filepath}"

if __name__ == '__main__':
fire.Fire(CLI)
```

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。