So a couple of things here.
The Github API will return a JSON. From the possible response available here it will not provide a list, but a dict (after conversion from JSON to a python object). I think flakes was correct in his comment.
As to your main issue, I think it has to do with the fact that you use word[0]
and word[1]
. You are slicing a simple string here. Because of the for loop, each word
variable will contain one of the elements from the polite_words
. There's no need to actually do any slicing here.
Take a look at the logic below, it will trigger on any comment that contains one of the strings in the polite_words
variable.
Also note that if you need the JSON from a request, using the requests library, you can just use response.json()
. See the assertion in the logic below. Do please note that I'm doing the simplest assertion to determine if the JSON values are the same.
import json
import requests
comment_url = "https://api.github.com/repos/octocat/Hello-World/pulls/comments/1"
resp = requests.get(comment_url)
text = resp.text
js = resp.json()
print(type(text), text)
print(type(js), js)
pull_request_comment = json.loads(text)
print(type(pull_request_comment), pull_request_comment)
# assert if the values from js and pull_request_comment are the same
assert json.dumps(js, sort_keys=True) == json.dumps(pull_request_comment, sort_keys=True)
polite_words = ("please", "appreciate", "would be great")
# safety check to see if we actually got a body
if "body" in pull_request_comment:
polite = False
for word in polite_words: # Loop through each element of reportTuple
if word in pull_request_comment["body"]:
polite = True
break # no need for further processing
if polite:
print("You are polite!!!")
else:
print("You are impolite!!!")
else:
print(f"no body found in response:\n{text}")
EDIT
So changing the processing of single comments into a function, and then doing a simple check on whether you got a list or a dict (after conversion) from the API, you can call the funcion based on that.
See the adjusted code below...
import json
import requests
polite_words = ("please", "appreciate", "would be great")
def process_comment(comment_dict: dict):
# safety check to see if we actually got a body
if "body" in comment_dict:
polite = False
for word in polite_words: # Loop through each element of reportTuple
if word in comment_dict["body"]:
polite = True
break # no need for further processing
if polite:
return "You are polite!!!"
else:
return "You are impolite!!!"
else:
return f"no body found in response:\n{text}"
comment_url = "https://api.github.com/repos/octocat/Hello-World/pulls/comments/1"
resp_json = requests.get(comment_url).json()
results = []
if type(resp_json) == dict:
results.append(process_comment(resp_json))
elif type(resp_json) == list:
results.extend([process_comment(elem) for elem in resp_json])
else:
print(f"unexpected return from API:\n{resp_json}")
print(results)
To actually answer your question about how JSON is converted, it depends. Both a list and a dict are valid JSON objects. The only thing that the library does, is to make python objects from JSON representations. As to what actually is a valid JSON, the actual RFC8259 has the full spec for it.
In that regard, this works:
huh = json.loads('"value"')
print(huh)
output
value