google / google/python-fire

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

未關閉
#358 5 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
Python
星號
28.2k
分支
1.5k
PR 合併指標
30 天內沒有已合併 PR

描述

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 🤔 ...

貢獻指南

開啟貢獻指南

評估

這個 Issue 還沒有評估資料。

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

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