repo_health_checkup.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:interview-with-python 作者: thundergolfer 项目源码 文件源码
def check_amount_of_commenting( readable ):
    """
    Check lines of file to see whether it has a lot of commenting in it,
    which we take to mean the presence of code explanation.
    """
    # TODO: I have hacked this code together and it could be silly and buggy.
    #       It does seem to get things roughly correct, which is good enough for the moment
    prev_toktype = token.INDENT
    first_line = None
    last_lineno = -1
    last_col = 0
    comment_lines = 0
    tokgen = tokenize.generate_tokens( readable )
    for toktype, ttext, (slineno, scol), (elineno, ecol), ltext in tokgen:
        if 1:   # Change to if 1 to see the tokens fly by.
            print("%10s %-14s %-20r %r" % (
                tokenize.tok_name.get(toktype, toktype),
                "%d.%d-%d.%d" % (slineno, scol, elineno, ecol),
                ttext, ltext
                ))
        if slineno > last_lineno:
            last_col = 0
        if toktype == token.STRING and prev_toktype == token.INDENT:
            # Docstring
            comment_lines += 1
        elif toktype == tokenize.COMMENT:
            # Comment
            comment_lines += 1
        prev_toktype = toktype
        last_col = ecol
        last_lineno = elineno
    return comment_lines, last_lineno # roughly num of comment lines divided by num of lines
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号