From 14fad54a57ad4775a81e28e62eae9c186d8fb209 Mon Sep 17 00:00:00 2001 From: raj_mathe Date: Sat, 23 Oct 2021 11:29:11 +0200 Subject: [PATCH] =?UTF-8?q?master=20>=20master:=20code=20-=20core=20method?= =?UTF-8?q?en=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/core/__init__.py | 0 code/core/log.py | 127 ++++++++++++++++++++++++++++++++++++++++++ code/core/metrics.py | 53 ++++++++++++++++++ 3 files changed, 180 insertions(+) create mode 100644 code/core/__init__.py create mode 100644 code/core/log.py create mode 100644 code/core/metrics.py diff --git a/code/core/__init__.py b/code/core/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/code/core/log.py b/code/core/log.py new file mode 100644 index 0000000..3b3ec83 --- /dev/null +++ b/code/core/log.py @@ -0,0 +1,127 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# IMPORTS +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +from code.local.io import *; +from code.local.misc import *; +from code.local.system import *; +from code.local.typing import *; + +from datetime import timedelta; + +from code.core.metrics import *; + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# GLOBAL VARIABLES +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +_logging_prefix: str = ''; +_quietmode: bool = False; +_debugmode: bool = False; +_ctr: Counter = Counter(); + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# METHOD get/set quiet mode, logging depth, timer +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +def getQuietMode() -> bool: + return _quietmode; + +def setQuietMode(mode: bool): + global _quietmode; + _quietmode = mode; + return; + +def getDebugMode() -> bool: + return _debugmode; + +def setDebugMode(mode: bool): + global _debugmode; + _debugmode = mode; + return; + +def restartCounter(): + global _ctr; + _ctr.reset(); + return; + +def addToCounter(n: int): + global _ctr; + _ctr.add(n); + return; + +def numberOfSteps() -> int: + return _ctr.numberOfStep; + +def timeElapsed() -> timedelta: + global _ctr; + _ctr.stop(); + return _ctr.elapsed; + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# Logging +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +def logGeneric(tag: str, *lines: Any, file: io.TextIOWrapper, force: bool = False, tag_all: bool = True): + if not force and _quietmode: + return; + tag = '' if tag == '' else tag + ' '; + file = file or sys.stdout; + for line in lines: + print('{}{}{}'.format('', tag, line), file=file); + if not tag_all: + tag = ''; + return; + +def logPlain(*lines: Any, force: bool = False, file: Any = None): + logGeneric('', *lines, force=force, file=file or sys.stdout); + +def logInfo(*lines: Any, force: bool = False, tag_all: bool = True, file: Any = None): + logGeneric('[\033[94;1mINFO\033[0m]', *lines, force=force, tag_all=tag_all, file=file or sys.stdout); + +def logDebug(*lines: Any, force: bool = False, tag_all: bool = True, file: Any = None): + if not _debugmode: + return; + logGeneric('[\033[96;1mDEBUG\033[0m]', *lines, force=force, tag_all=tag_all, file=file or sys.stdout); + +def logWarn(*lines: Any, force: bool = False, tag_all: bool = False, file: Any = None): + logGeneric('[\033[93;1mWARNING\033[0m]', *lines, force=force, tag_all=tag_all, file=file or sys.stdout); + +def logError(*lines: Any, force: bool = False, tag_all: bool = False, file: Any = None): + logGeneric('[\033[91;1mERROR\033[0m]', *lines, force=force, tag_all=tag_all, file=file or sys.stderr); + +def logFatal(*lines: Any, force: bool = False, tag_all: bool = False, file: Any = None): + logGeneric('[\033[91;1mFATAL\033[0m]', *lines, force=force, tag_all=tag_all, file=file or sys.stderr); + exit(1); + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# User Input +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +def askUserInput(message: str, expectedformat: Callable) -> Union[str, None]: + answer = None; + while True: + try: + answer = input('{}{}'.format(_logging_prefix, message)); + ## Meta+C erkennen: + except KeyboardInterrupt: + logPlain(''); + return None; + ## Meta+D erkennen: + except EOFError: + logPlain(''); + return None; + except: + continue; + if expectedformat(answer): + break; + return answer; + +def askConfirmation(message: str, default: bool = False) -> bool: + answer = askUserInput(message, lambda x: not not re.match(r'^(y|yes|j|ja|n|no|nein)$', x)); + if isinstance(answer, str): + return True if re.match(r'^(y|yes|j|ja)$', answer) else False; + return default; diff --git a/code/core/metrics.py b/code/core/metrics.py new file mode 100644 index 0000000..d40b099 --- /dev/null +++ b/code/core/metrics.py @@ -0,0 +1,53 @@ +# !/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# IMPORTS +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +from datetime import datetime; +from datetime import timedelta; + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# CLASS counter +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +class Counter(object): + _nr_steps: int; + _timeelapsed: timedelta; + _timecurrent: datetime; + + def __init__(self): + self.reset(); + + def __str__(self) -> str: + return str(self._nr_steps); + + @property + def numberOfStep(self) -> int: + return self._nr_steps; + + @property + def elapsedTime(self) -> timedelta: + return self._timeelapsed; + def start(self): + self._timecurrent = datetime.now(); + return self; + + def stop(self): + t0 = self._timecurrent; + t1 = datetime.now(); + self._timecurrent = t1; + self._timeelapsed += (t1 - t0); + return self; + + def add(self, n: int = 1): + self._nr_steps += n; + return self; + + def reset(self): + t = datetime.now(); + self._timeelapsed = t - t; + self._nr_steps = 0; + self._timecurrent = t; + return self;