为什么我的平均计算输出在错误的时间打印?

时间:2017-02-21 18:41:43

标签: python loops average

尝试从for循环中返回的值中获取平均值,并将其打印在组名旁边。我可以得到它来计算平均值并在返回下打印,但不在第2组中的第一个打印件旁边打印...

当我在第2组中添加参数时,我收到此错误:

    print "Group 2 - %s%%" % (avg)
NameError: name 'avg' is not defined

不确定我做错了什么,请帮忙。如果有人有更简单的方法,请随时提出建议。

我正在寻找的最终结果如下:

Group 1 - 100%
name1    100
name2    100
name3    100

Group 2 - 100%
name1    100
name2    100
name3    100

到目前为止,这是我的脚本:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import json
import pycurl
import time
from io import BytesIO

# Which monitors should data be retrieved for?
grp1 = ['000000000000000000', '000000000000000000', '000000000000000000']
grp2 = ['000000000000000000', '000000000000000000', '000000000000000000']

# Make calls to get the availability details
def connectMethod(method, url):
    c = pycurl.Curl()
    connectReturn = BytesIO()
    c.setopt(pycurl.URL, url)
    c.setopt(pycurl.HTTPHEADER, ["Authorization: authtoken 00000000000000000000000000000000"])
    c.setopt(c.WRITEFUNCTION, connectReturn.write)
    c.setopt (pycurl.CUSTOMREQUEST, method)
    c.perform()
    c.close()
    connectOutput = connectReturn.getvalue()
    return connectOutput

returned_items = {}
avail = list()

print "Group 1"
for item in group1:
    base_url = 'https://www.domain.com/api/reports/summary/'
    putData = item + '?period=13&unit_of_time=1'
    req_url = base_url + putData
    listOfAvail = json.loads(connectMethod('GET', req_url))
    returned_items[item] = listOfAvail
    name = listOfAvail['data']['info']['resource_name']
    avail_pct = listOfAvail['data']['summary_details']['availability_percentage']
    avg_avail = avail.append(avail_pct)
    print "%s\t%s%%" % (name, avail_pct)

avg = float(sum(avail))/len(avail)
print avg

print "Group 2 - %s%%" % (avg)
for item in group2:
    base_url = 'https://www.domain.com/api/reports/summary/'
    putData = item + '?period=13&unit_of_time=1'
    req_url = base_url + putData
    listOfAvail = json.loads(connectMethod('GET', req_url))
    returned_items[item] = listOfAvail
    name = listOfAvail['data']['info']['resource_name']
    avail_pct = listOfAvail['data']['summary_details']['availability_percentage']
    avg_avail = avail.append(avail_pct)
    print "%s\t%s%%" % (name, avail_pct)

avg = float(sum(avail))/len(avail)
print avg

1 个答案:

答案 0 :(得分:0)

快速,肮脏,丑陋,但它应该指向正确的方向让它弄明白并清理一下。

我刚刚修改了你的代码以输出你要求的内容 (我硬编码了您调用网络电话的名称和百分比)

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import json
import pycurl
import time
from io import BytesIO

# Which monitors should data be retrieved for?
group1 = ['000000000000000000', '000000000000000000', '000000000000000000']
group2 = ['000000000000000000', '000000000000000000', '000000000000000000']

# Make calls to get the availability details
def connectMethod(method, url):
    c = pycurl.Curl()
    connectReturn = BytesIO()
    c.setopt(pycurl.URL, url)
    c.setopt(pycurl.HTTPHEADER, ["Authorization: authtoken 00000000000000000000000000000000"])
    c.setopt(c.WRITEFUNCTION, connectReturn.write)
    c.setopt (pycurl.CUSTOMREQUEST, method)
    c.perform()
    c.close()
    connectOutput = connectReturn.getvalue()
    return connectOutput

def pprint(title, names, averages):
    avg = float(sum(percentages))/len(percentages)
    print "%s - %s%%" % (title, avg)
    for i in range(len(names)):
        print "%s\t%s" % (names[i], percentages[i])


returned_items = {}
percentages = []
names = []

for item in group1:
    base_url = 'https://www.domain.com/api/reports/summary/'
    putData = item + '?period=13&unit_of_time=1'
    req_url = base_url + putData
    #listOfAvail = json.loads(connectMethod('GET', req_url))
    returned_items[item] = {"name": "item 1" }  #listOfAvail
    name = "blah"  # listOfAvail['data']['info']['resource_name']
    avail_pct = 60  # listOfAvail['data']['summary_details']['availability_percentage']
    names.append(name)
    percentages.append(avail_pct)

pprint("Group 1", names, percentages)
print("")


del percentages[:]
del names[:]
for item in group2:
    base_url = 'https://www.domain.com/api/reports/summary/'
    putData = item + '?period=13&unit_of_time=1'
    req_url = base_url + putData
    #listOfAvail = json.loads(connectMethod('GET', req_url))
    returned_items[item] = {"name": "item 2" }  #listOfAvail
    name = "asdf"  #listOfAvail['data']['info']['resource_name']
    avail_pct = 87  #listOfAvail['data']['summary_details']['availability_percentage']
    names.append(name)
    percentages.append(avail_pct)

pprint("Group 1", names, percentages)

输出:

Group 1 - 60.0%
blah    60
blah    60
blah    60

Group 1 - 87.0%
asdf    87
asdf    87
asdf    87

基本上你只需要先执行所有计算,然后打印结果。

一些想法可以帮助你清理一下

代码重用for循环中的代码几乎相同。将此重构为您传递groupurl等的函数(这有意义吗)

查看requests库,它可能会压缩您的网络电话代码。