# -*- coding: utf-8 -*- from __future__ import absolute_import, division, print_function import hashlib import copy import six import uuid import random from utool import util_inject (print, print_, printDBG, rrr, profile) = util_inject.inject(__name__, '[hash]') # default length of hash codes HASH_LEN = 16 # HEX alphabet ALPHABET_16 = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'] # A large base-54 alphabet (all chars are valid for filenames but not # pretty) ALPHABET_54 = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ';', '=', '@', '[', ']', '^', '_', '`', '{', '}', '~', '!', '#', '$', '%', '&', '+', ','] # A large base-41 alphabet (prettier subset of base 54) ALPHABET_41 = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '@', '!', '%', '&',
ReloadingMetaclass KwargsWrapper """ from __future__ import absolute_import, division, print_function import sys import six import types import functools import collections from collections import defaultdict from utool.util_inject import inject from utool.util_set import oset from utool import util_arg from utool._internal.meta_util_six import get_funcname print, print_, printDBG, rrr, profile = inject(__name__, '[class]', DEBUG=False) # Registers which classes have which attributes # FIXME: this might cause memory leaks # FIXME: this does cause weird reimport behavior __CLASSTYPE_ATTRIBUTES__ = defaultdict(oset) __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()
# -*- coding: utf-8 -*- """ util_config TODO: FINISH ME AND ALLOW FOR CUSTOM SETTINGS BASED OFF OF A USER PROFILE """ from __future__ import absolute_import, division, print_function from utool import util_inject (print, print_, printDBG, rrr, profile) = util_inject.inject(__name__, '[config]') def read_repo_config(): pass def get_default_repo_config(): """ import utool """ REPO_CONFIG = { 'project_name': None, 'authors': [], 'licence': None, 'enable_cyth': None, 'docstr_style': None, } return REPO_CONFIG def get_default_global_config():
# -*- coding: utf-8 -*- """ util_config TODO: FINISH ME AND ALLOW FOR CUSTOM SETTINGS BASED OFF OF A USER PROFILE """ from __future__ import absolute_import, division, print_function from utool import util_inject (print, print_, printDBG, rrr, profile) = util_inject.inject(__name__, '[config]') def read_repo_config(): pass def get_default_repo_config(): """ import utool """ REPO_CONFIG = { 'project_name': None, 'authors': [], 'licence': None, 'enable_cyth': None, 'docstr_style': None, } return REPO_CONFIG
# -*- coding: utf-8 -*- from __future__ import absolute_import, division, print_function import functools from utool import util_inject print, print_, printDBG, rrr, profile = util_inject.inject(__name__, '[func]') def compose_functions(*func_list): """ Referenes: https://mathieularose.com/function-composition-in-python/ """ def apply_composition(f, g): def compose(x): return f(g(x)) return compose composed_func = functools.reduce(apply_composition, func_list) return composed_func def identity(input_): """ identity function """ return input_ # DEPRICATE EVERYTHING BELOW HERE #def uinput_1to1(func, input_): # """ universal input (really just accept list or tuple as input to a list
""" SeeAlso: utool._internal.util_importer """ from __future__ import absolute_import, division, print_function from utool import util_inject from utool import util_arg print, print_, printDBG, rrr, profile = util_inject.inject(__name__, '[import]') lazy_module_attrs = ['_modname', '_module', '_load_module'] class LazyModule(object): """ Waits to import the module until it is actually used. Caveat: there is no access to module attributes used in ``lazy_module_attrs`` CommandLine: python -m utool.util_import --test-LazyModule Example: >>> # DISABLE_DOCTEST >>> from utool.util_import import * # NOQA >>> import sys >>> assert 'this' not in sys.modules, 'this was imported before test start' >>> this = LazyModule('this') >>> assert 'this' not in sys.modules, 'this should not have been imported yet' >>> assert this.i == 25 >>> assert 'this' in sys.modules, 'this should now be imported'
from __future__ import absolute_import, division, print_function try: import numpy as np except ImportError as ex: pass from utool import util_inject print, print_, printDBG, rrr, profile = util_inject.inject(__name__, '[util_numpy]') def tiled_range(range_, cols): return np.tile(np.arange(range_), (cols, 1)).T #np.tile(np.arange(num_qf).reshape(num_qf, 1), (1, k_vsmany)) def random_indexes(max_index, subset_size=None, seed=None): """ random unrepeated indicies """ subst_ = np.arange(0, max_index) if seed is None: np.random.shuffle(subst_) else: randstate = np.random.RandomState(seed=seed) randstate.shuffle(subst_) if subset_size is None: subst = subst_ else: subst = subst_[0:min(subset_size, max_index)] return subst #def list_index(search_list, to_find_list): # """ Keep this function
# -*- coding: utf-8 -*- from __future__ import absolute_import, division, print_function import functools from utool import util_inject print, print_, printDBG, rrr, profile = util_inject.inject(__name__, "[func]") def compose_functions(*func_list): """ Referenes: https://mathieularose.com/function-composition-in-python/ """ def apply_composition(f, g): def compose(x): return f(g(x)) return compose composed_func = functools.reduce(apply_composition, func_list) return composed_func def identity(input_): """ identity function """ return input_ # DEPRICATE EVERYTHING BELOW HERE
the __setattr__ __getattr__ stuff needs to be redone, and DynStruct probably needs to go away. """ from __future__ import absolute_import, division, print_function import six from six.moves import cPickle try: import numpy as np 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, print_, printDBG, rrr, profile = util_inject.inject(__name__, '[pref]') # --- # GLOBALS # --- PrefNode = DynamicStruct.DynStruct VERBOSE_PREF = util_arg.get_argflag('--verbpref') def printDBG(msg): #print('[PREFS] '+msg) pass
import re import textwrap try: import numpy as np except ImportError: pass from os.path import join, splitext # Util from utool import util_cplat from utool import util_path from utool import util_num from utool import util_dev from utool import util_io from utool.util_dbg import printex from utool.util_inject import inject print, print_, printDBG, rrr, profile = inject(__name__, '[latex]') #def ensure_latex_environ(): # paths = os.environ['PATH'].split(os.pathsep) # mpl.rc('font',**{'family':'serif'}) # mpl.rc('text', usetex=True) # mpl.rc('text.latex',unicode=True) # mpl.rc('text.latex',preamble='\usepackage[utf8]{inputenc}') def find_ghostscript_exe(): import utool as ut if ut.WIN32: gs_exe = r'C:\Program Files (x86)\gs\gs9.16\bin\gswin32c.exe' else: gs_exe = 'gs'
from __future__ import absolute_import, division, print_function # Python import os from utool.util_inject import inject from utool.util_str import byte_str2 print, print_, printDBG, rrr, profile = inject(__name__, '[print]') try: # Resource does not exist in win32 import resource def time_in_usermode(): stime = resource.getrusage(resource.RUSAGE_SELF).ru_stime return stime def time_in_systemmode(): utime = resource.getrusage(resource.RUSAGE_SELF).ru_utime return utime def peak_memory(): """Returns the resident set size (the portion of a process's memory that is held in RAM.) """ # MAXRSS is expressed in kilobytes. Convert to bytes maxrss = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss * 1024 return maxrss def get_resource_limits(): #rlimit_keys = [key for key in six.iterkeys(resource.__dict__) if key.startswith('RLIMIT_')] #print('\n'.join(['(\'%s\', resource.%s),' % (key.replace('RLIMIT_', ''), key) for key in rlimit_keys])) rlim_keytups = [
from __future__ import absolute_import, division, print_function try: import numpy as np HAVE_NUMPY = True except ImportError: HAVE_NUMPY = False # TODO remove numpy pass from six.moves import zip from utool import util_iter from utool import util_alg from utool import util_inject print, print_, printDBG, rrr, profile = util_inject.inject(__name__, '[util_assert]') from utool import util_arg def get_first_None_position(list_): for index, item in enumerate(list_): if item is None: return index return None def assert_all_not_None(list_, list_name='some_list', key_list=[], verbose=not util_arg.QUIET, veryverbose=False): if util_arg.NO_ASSERTS: return try: index = get_first_None_position(list_) assert index is None, 'index=%r in %s is None' % (index, list_name) if veryverbose:
from utool import util_arg from utool import util_hash from utool import util_inject from utool import util_path from utool import util_io from utool import util_str from utool import util_cplat from utool import util_inspect from utool import util_list from utool._internal import meta_util_constants try: import numpy as np HAVE_NUMPY = True except ImportError: HAVE_NUMPY = False print, print_, printDBG, rrr, profile = util_inject.inject(__name__, '[cache]') # TODO: Remove globalness VERBOSE = util_arg.VERBOSE QUIET = util_arg.QUIET __SHELF__ = None # GLOBAL CACHE __APPNAME__ = meta_util_constants.default_appname # the global application name def get_default_appname(): global __APPNAME__ return __APPNAME__
# -*- coding: utf-8 -*- from __future__ import absolute_import, division, print_function from utool import util_inject import six print, print_, printDBG, rrr, profile = util_inject.inject(__name__, '[sqlite]') def get_tablenames(cur): """ Conveinience: """ cur.execute("SELECT name FROM sqlite_master WHERE type='table'") tablename_list_ = cur.fetchall() tablename_list = [str(tablename[0]) for tablename in tablename_list_ ] return tablename_list import collections SQLColumnRichInfo = collections.namedtuple('SQLColumnRichInfo', ('column_id', 'name', 'type_', 'notnull', 'dflt_value', 'pk')) def get_table_columns(cur, tablename, exclude_columns=[]): import utool as ut colnames_ = ut.get_table_columnname_list(cur, tablename) colnames = tuple([colname for colname in colnames_ if colname not in exclude_columns]) row_list = ut.get_table_rows(cur, tablename, colnames, unpack=False) column_list = zip(*row_list) return column_list def get_table_csv(cur, tablename, exclude_columns=[]): """ Conveinience: Converts a tablename to csv format Args: