dfa.py 文件源码

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

项目:revex 作者: lucaswiman 项目源码 文件源码
def _draw(self, full=False):  # pragma: no cover
        # type: (bool) -> None
        """
        Hack to draw the graph and open it in preview or display in an ipython
        notebook (if running).
        """
        import os, sys, tempfile, shutil
        if full:
            graph = self.as_multidigraph
        else:
            graph = self.live_subgraph

        invisible_start_node = object()
        graph.add_node(invisible_start_node, attr_dict={'label': ''}, color='white')
        graph.add_edge(invisible_start_node, self.start, attr_dict={'label': ' start'})

        if 'ipykernel' in sys.modules:
            import IPython.display
            directory = tempfile.mkdtemp()
            dotpath = os.path.join(directory, 'out.dot')
            pngpath = os.path.join(directory, 'out.png')
            try:
                nx.drawing.nx_agraph.write_dot(graph, dotpath)
                os.system(
                    'dot -Tpng {dotpath} -o {pngpath}'.format(**locals()))
                with open(pngpath, 'rb') as f:  # type: ignore
                    IPython.display.display(IPython.display.Image(data=f.read()))
            finally:
                shutil.rmtree(directory)
        elif sys.platform == 'darwin':
            nx.drawing.nx_agraph.write_dot(graph, '/tmp/foo_%s.dot' % id(graph))
            os.system(
                'dot -Tpng /tmp/foo_{0}.dot -o /tmp/foo_{0}.png'.format(id(graph)))
            os.system('open /tmp/foo_{0}.png'.format(id(graph)))
        else:
            raise NotImplementedError(
                'See https://github.com/lucaswiman/revex/issues/19. '
                'Patches welcome!')
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号