iprofiler.py 文件源码

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

项目:iprofiler 作者: j-towns 项目源码 文件源码
def generate_lprofile(self, fun):
        """
        Generate div containing profiled source code with timings of each line,
        taken from iline_profiler.
        """
        self.value_lprofile = ""
        try:
            filename = fun.co_filename
            firstlineno = fun.co_firstlineno
            name = fun.co_name
        except AttributeError:
            return

        ltimings_key = (filename, firstlineno, name)

        try:
            ltimings = self.lprofile.timings[ltimings_key]
        except KeyError:
            return

        # Currently the correct filename is stored at the end of ltimings.
        # This is a work-around to fix cProfiler giving useless filenames for
        # zipped packages.
        filename = ltimings[-1]

        if filename.endswith(('.pyc', '.pyo')):
            filename = openpy.source_from_cache(filename)
        if ".egg/" in filename:
            add_zipped_file_to_linecache(filename)

        raw_code = ""
        linenos = range(firstlineno, ltimings[-2][0] + 1)

        for lineno in linenos:
            raw_code += ulinecache.getline(filename, lineno)

        formatter = LProfileFormatter(firstlineno, ltimings, noclasses=True)
        self.value_lprofile = highlight(raw_code, PythonLexer(), formatter)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号