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)
```

貢獻指南

開啟貢獻指南

研究方向

從 CLI 範例開始,檢視 Python Fire 如何為命令物件呼叫 __str__。尋找現有的生命週期、清理或內容管理器相關指南與測試;若要視為完成,就必須對 Setup/Teardown 行為達成共識,但此 issue 未指定目標檔案或驗收標準。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
cli
Issue 類型
功能
難度
5/5
預估耗時
一週以上
活躍度
停滯
描述清晰度
需要釐清
新手友好度
20/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。