当前位置:网站首页>Basic use of argparse Library
Basic use of argparse Library
2022-07-20 13:00:00 【Ten thousand miles and a bright future arrived in an instant】
argparse Kuo is python The next command line parameter management Library , Support int、str、float、bool、 Array etc. 5 Basic data types . Default values are also supported when parsing command line parameters . Besides , It also supports modifying command line parameters , Add command line parameters and other operations in the code . adopt argparse library , It can realize simple management of parameters , Control some process or parameter values in code execution .
Basic usage
1、int Support
argparse Writing ,class_nums The default value is 7
parser.add_argument("--class_nums", type=int, default=7)
Command line parameter passing ( Suppose the name of the program file is test.py), A new value was passed , Override default
test.py --class_nums 8
2、str Support
argparse Writing ,load_from No default value is set , If you want to set it, please set default
parser.add_argument("--load_from", type=str, help='load trained model to generate predictions of validation set')
Command line parameter passing ( Suppose the name of the program file is test.py), A new value was passed "best.pth" Assign a value to load_from
test.py --load_from "best.pth"
3、float Support
argparse Writing ,lr The default value is =0.0003
parser.add_argument("--lr", type=float, default=0.0003)
Command line parameter passing ( Suppose the name of the program file is test.py), A new value was passed , Override default
test.py --lr 0.001
4、bool Support
argparse Writing ,pretrained The default value is None.store_true Indicates that the incoming parameter is True
parser.add_argument("--pretrained", action="store_true")
Command line parameter passing ( Suppose the name of the program file is test.py), here pretrained The value of is True
test.py --pretrained
5、 Array support
argparse Writing ,input_shape The default value is [256, 256].nargs Indicates that there may be multiple values for the incoming parameter , That is to say, the parameters passed in are not necessarily 2 individual ,3 One can also
parser.add_argument('--input_shape', nargs='+', type=int, default=[256, 256])
Command line parameter passing ( Suppose the name of the program file is test.py), here input_shape The value of is [512, 512]
test.py --input_shape 512 512
Full call
The following code realizes the parameter value , Modify parameter value , Add a complete call to the parameter value . The default parameters cannot be modified by executing the following code in a non console environment , Parameters can only be modified by executing under the console
import argparse
class Options:
def __init__(self):
parser = argparse.ArgumentParser(' Namespace ')
parser.add_argument("--class_nums", type=int, default=7)
parser.add_argument("--pretrained", action="store_true")
parser.add_argument("--lr", type=float, default=0.0003)
parser.add_argument("--load_from", type=str,
help='load trained model to generate predictions of validation set')
parser.add_argument('--input_shape', nargs='+', type=int, default=[256, 256])
self.parser = parser
def parse(self):
args = self.parser.parse_args()
return args
if __name__ == "__main__":
args = Options().parse()
print("\n Original parameters :")
print(args)
print("\n Take the parameter value :")
print("args.class_nums:",args.class_nums)
print("\n Modify the parameters :")
args.input_shape=[512,512]
print(args)
print("\n The new parameters :")
args.new_key=[512,512]
print(args)
Blogger test code :python .\test.py --input_shape 128 128
The execution output is as follows :
Original parameters :
Namespace(class_nums=7, input_shape=[128, 128], load_from=None, lr=0.0003, pretrained=False)
Take the parameter value :
args.class_nums: 7
Modify the parameters :
Namespace(class_nums=7, input_shape=[512, 512], load_from=None, lr=0.0003, pretrained=False)
The new parameters :
Namespace(class_nums=7, input_shape=[512, 512], load_from=None, lr=0.0003, new_key=[512, 512], pretrained=False)
边栏推荐
- 黑客破解赌博网站漏洞每月“薅羊毛”10万
- Reflective Decoding: Beyond Unidirectional Generation with Off-the-Shelf Languag
- phpcms添加多文本属性字段_详细教程
- STL vector的输出
- P1087 [NOIP2004 普及组第三题] FBI 树
- C# CRC校验帮助类
- Live short video source code - the development sequence of live short video source code is five steps
- 想请教一下,我现在在珠海,到哪里开户比较好?请问手机开户股票开户安全吗?
- Web3创业具备创新爆炸式增长的所有要素
- 使用OneDNS完美解决办公网络优化问题
猜你喜欢
新闻速递 | 恭喜肖晓容工程师获得Domo专业认证!
Phpcms add multi text attribute field_ Detailed tutorial
Develop those things: how to solve the memory leak in the conversion between go and C type?
为什么 PSP22 对 Polkadot 生态系统很重要
【网络研究院】微软警告针对10,000多个组织的大规模AiTM网络钓鱼攻击
为什么独立站卖家喜欢做电子邮件营销?原来转化率能提高这么多!
Shuttle + Alluxio 加速内存Shuffle起飞
查找——B+树
Why psp22 is important to Polkadot ecosystem
LNMP environment, website cannot be accessed
随机推荐
Degradation technology of template and generic programming
[HDU 6095] Rikka with Competition
js获取url地址栏参数的方法,解决中文乱码问题
[Hongke news] Hongke electronics officially established a partnership with Weka
Web3将如何改变人来的未来?
用C#为国产智能手表写“Hello, China. ”
STM32+BH1750光敏传感器获取光照强度
Configuration et surveillance des fluides
[Mamba] is CONDA slow?
Wechat applet_ 18. Communication between parent and child components
Crontab execution time setting
Is it safe to open an account in Guangzhou securities?
Output of STL Vector
News Express | congratulations to engineer Xiao Xiaorong for obtaining Domo professional certification!
Golang callback function & closure
STL vector容量
微服务--熔断和限流
QIODevice
瀏覽器調試
出于对员工人身安全的担忧 星巴克未来可能关闭更多美国门店