def quitflag(num=None, embed_=False, parent_locals=None, parent_globals=None): if num is None or util_arg.get_argflag('--quit' + six.text_type(num)): if parent_locals is None: parent_locals = get_parent_locals() if parent_globals is None: parent_globals = get_parent_globals() exec(execstr_dict(parent_locals, 'parent_locals')) exec(execstr_dict(parent_globals, 'parent_globals')) if embed_: print('Triggered --quit' + six.text_type(num)) embed(parent_locals=parent_locals, parent_globals=parent_globals) print('Triggered --quit' + six.text_type(num)) sys.exit(1)
def quitflag(num=None, embed_=False, parent_locals=None, parent_globals=None): if num is None or util_arg.get_argflag('--quit' + str(num)): if parent_locals is None: parent_locals = get_parent_locals() if parent_globals is None: parent_globals = get_parent_globals() exec(execstr_dict(parent_locals, 'parent_locals')) exec(execstr_dict(parent_globals, 'parent_globals')) if embed_: print('Triggered --quit' + str(num)) embed(parent_locals=parent_locals, parent_globals=parent_globals) print('Triggered --quit' + str(num)) sys.exit(1)
def cached_closure(func): fname_ = util_inspect.get_funcname(func) if fname is None else fname kwdefaults = util_inspect.get_kwdefaults(func) argnames = util_inspect.get_argnames(func) cacher = Cacher(fname_, cache_dir=cache_dir, appname=appname) if use_cache is None: use_cache_ = not util_arg.get_argflag('--nocache-' + fname) #_dbgdict = dict(fname_=fname_, key_kwds=key_kwds, appname=appname, # key_argx=key_argx, use_cache_=use_cache_) @functools.wraps(func) def cached_wraper(*args, **kwargs): try: if True: print('[utool] computing cached function fname_=%s' % (fname_,)) # Implicitly adds use_cache to kwargs cfgstr = get_cfgstr_from_args(func, args, kwargs, key_argx, key_kwds, kwdefaults, argnames) assert cfgstr is not None, 'cfgstr=%r cannot be None' % (cfgstr,) if kwargs.get('use_cache', use_cache_): # Make cfgstr from specified input data = cacher.tryload(cfgstr) if data is not None: return data # Cached missed compute function data = func(*args, **kwargs) # Cache save cacher.save(data, cfgstr) return data except Exception as ex: import utool _dbgdict2 = dict(key_argx=key_argx, lenargs=len(args), lenkw=len(kwargs),) msg = '\n'.join([ '+--- UTOOL --- ERROR IN CACHED FUNCTION', #'dbgdict = ' + utool.dict_str(_dbgdict), 'dbgdict2 = ' + utool.dict_str(_dbgdict2), ]) utool.printex(ex, msg) raise # Give function a handle to the cacher object cached_wraper.cacher = cacher return cached_wraper
FUTURE_ON = False QUIET = util_arg.QUIET SILENT = util_arg.SILENT VERBOSE_PARALLEL, VERYVERBOSE_PARALLEL = util_arg.get_module_verbosity_flags('par', 'parallel') #VERBOSE_PARALLEL = util_arg.VERBOSE or util_arg.get_argflag(('--verbose-par', '--verbpar', '--verbose-parallel', '--verbparallel')) #VERYVERBOSE_PARALLEL = util_arg.VERYVERBOSE or util_arg.get_argflag(('--veryverbose-par', '--veryverbpar', '--veryverbose-parallel', '--veryverbparallel')) STRICT = util_arg.STRICT if SILENT: def print(msg): pass __POOL__ = None #__EAGER_JOIN__ = util_arg.get_argflag('--eager-join') __EAGER_JOIN__ = not util_arg.get_argflag('--noclose-pool') __NUM_PROCS__ = util_arg.get_argval('--num-procs', int, default=None) __FORCE_SERIAL__ = util_arg.get_argflag( ('--utool-force-serial', '--force-serial', '--serial')) #__FORCE_SERIAL__ = True # __SERIAL_FALLBACK__ = not util_arg.get_argflag('--noserial-fallback') __TIME_GENERATE__ = VERBOSE_PARALLEL or util_arg.get_argflag('--time-generate') # Maybe global pooling is not correct? USE_GLOBAL_POOL = util_arg.get_argflag('--use_global_pool') # FIXME: running tests in IBEIS has errors when this number is low # Due to the large number of parallel processes running? MIN_PARALLEL_TASKS = 4
from utool import util_print from utool import util_time from utool import util_iter from utool import util_dbg from utool import util_arg from utool import util_inject from utool._internal import meta_util_six try: import numpy as np HAVE_NUMPY = True except ImportError: HAVE_NUMPY = False (print, print_, printDBG, rrr, profile) = util_inject.inject(__name__, '[decor]') # Commandline to toggle certain convinience decorators SIG_PRESERVE = util_arg.get_argflag('--sigpreserve') #SIG_PRESERVE = not util_arg.SAFE or util_arg.get_argflag('--sigpreserve') ONEX_REPORT_INPUT = '--onex-report-input' in sys.argv #IGNORE_TRACEBACK = '--smalltb' in sys.argv or '--ignoretb' in sys.argv IGNORE_TRACEBACK = not ('--nosmalltb' in sys.argv or '--noignoretb' in sys.argv) # do not ignore traceback when profiling PROFILING = hasattr(builtins, 'profile') UNIQUE_NUMPY = True NOINDENT_DECOR = False #os.environ.get('UTOOL_AUTOGEN_SPHINX_RUNNING', 'OFF') #def composed(*decs): # """ combines multiple decorators """ # def deco(f):
import textwrap import keyword import re import types import functools from os.path import splitext, split, basename, dirname from utool import util_inject from utool import util_arg from utool import util_list from utool import util_print from utool import util_str from utool import util_type from utool._internal import meta_util_six print, print_, printDBG, rrr, profile = util_inject.inject(__name__, '[dbg]') RAISE_ALL = util_arg.get_argflag('--raise-all', help='Causes ut.printex to always reraise errors') FORCE_TB = util_arg.get_argflag('--force-tb', help='Causes ut.printex to always print traceback') # --- Exec Strings --- IPYTHON_EMBED_STR = r''' try: import IPython print('Presenting in new ipython shell.') embedded = True IPython.embed() except Exception as ex: warnings.warn(repr(ex)+'\n!!!!!!!!') embedded = False '''
from utool import util_arg from utool import util_time from utool import util_iter from utool import util_cplat from six.moves import range, zip import collections import six # NOQA print, rrr, profile = util_inject.inject2(__name__, '[progress]') default_timer = util_time.default_timer QUIET = util_arg.QUIET SILENT = util_arg.SILENT VALID_PROGRESS_TYPES = ['none', 'dots', 'fmtstr', 'simple'] AGGROFLUSH = util_arg.get_argflag('--aggroflush') PROGGRESS_BACKSPACE = not util_arg.get_argflag(('--screen', '--progress-backspace')) NO_PROGRESS = util_arg.get_argflag(('--no-progress', '--noprogress')) FORCE_ALL_PROGRESS = util_arg.get_argflag(('--force-all-progress',)) #('--screen' not in sys.argv and '--progress-backspace' not in sys.argv) DEBUG_FREQ_ADJUST = util_arg.get_argflag('--debug-adjust-freq') #PROGRESS_WRITE = sys.stdout.write #PROGRESS_FLUSH = sys.stdout.flush # FIXME: if this is loaded before logging beings # progress will not be logged #PROGRESS_WRITE = util_logging.__UTOOL_WRITE__ PROGRESS_WRITE = print
import thread as _thread elif six.PY3: import _thread import threading from utool._internal.meta_util_six import get_funcname from utool import util_progress from utool import util_time from utool import util_arg from utool import util_dbg from utool import util_inject from utool import util_cplat util_inject.noinject('[parallel]') QUIET = util_arg.QUIET SILENT = util_arg.SILENT PAR_VERBOSE = util_arg.VERBOSE or util_arg.get_argflag(('--verbose-par', '--verbpar')) STRICT = util_arg.STRICT if SILENT: def print(msg): pass __POOL__ = None __EAGER_JOIN__ = not util_arg.get_argflag('--noclose-pool') __TIME_GENERATE__ = util_arg.get_argflag('--time-generate') __NUM_PROCS__ = util_arg.get_argval('--num-procs', int, default=None) __FORCE_SERIAL__ = util_arg.get_argflag(('--utool-force-serial', '--force-serial', '--serial')) __SERIAL_FALLBACK__ = not util_arg.get_argflag('--noserial-fallback') MIN_PARALLEL_TASKS = 2
SILENT = util_arg.SILENT VERBOSE_PARALLEL, VERYVERBOSE_PARALLEL = util_arg.get_module_verbosity_flags( 'par', 'parallel') #VERBOSE_PARALLEL = util_arg.VERBOSE or util_arg.get_argflag(('--verbose-par', '--verbpar', '--verbose-parallel', '--verbparallel')) #VERYVERBOSE_PARALLEL = util_arg.VERYVERBOSE or util_arg.get_argflag(('--veryverbose-par', '--veryverbpar', '--veryverbose-parallel', '--veryverbparallel')) STRICT = util_arg.STRICT if SILENT: def print(msg): pass __POOL__ = None #__EAGER_JOIN__ = util_arg.get_argflag('--eager-join') __EAGER_JOIN__ = not util_arg.get_argflag('--noclose-pool') __NUM_PROCS__ = util_arg.get_argval('--num-procs', int, default=None) __FORCE_SERIAL__ = util_arg.get_argflag( ('--utool-force-serial', '--force-serial', '--serial')) #__FORCE_SERIAL__ = True __SERIAL_FALLBACK__ = not util_arg.get_argflag('--noserial-fallback') __TIME_GENERATE__ = VERBOSE_PARALLEL or util_arg.get_argflag('--time-generate') # Maybe global pooling is not correct? USE_GLOBAL_POOL = util_arg.get_argflag('--use_global_pool') # FIXME: running tests in IBEIS has errors when this number is low # Due to the large number of parallel processes running? MIN_PARALLEL_TASKS = 4 #MIN_PARALLEL_TASKS = 16
__CLASSNAME_CLASSKEY_REGISTER__ = defaultdict(util_set.oset) #_rrr = rrr #def rrr(verbose=True): # """ keep registered functions through reloads ? """ # global __CLASSTYPE_ATTRIBUTES__ # global __CLASSTYPE_POSTINJECT_FUNCS__ # cta = __CLASSTYPE_ATTRIBUTES__.copy() # ctpif = __CLASSTYPE_POSTINJECT_FUNCS__.copy() # rrr_(verbose=verbose) # __CLASSTYPE_ATTRIBUTES__ = cta # __CLASSTYPE_POSTINJECT_FUNCS__ = ctpif QUIET_CLASS = util_arg.get_argflag(('--quiet-class', '--quietclass')) VERBOSE_CLASS = ( util_arg.get_argflag(('--verbose-class', '--verbclass')) or (not QUIET_CLASS and util_arg.VERYVERBOSE)) def inject_instance(self, classkey=None, allow_override=False, verbose=VERBOSE_CLASS, strict=True): """ Injects an instance (self) of type (classkey) with all functions registered to (classkey) call this in the __init__ class function Args: self: the class instance
except ImportError as ex: pass from utool import DynamicStruct from utool import util_dbg from utool import util_arg from utool import util_type from utool import util_inject # print, rrr, profile = util_inject.inject(__name__, '[pref]') util_inject.noinject(__name__, '[pref]') # --- # GLOBALS # --- PrefNode = DynamicStruct.DynStruct VERBOSE_PREF = util_arg.get_argflag('--verbpref') # --- # Classes # --- class PrefInternal(DynamicStruct.DynStruct): def __init__(_intern, name, doc, default, hidden, fpath, depeq, choices): super(PrefInternal, _intern).__init__(child_exclude_list=[]) # self._intern describes this node _intern.name = name # A node has a name _intern.doc = doc # A node has a name _intern.value = default # A node has a value _intern.hidden = hidden # A node can be hidden _intern.fpath = fpath # A node is cached to _intern.depeq = depeq # A node depends on
import datetime from utool import util_logging from utool import util_inject from utool import util_arg from utool import util_time from utool import util_iter import six # NOQA print, print_, printDBG, rrr, profile = util_inject.inject(__name__, '[progress]') default_timer = util_time.default_timer QUIET = util_arg.QUIET SILENT = util_arg.SILENT VALID_PROGRESS_TYPES = ['none', 'dots', 'fmtstr', 'simple'] AGGROFLUSH = util_arg.get_argflag('--aggroflush') PROGGRESS_BACKSPACE = not util_arg.get_argflag(('--screen', '--progress-backspace')) NO_PROGRESS = util_arg.get_argflag(('--no-progress', '--noprogress')) #('--screen' not in sys.argv and '--progress-backspace' not in sys.argv) #PROGRESS_WRITE = sys.stdout.write #PROGRESS_FLUSH = sys.stdout.flush # FIXME: if this is loaded before logging beings # progress will not be logged PROGRESS_WRITE = util_logging.__UTOOL_WRITE__ PROGRESS_FLUSH = util_logging.__UTOOL_FLUSH__ def test_progress():
import re import sys import shutil import fnmatch import warnings from utool.util_regex import extend_regex from utool import util_dbg from utool.util_progress import progress_func from utool._internal import meta_util_path from utool import util_inject from utool import util_arg from utool._internal.meta_util_arg import NO_ASSERTS, VERBOSE, VERYVERBOSE, QUIET print, print_, printDBG, rrr, profile = util_inject.inject(__name__, '[util_path]') PRINT_CALLER = util_arg.get_argflag('--print-caller') # FIXME: name __IMG_EXTS = ['.jpg', '.jpeg', '.png', '.tif', '.tiff', '.ppm'] __LOWER_EXTS = [ext.lower() for ext in __IMG_EXTS] __UPPER_EXTS = [ext.upper() for ext in __IMG_EXTS] IMG_EXTENSIONS = set(__LOWER_EXTS + __UPPER_EXTS) def newcd(path): """ DEPRICATE """ cwd = os.getcwd() os.chdir(path) return cwd unixpath = meta_util_path.unixpath
__CLASSTYPE_POSTINJECT_FUNCS__ = defaultdict(oset) #_rrr = rrr #def rrr(verbose=True): # """ keep registered functions through reloads ? """ # global __CLASSTYPE_ATTRIBUTES__ # global __CLASSTYPE_POSTINJECT_FUNCS__ # cta = __CLASSTYPE_ATTRIBUTES__.copy() # ctpif = __CLASSTYPE_POSTINJECT_FUNCS__.copy() # rrr_(verbose=verbose) # __CLASSTYPE_ATTRIBUTES__ = cta # __CLASSTYPE_POSTINJECT_FUNCS__ = ctpif QUIET_CLASS = util_arg.get_argflag(('--quiet-class', '--quietclass')) VERBOSE_CLASS = util_arg.get_argflag(('--verbose-class', '--verbclass')) or (not QUIET_CLASS and util_arg.VERYVERBOSE) def inject_instance(self, classtype=None, allow_override=False, verbose=VERBOSE_CLASS, strict=True): """ Injects an instance (self) of type (classtype) with all functions registered to (classtype) call this in the __init__ class function Args: self: the class instance classtype: key for a class, preferably the class type itself, but it doesnt have to be
import functools try: import numpy as np except ImportError: pass from os.path import splitext, split, basename, dirname from utool import util_inject from utool import util_arg from utool import util_list from utool import util_print from utool import util_str from utool import util_type from utool._internal import meta_util_six print, rrr, profile = util_inject.inject2(__name__, '[dbg]') RAISE_ALL = util_arg.get_argflag('--raise-all', help='Causes ut.printex to always reraise errors') FORCE_TB = util_arg.get_argflag('--force-tb', help='Causes ut.printex to always print traceback') # COLORED_EXCEPTIONS = util_arg.get_argflag(('--colorex', '--cex')) COLORED_EXCEPTIONS = not util_arg.get_argflag(('--no-colorex', '--no-cex')) def print_traceback(with_colors=True): """ prints current stack """ #traceback.print_tb() import traceback stack = traceback.extract_stack() stack_lines = traceback.format_list(stack) tbtext = ''.join(stack_lines) if with_colors:
pass from utool import DynamicStruct from utool import util_dbg from utool import util_arg from utool import util_type from utool import util_inject # print, rrr, profile = util_inject.inject(__name__, '[pref]') util_inject.noinject(__name__, '[pref]') # --- # GLOBALS # --- PrefNode = DynamicStruct.DynStruct VERBOSE_PREF = util_arg.get_argflag('--verbpref') # --- # Classes # --- class PrefInternal(DynamicStruct.DynStruct): def __init__(_intern, name, doc, default, hidden, fpath, depeq, choices): super(PrefInternal, _intern).__init__(child_exclude_list=[]) # self._intern describes this node _intern.name = name # A node has a name _intern.doc = doc # A node has a name _intern.value = default # A node has a value _intern.hidden = hidden # A node can be hidden _intern.fpath = fpath # A node is cached to _intern.depeq = depeq # A node depends on