recipe-440501.py 文件源码

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

项目:code 作者: ActiveState 项目源码 文件源码
def load_comments(self, pkgfile):
    """ Open the package and load comments if any. 
    Return the loaded comments """

        # Note: This has to be called with a Python
        # source file (.py) only!

    if not os.path.exists(pkgfile):
        return ""

    comment = ""

    try:
        of = open(pkgfile,'rb')
        data = of.read()
        if data:
        # Create code object
                try:
                    c = compiler.compile(data,pkgfile,'exec')
                    # Get the position of first line of code
                    if c:
                        lno = c.co_firstlineno
                        lnum = 0
                        # Read file till this line number
                        of.seek(0)
                        for line in of:
                            comment = "".join((comment, line))
                            lnum += 1
                            if lnum==lno or line=="\n": break
                except SyntaxError, e:
                    pass
                except Exception, e:
                    pass
        of.close()
    except (OSError, IOError, TypeError), e:
            pass

    return comment
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号