def printex(ex, msg='[!?] Caught exception', prefix=None, key_list=[], locals_=None, iswarning=False, tb=TB, pad_stdout=True, N=0, use_stdout=False, reraise=False, msg_=None, keys=None): """ Prints (and/or logs) an exception with relevant info Args: ex (Exception): exception to print msg (str): a message to display to the user keys (None): a list of strings denoting variables or expressions of interest iswarning (bool): prints as a warning rather than an error if True (defaults to False) tb (bool): if True prints the traceback in the error message pad_stdout (bool): separate the error message from the rest of stdout with newlines prefix (None): locals_ (None): N (int): use_stdout (bool): reraise (bool): msg_ (None): key_list (list): DEPRICATED use keys Returns: None """ if isinstance(ex, MemoryError): import utool as ut ut.print_resource_usage() #ut.embed() if keys is not None: # shorthand for key_list key_list = keys # Get error prefix and local info if prefix is None: prefix = get_caller_prefix(aserror=True, N=N) if locals_ is None: locals_ = get_caller_locals(N=N) # build exception message if msg is True: key_list = get_caller_locals() msg = msg_ exstr = formatex(ex, msg, prefix, key_list, locals_, iswarning, tb=tb) # get requested print function if use_stdout: def print_func(*args): msg = ', '.join(map(str, args)) sys.stdout.write(msg + '\n') sys.stdout.flush() else: print_func = print if pad_stdout: print_func('\n+------\n') # print the execption print_func(exstr) if pad_stdout: print_func('\nL______\n') # If you dont know where an error is coming from raise-all if (reraise and not iswarning) or RAISE_ALL: sys.stdout.flush() sys.stderr.flush() raise ex
def devmain(): """ The Developer Script A command line interface to almost everything -w # wait / show the gui / figures are visible --cmd # ipython shell to play with variables -t # run list of tests Examples: """ helpstr = ut.codeblock( ''' Dev is meant to be run as an interactive script. The dev.py script runs any test you regiter with @devcmd in any combination of configurations specified by a Config object. Dev caches information in order to get quicker results. # FIXME: Provide quicker results # FIXME: len(line) ''') INTRO_TITLE = 'The dev.py Script' #INTRO_TEXT = ''.join((ut.bubbletext(INTRO_TITLE, font='cybermedium'), helpstr)) INTRO_TEXT = ut.bubbletext(INTRO_TITLE, font='cybermedium') INTRO_STR = ut.msgblock('dev.py Intro', INTRO_TEXT) EXAMPLE_STR = ut.msgblock('dev.py Examples', ut.codeblock(EXAMPLE_TEXT)) if ut.NOT_QUIET: print(INTRO_STR) if ut.get_argflag(('--help', '--verbose')): print(EXAMPLE_STR) CMD = ut.get_argflag('--cmd') NOGUI = not ut.get_argflag('--gui') if len(sys.argv) == 1: print('Run dev.py with arguments!') sys.exit(1) # Run Precommands run_devprecmds() # # # Run IBEIS Main, create controller, and possibly gui print('++dev') main_locals = ibeis.main(gui=ut.get_argflag('--gui')) #utool.set_process_title('IBEIS_dev') # # # Load snippet variables SNIPPITS = True and CMD if SNIPPITS: snippet_locals = dev_snippets(main_locals) snippet_execstr = utool.execstr_dict(snippet_locals, 'snippet_locals') exec(snippet_execstr) # # # Development code RUN_DEV = True # RUN_DEV = '__IPYTHON__' in vars() if RUN_DEV: dev_locals = run_dev(main_locals['ibs']) dev_execstr = utool.execstr_dict(dev_locals, 'dev_locals') exec(dev_execstr) command = ut.get_argval('--eval', type_=str, default=None) if command is not None: result = eval(command, globals(), locals()) print('result = %r' % (result,)) #ibs.search_annot_notes('360') # # # Main Loop (IPython interaction, or some exec loop) #if '--nopresent' not in sys.argv or '--noshow' in sys.argv: ut.show_if_requested() if ut.get_argflag(('--show', '--wshow')): pt.present() main_execstr = ibeis.main_loop(main_locals, ipy=(NOGUI or CMD)) exec(main_execstr) # # # Memory profile if ut.get_argflag('--memprof'): utool.print_resource_usage() utool.memory_profile() print('exiting dev')
def devmain(): """ The Developer Script A command line interface to almost everything -w # wait / show the gui / figures are visible --cmd # ipython shell to play with variables -t # run list of tests Examples: """ helpstr = ut.codeblock(''' Dev is meant to be run as an interactive script. The dev.py script runs any test you regiter with @devcmd in any combination of configurations specified by a Config object. Dev caches information in order to get quicker results. # FIXME: Provide quicker results # FIXME: len(line) ''') INTRO_TITLE = 'The dev.py Script' #INTRO_TEXT = ''.join((ut.bubbletext(INTRO_TITLE, font='cybermedium'), helpstr)) INTRO_TEXT = ut.bubbletext(INTRO_TITLE, font='cybermedium') INTRO_STR = ut.msgblock('dev.py Intro', INTRO_TEXT) EXAMPLE_STR = ut.msgblock('dev.py Examples', ut.codeblock(EXAMPLE_TEXT)) if ut.NOT_QUIET: print(INTRO_STR) if ut.get_argflag(('--help', '--verbose')): print(EXAMPLE_STR) CMD = ut.get_argflag('--cmd') NOGUI = not ut.get_argflag('--gui') if len(sys.argv) == 1: print('Run dev.py with arguments!') sys.exit(1) # Run Precommands run_devprecmds() # # # Run IBEIS Main, create controller, and possibly gui print('++dev') main_locals = ibeis.main(gui=ut.get_argflag('--gui')) #utool.set_process_title('IBEIS_dev') # # # Load snippet variables SNIPPITS = True and CMD if SNIPPITS: snippet_locals = dev_snippets(main_locals) snippet_execstr = utool.execstr_dict(snippet_locals, 'snippet_locals') exec(snippet_execstr) # # # Development code RUN_DEV = True # RUN_DEV = '__IPYTHON__' in vars() if RUN_DEV: dev_locals = run_dev(main_locals['ibs']) dev_execstr = utool.execstr_dict(dev_locals, 'dev_locals') exec(dev_execstr) command = ut.get_argval('--eval', type_=str, default=None) if command is not None: result = eval(command, globals(), locals()) print('result = %r' % (result, )) #ibs.search_annot_notes('360') # # # Main Loop (IPython interaction, or some exec loop) #if '--nopresent' not in sys.argv or '--noshow' in sys.argv: ut.show_if_requested() if ut.get_argflag(('--show', '--wshow')): pt.present() main_execstr = ibeis.main_loop(main_locals, ipy=(NOGUI or CMD)) exec(main_execstr) # # # Memory profile if ut.get_argflag('--memprof'): utool.print_resource_usage() utool.memory_profile() print('exiting dev')
def printex(ex, msg='[!?] Caught exception', prefix=None, key_list=[], locals_=None, iswarning=False, tb=TB, pad_stdout=True, N=0, use_stdout=False, reraise=False, msg_=None, keys=None, colored=None): """ Prints (and/or logs) an exception with relevant info Args: ex (Exception): exception to print msg (str): a message to display to the user keys (None): a list of strings denoting variables or expressions of interest iswarning (bool): prints as a warning rather than an error if True (defaults to False) tb (bool): if True prints the traceback in the error message pad_stdout (bool): separate the error message from the rest of stdout with newlines prefix (None): locals_ (None): N (int): use_stdout (bool): reraise (bool): msg_ (None): key_list (list): DEPRICATED use keys Returns: None """ import utool as ut if isinstance(ex, MemoryError): ut.print_resource_usage() if keys is not None: # shorthand for key_list key_list = keys # Get error prefix and local info if prefix is None: prefix = get_caller_prefix(aserror=True, N=N) if locals_ is None: locals_ = get_caller_locals(N=N) # build exception message if msg is True: key_list = get_caller_locals() msg = msg_ exstr = formatex(ex, msg, prefix, key_list, locals_, iswarning, tb=tb, colored=colored) # get requested print function if use_stdout: def print_func(*args): msg = ', '.join(list(map(six.text_type, args))) sys.stdout.write(msg + '\n') sys.stdout.flush() else: print_func = ut.partial(ut.colorprint, color='yellow' if iswarning else 'red') # print_func = print if pad_stdout: print_func('\n+------\n') # print the execption print_func(exstr) if pad_stdout: print_func('\nL______\n') # If you dont know where an error is coming from raise-all if (reraise and not iswarning) or RAISE_ALL: sys.stdout.flush() sys.stderr.flush() raise ex if ut.get_argflag('--exit-on-error'): print('WARNING: dont use this flag. Some errors are meant to be caught') ut.print_traceback() print('REQUESTED EXIT ON ERROR') sys.exit(1)
#!/usr/bin/env python2.7 # TODO: ADD COPYRIGHT TAG #from utool import util_resources import psutil import os if __name__ == '__main__': meminfo = psutil.Process(os.getpid()).get_memory_info() rss = meminfo[0] # Resident Set Size / Mem Usage vms = meminfo[1] # Virtual Memory Size / VM Size # NOQA print('+-----------------------') print('| BEFORE UTOOL IMPORT ') print('| * current_memory (before import) = %.2f MB' % (rss / (2.0 ** 20))) print('importing') import utool utool.print_resource_usage()
def printex(ex, msg='[!?] Caught exception', prefix=None, key_list=[], locals_=None, iswarning=False, tb=TB, pad_stdout=True, N=0, use_stdout=False, reraise=False, msg_=None, keys=None, colored=None): """ Prints (and/or logs) an exception with relevant info Args: ex (Exception): exception to print msg (str): a message to display to the user keys (None): a list of strings denoting variables or expressions of interest iswarning (bool): prints as a warning rather than an error if True (defaults to False) tb (bool): if True prints the traceback in the error message pad_stdout (bool): separate the error message from the rest of stdout with newlines prefix (None): locals_ (None): N (int): use_stdout (bool): reraise (bool): msg_ (None): key_list (list): DEPRICATED use keys Returns: None """ import utool as ut if isinstance(ex, MemoryError): ut.print_resource_usage() if keys is not None: # shorthand for key_list key_list = keys # Get error prefix and local info if prefix is None: prefix = get_caller_prefix(aserror=True, N=N) if locals_ is None: locals_ = get_parent_frame(N=N).f_locals # build exception message if msg is True: key_list = get_parent_frame().f_locals msg = msg_ exstr = formatex(ex, msg, prefix, key_list, locals_, iswarning, tb=tb, colored=colored) # get requested print function if use_stdout: def print_func(*args): msg = ', '.join(list(map(six.text_type, args))) sys.stdout.write(msg + '\n') sys.stdout.flush() else: print_func = ut.partial(ut.colorprint, color='yellow' if iswarning else 'red') # print_func = print if pad_stdout: print_func('\n+------\n') # print the execption print_func(exstr) if pad_stdout: print_func('\nL______\n') # If you dont know where an error is coming from raise-all if (reraise and not iswarning) or RAISE_ALL: sys.stdout.flush() sys.stderr.flush() raise ex if ut.get_argflag('--exit-on-error'): print('WARNING: dont use this flag. Some errors are meant to be caught') ut.print_traceback() print('REQUESTED EXIT ON ERROR') sys.exit(1)