Esempio n. 1
0
def crun(pycode, snakeviz=True):
    '''
    use snakeviz and cProfile to analyse the code performance
    a visualization flame graph web page will be opened in your web browser
    
    Parameters
    ----------
    pycode : str
        Python code
    snakeviz : bool, default True
        use snakeviz to get flame graph in web page
        otherwise, print cProfile result sorted by time
    '''
    from cProfile import run

    if not snakeviz:
        return run(pycode, sort='time')
    import webbrowser
    if not webbrowser.open(''):
        from boxx import warn
        msg = '''**Can't detect browser** in operating environment.
so, we use cProfile.run(pycode,sort='time'),
instead of using snakeviz to visualization code perfomance in web page'''
        warn(msg)
        run(pycode, sort='time')
        print('\n\n' + msg)
        return
    run(pycode, os.path.join(tmpYl, "snakeviz.result"))
    if not sysi.win:
        from . import softInPath
        assert softInPath('snakeviz'), 'run `pip install snakeviz`'
        os.system('snakeviz %s &' % os.path.join(tmpYl, 'snakeviz.result'))
    elif sysi.win:
        os.system('start /b  snakeviz.exe %s ' %
                  os.path.join(tmpYl, 'snakeviz.result'))
Esempio n. 2
0
def warn1time(msg, *l, **kv):
    '''
    log a warning of type warnType warn will auto fill filename and line 
    
    warn only one time
    '''
    if not warn1timeCache.get(msg):
        warn(msg, *l, **kv)
        warn1timeCache[msg] = True
Esempio n. 3
0
def browserOpen(url):
    '''
    open url with browser
    if can't open browser raise warn
    '''
    import webbrowser
    if not webbrowser.open_new_tab(url):
        from boxx import warn
        warn('''can't open url with web browser, plaese open url:"%s" in your browser'''%url)
Esempio n. 4
0
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()
    try:
        from pyheat import PyHeat
    except ModuleNotFoundError as e:
        print("Please pip install py-heat!")
        raise e
    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)