这两天放假在家,拿py重写了之前的统计脚本,算是好好practice了一把各种基本用法,记录一下,为了将来的加速。。
- 判断命令行参数,给出Usage提示
if len(sys.argv) != 3:
print("Usage: %s <folder name> <12 or 24>" % sys.argv[0])
sys.exit(1)
- float 设置小数点位数
datatable[row][27] = round(float(ServerUtilDict[server]["avgUser"])/100, 4) # 用round函数控制位数
- 字典basic
遍历
ServerUtilDict, UtilDict = {}, {}
for (k, v) in FolderIPDict.items():
UtilDict = copy.deepcopy(UtilDict) if UtilDict else UtilDict
UtilDict = {}
ServerUtilDict[v] = UtilDict
# 以上,字典嵌套字典,利用FolderIPDict中的v作为ServerUtilDict中的k,对应的v为UtilDict,注意要deepcopy,不然指向同一个引用
字典构造
PowerDict = {} # Dict一定要先初始化
PowerDict["avg"] = line.split(" ")[0].strip() # 直接指定key赋值,对比list,要用append函数添加
字典引用
ServerPerfDict[server]["sql_nbr"] # 外层dict的v是另外一个dict,直接用内层dict的k来做引用
# sample
In [278]: util={}
In [279]: util["avg"] = 10
In [280]: util["max"] = 12
In [281]: util
Out[281]: {'avg': 10, 'max': 12}
In [283]: serverUtil = {}
In [284]: serverUtil["1.1.1.1"] = util
In [285]: serverUtil
Out[285]: {'1.1.1.1': {'avg': 10, 'max': 12}}
In [286]: serverUtil["1.1.1.1"]["avg"]
Out[286]: 10
In [287]: serverUtil["1.1.1.1"]["max"]
Out[287]: 12
- 一维列表,初始化,引用
In [293]: utilList = [] # 必须先要初始化
In [295]: utilList.append(13) # 往列表加东西,必须要append
In [296]: utilList.append(15)
In [297]: utilList
Out[297]: [13, 15]
In [298]: serverUtil["2.2.2.2"]=utilList # 把列表作为dict某个k的v
In [299]: serverUtil
Out[299]: {'1.1.1.1': {'avg': 10, 'max': 12}, '2.2.2.2': [13, 15]}
In [300]: serverUtil["2.2.2.2"][1] # 这样引用
Out[300]: 15
- 一维列表可以简单变二维甚至更多
In [307]: utilList
Out[307]: [13, 15]
In [308]: li=[1,2]
In [309]: li
Out[309]: [1, 2]
In [310]: li[0]
Out[310]: 1
In [311]: utilList.append(li)
In [312]: util
util utilList
In [312]: utilList
Out[312]: [13, 15, [1, 2]]
In [313]: utilList[2]
Out[313]: [1, 2]
In [314]: utilList[2][0]
Out[314]: 1
In [315]: utilList[2][1]
Out[315]: 2
- 删除列表元素
In [316]: utilList
Out[316]: [13, 15, [1, 2]]
In [317]: del utilList[1]
In [318]: utilList
Out[318]: [13, [1, 2]]
In [319]: del utilList[1][0]
In [320]: utilList
Out[320]: [13, [2]]
- python调用bash命令
cmd_getcpu = "grep -A 1 avg-cpu " + sdr + " | grep -v 'avg-cpu' | grep -v '\-\-' " \
"| awk -F ' ' '{print($1,$2,$3,$4,$5,$6)}' > tmpcpu"
os.system(cmd_getcpu) # create file "tmpcpu" in curent dir
#后面该怎处理怎么处理
- 遍历某个目录下面的子目录中的指定文件,用正则表达式匹配行,取出关键字
def getFolderIPDict(WorkingDir):
subfolders = os.walk(WorkingDir).next()[1] # check os.walk? to understand the return tuple structure
IPList = []
for i, value in enumerate(subfolders):
try:
if value.index("tdw") == 0:
IPList.append((value.replace("-", ".")).replace("tdw.", ""))
except ValueError:
print "Something wrong with sub-folders names, should be in such format: tdw-100-76-29-3"
sys.exit(1)
# if goes here, means "twd" not found in some of the sub-folder, probably structure wrong
FolderIPDict = dict(zip(subfolders, IPList))
return FolderIPDict
def getServerPerfDict(FolderIPDict):
ServerPerfFile = "sum_cron*"
ServerPerfDict, PerfDict = {}, {}
for (k, v) in FolderIPDict.items():
PerfDict = copy.deepcopy(PerfDict) if PerfDict else PerfDict
PerfDict = {}
ServerPerfDict[v] = PerfDict
with open(glob.glob(os.path.join(WorkingDir, k, ServerPerfFile))[0]) as f:
# f like ../0524-24H/tdw-100-76-29-2/sum_cron.2017-05-24.tdw-100-76-29-2
# only the 1st 10 lines in this file matters,,, could add "failure tasks info" later
count = 0
for line in f:
count += 1
if count == 3 and re.compile("Total executors mr").search(line):
PerfDict["mr_nbr"] = line.split(":")[1].strip()
continue
# print ServerPerfDict
# sys.exit(1)
return ServerPerfDict