google / google/python-fire

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

Đang mở
#242 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Python
Star
28.2k
Fork
1.5k
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.