来自json响应的解码二进制内容的行数

时间:2016-02-01 05:41:40

标签: python json github

下面有一些代码可以计算解码后的GitHub二进制内容中的行数,然后根据文件的更改次数查找百分比变化。它包含在if / else语句的循环内的循环中。我现在有什么工作,但它在pull请求中输出每个单独文件的结果。如果返回文件集中的任何结果满足if语句中的条件(否则打印没有文件已更改),我想写if / else一次,然后转到下一组进行评估。

     found = False
     for data in repo.pull_request(prs.number).files():
         if data.filename.endswith((".png",".jpeg",".gif")):
             pass
         else:
             for files_content in [repo.blob(data.sha)]:
                 binary_coded_content = io.BytesIO((base64.b64decode(files_content.content)))
                 tempfile = 'temp'
                 with open(tempfile,'wb') as f:
                     f.write(binary_coded_content.read())
                 num_lines = sum(1 for line in open(tempfile, encoding='utf8') if line.rstrip())
                 if data.changes_count/num_lines > 0.25:
                     found = True
                     break
     if found:
         print("A file has changed by more than 25%", '\n')
     else:
         print("No file has changed by more than 25%", '\n')

1 个答案:

答案 0 :(得分:1)

如果我理解正确,你想创建一个找到的变量并在循环之外测试它

found = False # <----
for data in repo.pull_request(prs.number).files():
    if data.filename.endswith((".png",".jpeg",".gif")):
        pass
    else:

        for files_content in [repo.blob(data.sha)]:
             binary_coded_content = io.BytesIO((base64.b64decode(files_content.content)))
             tempfile = 'temp'
             with open(tempfile,'wb') as f:
                 f.write(binary_coded_content.read())
             num_lines = sum(1 for line in open(tempfile, encoding='utf8') if line.rstrip()) 
             if data.changes_count/num_lines > 0.25:
                found = True # <----
                break #<--- break inner loop
        if found:
            break #<--- break outer loop
    if found: # after the loop ended, we check if we found something...
        print("A file has changed by more than 25%")
    else:
        print("No file has changed by more than 25%")