1

I have a python script which makes an http request to an API in order to pull "member" data. It then runs a loop to hit several different member ID's (listed in a text file) to see if each of them are "expired" or not. What I would like to do is print out a summary of these results instead of printing out the individual results for each of the members. Here is my Python script:

import requests
import json

qa_members = open("numbers_members.txt")
arraylist = []
    for line in qa_members.readlines():
    arraylist.extend(line.split())
qa_members.close()

arraylist = map(int, arraylist)


authorization_code = raw_input("please enter your authorization code: ")
members = arraylist
print "Member Check Starting"

for member in members:
    url = "http://api22.wiki.com/v1.12/member?id="+str(member)  

    header = {"Authorization": authorization_code}

    response = requests.get(url, headers=header)

    member_check = json.loads(response.text)

    final_member_status = member_check["response"]["member"]
    ["is_expired"]

    print str(member) + " expired status: " + str(final_member_status)


if final_member_status == False:
    print "All Members Passed"
else:
    print "Some members are expired, please refer to log"

print "Member Check Complete"

From this, I get several terminal outputs like:

123 expired status: False
567 expired status: False

Is there way to instead, print out a summary of the results? It could be something like:

"24 members returned False"
"11 members returned True"

It would also be helpful to find out the ID of a particular member if the status returned was "true":

"Member 1234 returned True"

Or something similar to that. Any help is greatly appreciated.

Thanks!

user7681184
  • 893
  • 2
  • 12
  • 24

3 Answers3

0

A first thought:

falsecount = 0
truecount = 0
for member in members:
    url = "http://api22.wiki.com/v1.12/member?id="+str(member)  

    header = {"Authorization": authorization_code}

    response = requests.get(url, headers=header)

    member_check = json.loads(response.text)

    final_member_status = member_check["response"]["member"]["is_expired"]

    if final_member_status = False:
        falsecount += 1
    else:
        truecount +=1

print str(falsecount) + "members returned False"
print str(truecount) + "members returned False"

EDIT

I am unable to check however what the output of final_member_status = member_check["response"]["member"]["is_expired"] is. This could be "False" or False (thus, a string or an actual evaluation), if it is "False" (a string) then my line should read if final_member_status = "False", however, if it is an evaluation (returning False if the mentioned son field does not exist) the above code is correct.

Montmons
  • 1,416
  • 13
  • 44
0

create a list and append your results to it:

results = []

for member in members:
    (...)
    results.append(str(final_member_status))

How can I count the occurrences of a list item in Python?

>>> [1, 2, 3, 4, 1, 4, 1].count(1)
3
Community
  • 1
  • 1
litepresence
  • 3,109
  • 1
  • 27
  • 35
  • This option is way slower then creating an int value and simply counting `+1` in each round. However, if you'd like to be able to backtrace the output it is indeed a better choice. – Montmons Apr 01 '17 at 18:30
0

You can use two counters outside the for loop and then increase the counters based on True or False, something like this :

# Set both counts to 0 outside the for loop
true_count = 0
false_count = 0

# increment count values based on check inside the for loop
if final_member_status : 
    true_count = true_count + 1
else :
    false_count = false_count + 1

# And finally print outside for loop in the desired format
print("{} members returned True".format(str(true_count)))
print("{} members returned False".format(str(false_count)))

This will result the aggregated sum of both checks.

Satish Prakash Garg
  • 2,213
  • 2
  • 16
  • 25