google / google/python-fire

What is the best way to have a "factory method" style?

Đang mở
#358 5 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ả

Right now I ended up with a following approach ( not sure if it the best):

```python
import fire
import foo
import bar

def foo(*args):
Foo(*args).configure()

def bar(*args):
Bar(*args).configure()

def dispatch(**args):
print("arguments passed", args)
"""Factory Method"""
localizers = {
"policy": foo,
"repo": bar,
}
return localizers[args["obj_type"]](args)

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

Ideally I would want that `Fire` method allow me to pass classnames as variable which I define through a command line, something like that:

```python
from ado import * # Repo and Policy classes are defined at libs/ado.py

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

```bash
python script.py --obj_type=Foo
```

Not sure if it's possible 🤔 ...

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.