diff --git a/lib/vsc/utils/affinity.py b/lib/vsc/utils/affinity.py index eb1859e2..98d2eb72 100644 --- a/lib/vsc/utils/affinity.py +++ b/lib/vsc/utils/affinity.py @@ -110,7 +110,7 @@ class cpu_set_t(ctypes.Structure): _fields_ = [('__bits', cpu_mask_t * NMASKBITS)] def __init__(self, *args, **kwargs): - super(cpu_set_t, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) self.cpus = None def __str__(self): @@ -126,13 +126,13 @@ def convert_hr_bits(self, txt): # sanity check if indices[1] < indices[0]: logging.error("convert_hr_bits: end is lower then start in '%s'", rng) - raise Exception("convert_hr_bits: end is lower then start") + raise ValueError("convert_hr_bits: end is lower then start") elif indices[0] < 0: logging.error("convert_hr_bits: negative start in '%s'", rng) - raise Exception("convert_hr_bits: negative start") + raise ValueError("convert_hr_bits: negative start") elif indices[1] > CPU_SETSIZE + 1: # also covers start, since end > start logging.error("convert_hr_bits: end larger then max %s in '%s'", CPU_SETSIZE, rng) - raise Exception("convert_hr_bits: end larger then max") + raise ValueError("convert_hr_bits: end larger then max") self.cpus[indices[0]:indices[1] + 1] = [1] * (indices[1] + 1 - indices[0]) logging.debug("convert_hr_bits: converted %s into cpus %s", txt, self.cpus) @@ -146,10 +146,10 @@ def convert_bits_hr(self): parsed_idx = [] for idx in cpus_index: if prev + 1 < idx: - parsed_idx.append("%s" % idx) + parsed_idx.append(f"{idx}") else: first_idx = parsed_idx[-1].split("-")[0] - parsed_idx[-1] = "%s-%s" % (first_idx, idx) + parsed_idx[-1] = f"{first_idx}-{idx}" prev = idx return ",".join(parsed_idx) @@ -193,13 +193,13 @@ def set_bits(self, cpus=None): # get_cpus() rescans logging.error("set_bits: something went wrong: previous cpus %s; current ones %s", prev_cpus[:20], self.cpus[:20]) - raise Exception("set_bits: something went wrong: previous cpus / current ones") + raise ValueError("set_bits: something went wrong: previous cpus / current ones") def str_cpus(self): """Return a string representation of the cpus""" if self.cpus is None: self.get_cpus() - return "".join(["%d" % x for x in self.cpus]) + return "".join([f"{int(x)}" for x in self.cpus]) # /* Get the CPU affinity for a task */ @@ -292,7 +292,7 @@ def getpriority(which=None, who=None): which = PRIO_PROCESS elif which not in (PRIO_PROCESS, PRIO_PGRP, PRIO_USER,): logging.error("getpriority: which %s not in correct range", which) - raise Exception("getpriority: which not in correct range") + raise ValueError("getpriority: which not in correct range") if who is None: who = 0 # current which-ever prio = _libc.getpriority(priority_which_t(which), @@ -318,7 +318,7 @@ def setpriority(prio, which=None, who=None): which = PRIO_PROCESS elif which not in (PRIO_PROCESS, PRIO_PGRP, PRIO_USER,): logging.error("setpriority: which %s not in correct range", which) - raise Exception("setpriority: which not in correct range") + raise ValueError("setpriority: which not in correct range") if who is None: who = 0 # current which-ever @@ -326,11 +326,11 @@ def setpriority(prio, which=None, who=None): prio = int(prio) except ValueError: logging.error("setpriority: failed to convert priority %s into int", prio) - raise Exception("setpriority: failed to convert priority into int") + raise ValueError("setpriority: failed to convert priority into int") if prio < PRIO_MIN or prio > PRIO_MAX: logging.error("setpriority: prio not in allowed range MIN %s MAX %s", PRIO_MIN, PRIO_MAX) - raise Exception("setpriority: prio not in allowed range MIN MAX") + raise ValueError("setpriority: prio not in allowed range MIN MAX") ec = _libc.setpriority(priority_which_t(which), id_t(who), diff --git a/lib/vsc/utils/fancylogger.py b/lib/vsc/utils/fancylogger.py index 89deb7d3..0d373756 100644 --- a/lib/vsc/utils/fancylogger.py +++ b/lib/vsc/utils/fancylogger.py @@ -186,8 +186,8 @@ def _env_to_boolean(varname, default=False): # logging.addLevelName is not a real replacement (it overwrites existing level names) levelnames = logging._nameToLevel -for key in LOG_LEVEL_ALIASES: - levelnames[key] = LOG_LEVEL_ALIASES[key] +for key, name in LOG_LEVEL_ALIASES.items(): + levelnames[key] = name # mpi rank support @@ -212,11 +212,11 @@ class MissingLevelName(KeyError): def getLevelInt(level_name): """Given a level name, return the int value""" if not isinstance(level_name, str): - raise TypeError('Provided name %s is not a string (type %s)' % (level_name, type(level_name))) + raise TypeError(f'Provided name {level_name} is not a string (type {type(level_name)})') level = logging.getLevelName(level_name) if not isinstance(level, int): - raise MissingLevelName('Unknown loglevel name %s' % level_name) + raise MissingLevelName(f'Unknown loglevel name {level_name}') return level @@ -316,8 +316,8 @@ def raiseException(self, message, exception=None, catch=False): # extend the message with the traceback and some more details # or use self.exception() instead of self.warning()? tb_text = "\n".join(traceback.format_tb(tb)) - message += " (%s)" % detail - fullmessage += " (%s\n%s)" % (detail, tb_text) + message += f" ({detail})" + fullmessage += f" ({detail}\n{tb_text})" if exception is None: exception = self.RAISE_EXCEPTION_CLASS @@ -346,9 +346,9 @@ def deprecated(self, msg, cur_ver, max_ver, depth=2, exception=None, log_callbac loose_mv.version = loose_mv.version[:depth] if loose_cv >= loose_mv: - self.raiseException("DEPRECATED (since v%s) functionality used: %s" % (max_ver, msg), exception=exception) + self.raiseException(f"DEPRECATED (since v{max_ver}) functionality used: {msg}", exception=exception) else: - deprecation_msg = "Deprecated functionality, will no longer work in v%s: %s" % (max_ver, msg) + deprecation_msg = f"Deprecated functionality, will no longer work in v{max_ver}: {msg}" log_callback(deprecation_msg) def _handleFunction(self, function, levelno, **kwargs): @@ -423,7 +423,7 @@ def info(x): def get_parent_info(self, prefix, verbose=True): """Return pretty text version""" rev_parent_info = self._get_parent_info(verbose=verbose) - return ["%s %s%s" % (prefix, " " * 4 * idx, info) for idx, info in enumerate(rev_parent_info)] + return [f"{prefix} {' ' * 4 * idx}{info}" for idx, info in enumerate(rev_parent_info)] def __copy__(self): """Return shallow copy, in this case reference to current logger""" @@ -438,7 +438,7 @@ def thread_name(): """ returns the current threads name """ - return threading.currentThread().getName() + return threading.current_thread().name def getLogger(name=None, fname=False, clsname=False, fancyrecord=None): @@ -479,8 +479,8 @@ def getLogger(name=None, fname=False, clsname=False, fancyrecord=None): l.fancyrecord = fancyrecord if _env_to_boolean('FANCYLOGGER_GETLOGGER_DEBUG'): print('FANCYLOGGER_GETLOGGER_DEBUG') - print('name %s fname %s fullname %s' % (name, fname, fullname)) - print("getRootLoggerName: %s" % getRootLoggerName()) + print(f'name {name} fname {fname} fullname {fullname}') + print(f"getRootLoggerName: {getRootLoggerName()}") if hasattr(l, 'get_parent_info'): print('parent_info verbose') print("\n".join(l.get_parent_info("FANCYLOGGER_GETLOGGER_DEBUG"))) @@ -553,7 +553,7 @@ def logToScreen(enable=True, handler=None, name=None, stdout=False, colorize=Non return _logToSomething(FancyStreamHandler, handleropts, - loggeroption='logtoscreen_stdout_%s' % str(stdout), + loggeroption=f'logtoscreen_stdout_{str(stdout)}', name=name, enable=enable, handler=handler, @@ -586,12 +586,12 @@ def logToFile(filename, enable=True, filehandler=None, name=None, max_bytes=MAX_ os.makedirs(directory) except Exception as ex: exc, detail, tb = sys.exc_info() - raise(exc("Cannot create logdirectory %s: %s \n detail: %s" % (directory, ex, detail))).with_traceback(tb) + raise(exc(f"Cannot create logdirectory {directory}: {ex} \n detail: {detail}")).with_traceback(tb) return _logToSomething( logging.handlers.RotatingFileHandler, handleropts, - loggeroption='logtofile_%s' % filename, + loggeroption=f'logtofile_{filename}', name=name, enable=enable, handler=filehandler, @@ -611,7 +611,7 @@ def logToUDP(hostname, port=5005, enable=True, datagramhandler=None, name=None): handleropts = {'hostname': hostname, 'port': port} return _logToSomething(logging.handlers.DatagramHandler, handleropts, - loggeroption='logtoudp_%s:%s' % (hostname, str(port)), + loggeroption=f'logtoudp_{hostname}:{str(port)}', name=name, enable=enable, handler=datagramhandler, @@ -702,7 +702,7 @@ def _getSysLogFacility(name=None): name = 'user' facility = getattr(logging.handlers.SysLogHandler, - "LOG_%s" % name.upper(), logging.handlers.SysLogHandler.LOG_USER) + f"LOG_{name.upper()}", logging.handlers.SysLogHandler.LOG_USER) return facility diff --git a/setup.py b/setup.py index c404461d..236dd37c 100755 --- a/setup.py +++ b/setup.py @@ -37,7 +37,7 @@ from vsc.install.shared_setup import ag, kh, jt, sdw PACKAGE = { - 'version': '3.5.6', + 'version': '3.5.7', 'author': [sdw, jt, ag, kh], 'maintainer': [sdw, jt, ag, kh], 'install_requires': [