pythongithub3.py

Count total number of lines in a github pull request file


I'm using the github3.py library to query changes in files associated with pull requests.

I have the code written below that retrieves the total number of line changes made to the file. I would like to expand this to come up with a percentage of change based on the total number of lines in the file. Has anyone found a way to get the total lines in a file using the github3.py library? Reading the github3.py docs and the GitHub api docs, I don't see a clear way to do this.

for prs in repo.pull_requests():
    for data in repo.pull_request(prs.number).files():
        capture_changes = data.as_dict()
        print(capture_changes['changes'])

Update: After using readlines() as suggested by user5823815, I came up with this which worked, but I have a redirection issue that I will address in another question.

import urllib
from urllib import requests
response = urllib.request.urlopen("https://source_url")
lines = response.readlines()
num_lines = sum(1 for line in lines if line.rstrip())
print(num_lines)

Solution

  • For the doc you could use readlines() to take all lines and assign them to a list. Measure the length of the list it would produce. Then take the length of the pull and simply divide the pull by the length of the doc.