master > master: code - core methoden hinzugefügt
This commit is contained in:
parent
5875fb4a90
commit
14fad54a57
0
code/core/__init__.py
Normal file
0
code/core/__init__.py
Normal file
127
code/core/log.py
Normal file
127
code/core/log.py
Normal file
@ -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;
|
53
code/core/metrics.py
Normal file
53
code/core/metrics.py
Normal file
@ -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;
|
Loading…
x
Reference in New Issue
Block a user