master > master: code - fügte algorithmen und config datei hinzu
This commit is contained in:
60
code/core/config.py
Normal file
60
code/core/config.py
Normal file
@@ -0,0 +1,60 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# IMPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
from code.local.typing import *;
|
||||
from code.local.config import *;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# GLOBAL VARIABLES
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
#
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# METHOD read config file
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
def ReadConfigFile(path: str) -> dict:
|
||||
with open(path, 'r') as fp:
|
||||
spec = load(fp, Loader=FullLoader);
|
||||
assert isinstance(spec, dict), 'Die Configdatei muss eines Dictionary-Typs sein';
|
||||
return spec;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# METHOD extract attribut from dictionary/list
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
def GetAttribute(
|
||||
obj: Any,
|
||||
*keys: Union[str, int, List[Union[str, int]]],
|
||||
expectedtype: Union[Type, Tuple[Type]] = Any,
|
||||
default: Any = None
|
||||
) -> Any:
|
||||
if len(keys) == 0:
|
||||
return obj;
|
||||
nextkey = keys[0];
|
||||
nextkey = nextkey if isinstance(nextkey, list) else [ nextkey ];
|
||||
try:
|
||||
for key in nextkey:
|
||||
if isinstance(key, str) and isinstance(obj, dict) and key in obj:
|
||||
value = obj[key];
|
||||
if len(keys) <= 1:
|
||||
return value if isinstance(value, expectedtype) else default;
|
||||
else:
|
||||
return GetAttribute(obj[key], *keys[1:], expectedtype=expectedtype, default=default);
|
||||
elif isinstance(key, int) and isinstance(obj, (list,tuple)) and key < len(obj):
|
||||
value = obj[key];
|
||||
if len(keys) <= 1:
|
||||
return value if isinstance(value, expectedtype) else default;
|
||||
else:
|
||||
return GetAttribute(obj[key], *keys[1:], expectedtype=expectedtype, default=default);
|
||||
except:
|
||||
pass;
|
||||
if len(keys) <= 1:
|
||||
return default;
|
||||
path = ' -> '.join([ str(key) for key in keys ]);
|
||||
raise Exception('Konnte \033[1m{}\033[0m im Objekt nicht finden!'.format(path));
|
||||
@@ -27,39 +27,39 @@ _ctr: Counter = Counter();
|
||||
# METHOD get/set quiet mode, logging depth, timer
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
def getQuietMode() -> bool:
|
||||
def GetQuietMode() -> bool:
|
||||
return _quietmode;
|
||||
|
||||
def setQuietMode(mode: bool):
|
||||
def SetQuietMode(mode: bool):
|
||||
global _quietmode;
|
||||
_quietmode = mode;
|
||||
return;
|
||||
|
||||
def getDebugMode() -> bool:
|
||||
def GetDebugMode() -> bool:
|
||||
return _debugmode;
|
||||
|
||||
def setDebugMode(mode: bool):
|
||||
def SetDebugMode(mode: bool):
|
||||
global _debugmode;
|
||||
_debugmode = mode;
|
||||
return;
|
||||
|
||||
def restartCounter():
|
||||
def RestartCounter():
|
||||
global _ctr;
|
||||
_ctr.reset();
|
||||
return;
|
||||
|
||||
def addToCounter(n: int):
|
||||
def AddToCounter(n: int = 1):
|
||||
global _ctr;
|
||||
_ctr.add(n);
|
||||
return;
|
||||
|
||||
def numberOfSteps() -> int:
|
||||
def NumberOfSteps() -> int:
|
||||
return _ctr.numberOfStep;
|
||||
|
||||
def timeElapsed() -> timedelta:
|
||||
def TimeElapsed() -> timedelta:
|
||||
global _ctr;
|
||||
_ctr.stop();
|
||||
return _ctr.elapsed;
|
||||
return _ctr.elapsedTime;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# Logging
|
||||
|
||||
Reference in New Issue
Block a user