生成命令行接口--google开源的fire使用体验【python-fire】
在python中,命令行接口常用的argparse 和click,但是相对于python-fire 来说灵活度太缺了,fire可以直接将python中的函数,以命令行显示.
简单的介绍几个例子:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#filename: fire-demo.py
__author__ = 'xijun1'
import fire def demo(param):
"""
just a demo
:param param: any str
:return:
"""
print "hello {} !".format(param)
return; if __name__ == '__main__':
fire.Fire()
执行的格式为: python 如果fire.Fire()没有指定名称时:
需要在执行命令行时,带入函数名: python fire-demo.py demo ,同时,如果函数有参数是,需要输入参数,不然会不错,并提示:
python_demo python fire-demo.py demo
Fire trace:
1. Initial component
2. Accessed property "demo"
3. ('The function received no value for the required argument:', 'param') Type: function
String form: <function demo at 0x10e424c80>
File: ~/github/python_demo/fire-demo.py
Line: 20
Docstring: just a demo
:param param: any str
:return: Usage: fire-demo.py demo PARAM
fire-demo.py demo --param PARAM
如果带入参数: python fire-demo.py demo “google”
python_demo python fire-demo.py demo "google"
hello google !
或者我们也可以这样:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'xijun1'
import fire def demo(param):
"""
just a demo
:param param: any str
:return:
"""
print "hello {} !".format(param)
return; if __name__ == '__main__':
fire.Fire(demo)
只需要执行:
python_demo python fire-demo.py google
hello google !
当我们面对一个类Class的时候:
class ho:
def __init__(self,type):
self.type = type
pass def hodemo(self, param):
""" :param param: any string
:return:
"""
print "{} hello {} !".format( self.type,param)
return;
我们只需要执行:
python_demo python fire-demo.py ho --type "gxjun" hodemo "google"
gxjun hello google !
当然我们也可以将类写入Fire中:
python_demo python fire-demo.py --type gxjun hodemo google
gxjun hello google !
生成命令行接口--google开源的fire使用体验【python-fire】的更多相关文章
- Google 开源的 Python 命令行库:初探 fire
作者:HelloGitHub-Prodesire HelloGitHub 的<讲解开源项目>系列,项目地址:https://github.com/HelloGitHub-Team/Arti ...
- Google 开源的 Python 命令行库:深入 fire(二)
作者:HelloGitHub-Prodesire HelloGitHub 的<讲解开源项目>系列,项目地址:https://github.com/HelloGitHub-Team/Arti ...
- 利用中文数据跑Google开源项目word2vec
一直听说word2vec在处理词与词的相似度的问题上效果十分好,最近自己也上手跑了跑Google开源的代码(https://code.google.com/p/word2vec/). 1.语料 首先准 ...
- Google开源的Deep-Learning项目word2vec
用中文把玩Google开源的Deep-Learning项目word2vec google最近新开放出word2vec项目,该项目使用deep-learning技术将term表示为向量,由此计算te ...
- breakpad是Google开源的一套跨平台工具
windows下捕获dump之Google breakpad_client的理解 breakpad是Google开源的一套跨平台工具,用于dump的处理.很全的一套东西,我这里只简单涉及break ...
- [转]Google开源Leak Finder—用于检测内存泄漏的JavaScript工具-----可惜,暂时打不开google的网站,下载不了
近日,Google开源了Leak Finder,这款工具可以查看JavaScript应用的堆,进而发现内存泄漏. 作为一门垃圾收集语言,JavaScript并不会出现常见的内存泄露情况,特别是像C++ ...
- Google开源项目风格指南
Google开源项目风格指南 来源 https://github.com/zh-google-styleguide/zh-google-styleguide Google 开源项目风格指南 (中文版) ...
- Google 开源的依赖注入库,比 Spring 更小更快!
Google开源的一个依赖注入类库Guice,相比于Spring IoC来说更小更快.Elasticsearch大量使用了Guice,本文简单的介绍下Guice的基本概念和使用方式. 学习目标 概述: ...
- [Android]Google 开源的 Android 排版库:FlexboxLayout
最近Google开源了一个项目叫「FlexboxLayout」. 1.什么是 Flexbox 简单来说 Flexbox 是属于web前端领域CSS的一种布局方案,是2009年W3C提出了一种新的布局方 ...
随机推荐
- string通过逗号分割不用split方法
package com.simon; import java.util.ArrayList; import java.util.Iterator; import java.util.List; pub ...
- thinkphp5 Request请求类
获取请求类的几种方式: 1.助手函数(严格不算ba ) input('post.name'): 2.$request=\think\Request::instance(); 3.控制器中必须继承Con ...
- java设计模式之-观察者模式(发布-订阅模式)
1.观察者模式定义 观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象. 这个主题对象在状态上发生变化时,会通知所有观察者对象,让它们能够自动更新自己. 2.观察者模式结构 ...
- 大数据技术 - MapReduce的Shuffle及调优
本章内容我们学习一下 MapReduce 中的 Shuffle 过程,Shuffle 发生在 map 输出到 reduce 输入的过程,它的中文解释是 “洗牌”,顾名思义该过程涉及数据的重新分配,主要 ...
- 60.Search Insert Position.md
描述 给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引.如果没有,返回到它将会被按顺序插入的位置. 你可以假设在数组中无重复元素. 您在真实的面试中是否遇到过这个题? 样例 Given ...
- BZOJ.5289.[AHOI/HNOI2018]排列(贪心 heap)
BZOJ LOJ 洛谷 \(Kelin\)写的挺清楚的... 要求如果\(a_{p_j}=p_k\),\(k\lt j\),可以理解为\(k\)要在\(j\)之前选. 那么对于给定的\(a_j=k\) ...
- python基础一 ------如何根据字典值对字典进行"排序"
需求:{姓名:成绩} 的字典,按成绩进行排序 方法一:转化为元组,(91,"张三")的形式 ,用sorted()函数进行排序 方法二 :设置sorted() 中key的参数的值 # ...
- [CSAcademy]Sum of Powers
[CSAcademy]Sum of Powers 题目大意: 给定\(n,m,k(n,m,k\le4096)\).一个无序可重集\(A\)为合法的,当且仅当\(|A|=m\)且\(\sum A_i=n ...
- python学习—几个简单小程序
1. 输出1到100的偶数 #!/usr/bin/env python # -*- coding:utf-8 -*- #定义初始值 start=1 while True: #判断start的值若其为5 ...
- ubuntu 配置
这是安装语言包 sudo apt-get install language-pack-touch-zh-hans language-pack-touch-zh-hant 这是安装字体 sudo apt ...