首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >python -fire 快速创建命令行接口

python -fire 快速创建命令行接口

原创
作者头像
AI拉呱
修改于 2021-03-14 17:22:43
修改于 2021-03-14 17:22:43
9660
举报

fire

•fire可以快速创建命令行接口

•fire可以方便快速debug python代码

install

代码语言:javascript
复制
$ pip install fire

简单实例

代码语言:javascript
复制
import fire

def hello(name="World"): return "Hello %s!" % name

if name == 'main': fire.Fire(hello)

代码语言:javascript
复制
  -  运行
```sh
python hello.py  # Hello World!
python hello.py --name=David  # Hello David!
python hello.py --help  # Shows usage information

完整示例

代码语言:javascript
复制
# -*- coding:utf-8 -*-
# /usr/bin/python
'''
-------------------------------------------------
   File Name   :  envsInfo.py
   Description :  确认开发环境
   Run Script  :  python  envsInfo.py all
       Param   :  -all, sys, py, qlib
   Envs        :  python == 3.66
   Date        :  2021/3/4  下午4:58
   CodeStyle   :  standard, simple, readable, maintainable, and portable!
-------------------------------------------------
   Change Activity:
          2021/3/4 : build
-------------------------------------------------
__Author__ = "Yan Errol 13075851954"
__Email__ = "260187357@qq.com"
__Copyright__ = "Copyright 2021, Yan Errol"
-------------------------------------------------
'''
import warnings
warnings.filterwarnings("ignore")

import sys,os
import platform
import qlib
import fire
import pkg_resources
from pathlib import Path

QLIB_PATH = Path(__file__).absolute().resolve().parent.parent


class InfoCollector:
    """
    User could collect system info by following commands
    `cd scripts && python collect_info.py all`
    - NOTE: please avoid running this script in the project folder which contains `qlib`
    """

    def sys(self):
        """collect system related info"""
        for method in ["system", "machine", "platform", "version"]:
            print(getattr(platform, method)())

    def py(self):
        """collect Python related info"""
        print("Python version: {}".format(sys.version.replace("\n", " ")))

    def qlib(self):
        """collect qlib related info"""
        print("Qlib version: {}".format(qlib.__version__))
        REQUIRED = [
            "numpy",
            "pandas",
            "scipy",
            "requests",
            "sacred",
            "python-socketio",
            "redis",
            "python-redis-lock",
            "schedule",
            "cvxpy",
            "hyperopt",
            "fire",
            "statsmodels",
            "xlrd",
            "plotly",
            "matplotlib",
            "tables",
            "pyyaml",
            "mlflow",
            "tqdm",
            "loguru",
            "lightgbm",
            "tornado",
            "joblib",
            "fire",
            "ruamel.yaml",
            "tensorflow==1.15.0",
            "lxml",
            "yahooquery"
        ]

        for package in REQUIRED:
            version = pkg_resources.get_distribution(package).version
            print(f"{package}=={version}")

    def file(self):
        '''collect file info'''
        print("pwd:{}".format(os.getcwd()))

    def all(self):
        """collect all info"""
        for method in ["sys", "py", "qlib","file"]:
            getattr(self, method)()
            print()


if __name__ == "__main__":
    fire.Fire(InfoCollector)

•运行

•python envsInfo.py all

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

目录
  • fire
  • install
  • 简单实例
  • 完整示例
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档