1 Star 0 Fork 0

陨石旁的天际/python-argparse

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
python-argparse.py 2.00 KB
一键复制 编辑 原始数据 按行查看 历史
陨石旁的天际 提交于 2023-09-12 23:46 . add extra option
#!/usr/bin/env python3
#
import argparse
def numbers_set(nlist):
# e.g. 1,2,4-6,20 => 1,2,4,5,6,20
if nlist is None or len(nlist.strip()) == 0:
return set()
nums = []
for n in nlist.strip().split(','):
if '-' in n:
b, e = n.split('-')
nums.extend(list(range(int(b), int(e) + 1)))
else:
nums.append(int(n))
return set(nums)
def args_parser():
comment = ("A simple of parse number list.")
parser = argparse.ArgumentParser(description = comment,
formatter_class =
argparse.RawTextHelpFormatter)
parser.add_argument("-m", dest = "m", action = "store",
help = "memory bind nodes")
parser.add_argument("-c", dest = "c", action = "store",
help = "CPU bind nodes")
parser.add_argument("-N", dest = "N", type = int, action = "store",
default = 8, help = "NUMA nodes")
parser.add_argument("extra", nargs = "*",
help = "extra arguments")
return parser
def usage():
args_parser().parse_args(["-h"])
def parse_args(args_list = None):
args = args_parser().parse_args(args_list)
memnodes = numbers_set(args.m)
if len(memnodes) == 0 and not args.N is None:
memnodes = set(range(0, args.N))
setattr(args, "memnodes", memnodes)
cpunodes = numbers_set(args.c)
if len(cpunodes) == 0 and not args.N is None:
cpunodes = set(range(0, args.N))
setattr(args, "cpunodes", cpunodes)
return args
args = parse_args()
print(args)
result = {}
for c in args.cpunodes:
row = {}
for m in args.memnodes:
# TODO
row[m] = str(c) + str(m)
result[c] = row
# 表头
print("CPU\MEM", end = "")
for m in args.memnodes:
print("\t%d" % m, end = "")
print("")
for k1, v1 in result.items():
# 列头
print("%d" % k1, end = "")
for k2, v2 in v1.items():
print("\t%s" % v2, end = "")
print("")
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/xunknown/python-argparse.git
git@gitee.com:xunknown/python-argparse.git
xunknown
python-argparse
python-argparse
master

搜索帮助