def heatMap(pathOrCode): '''显示python代码的时间热力图 ps.会让代码里面的中文全部失效 Parameters ---------- path : str of code or path of .py .py文件路径或着python代码 ''' from pyheat import PyHeat path = '/tmp/pyheat-tmp.py' code = pathOrCode try : if os.path.isfile(pathOrCode): path = pathOrCode+'_HEAT_MAP_TMP.py' with open(pathOrCode) as f: code = f.read() code = code.decode('ascii','replace').replace(u'\ufffd','$?') with open(path,'w') as f: f.write(code) ph = PyHeat(path) ph.create_heatmap() ph.show_heatmap() finally: if os.path.isfile(path): os.remove(path)
class PyHeatTest(unittest.TestCase): def setUp(self): data_path = os.path.dirname(os.path.realpath(__file__)) self.pyheat = PyHeat(data_path + "/test_program.py") def test_pyheat_show(self): """Basic test to check pyheat class works end to end.""" self.pyheat.create_heatmap() self.pyheat.show_heatmap(blocking=False) def tearDown(self): self.pyheat.close_heatmap()
def heat(self, line, cell): """Method to profile the python code in the ipython cell and display it as a heatmap using py-heat package. :param line: Line value for the ipython line this magic is called from. :param cell: Cell value for the ipython cell this magic is called from. """ args = magic_arguments.parse_argstring(self.heat, line) filename = args.out if filename is not None: filename = os.path.expanduser(args.out) tmp_file = 'ipython_cell_input.py' with open(tmp_file, 'wb') as f: f.write(cell.encode()) pyheat = PyHeat(tmp_file) pyheat.create_heatmap() pyheat.show_heatmap(output_file=filename) pyheat.close_heatmap()
def setUp(self): data_path = os.path.dirname(os.path.realpath(__file__)) self.pyheat = PyHeat(data_path + "/test_program.py")
def heatmap(pathOrCode): '''show the heatmap of code or python file if raise UnicodeDecodeError in Python 2 which may cause by Chinese, Japaneses then will replace all symbol not belong ascii to "?$" Parameters ---------- pathOrCode : str .py file path or Python code Chinese: 会让代码里面的中文全部失效 Parameters ---------- pathOrCode : str of code or path of .py .py文件路径或着python代码 ''' beforImportPlt() from pyheat import PyHeat import matplotlib.pyplot as plt tmppath = 'code-tmp-pyheat-boxx.py' ispath = pathOrCode.endswith('.py') path = pathOrCode if ispath else tmppath try: if not ispath: with open(tmppath, 'w') as f: f.write(pathOrCode) ph = PyHeat(path) ph.create_heatmap() ph.show_heatmap() except UnicodeDecodeError: plt.show() msg = '''UnicodeDecodeError! try to replace not ascii symbol to '$?' and retry''' from boxx import warn warn(msg) with open(path) as f: code = f.read() code = code.decode('ascii', 'replace').replace('\ufffd', '$?') with open(tmppath, 'w') as f: f.write(code.encode('utf-8')) ph = PyHeat(tmppath) ph.create_heatmap() ph.show_heatmap() finally: if os.path.isfile(tmppath): os.remove(tmppath)
#%% from pyheat import PyHeat ph = PyHeat('C:\Study\VScode\TestJupyter\code.py') ph.create_heatmap() # To view the heatmap. ph.show_heatmap()