ads1_2021/code/main.py

118 lines
4.9 KiB
Python
Raw Normal View History

2021-10-22 15:29:01 +02:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# IMPORTS
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import os;
import sys
2021-10-22 15:29:01 +02:00
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))));
from code.core.log import *;
from code.core.config import *;
from code.display.display import *;
from code.algorithms.exports import *;
2021-10-22 15:29:01 +02:00
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# GLOBAL VARIABLES/CONSTANTS
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
parser: argparse.ArgumentParser;
PATH_TO_CONFIG: str = 'code/config.yml';
2021-10-22 15:29:01 +02:00
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# MAIN PROCESS
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def enter(args: argparse.Namespace):
SetQuietMode(args.quiet);
SetDebugMode(args.debug);
configpath = PATH_TO_CONFIG if args.path is None else args.path;
if args.all is not None:
LoopThroughCases(path=configpath);
else:
parser.print_help();
2021-10-22 15:29:01 +02:00
return;
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# SECONDARY PROCESSES
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def LoopThroughCases(path: str):
'''
Durchlauf aller Testfälle.
'''
config = ReadConfigFile(path);
cases = GetAttribute(config, 'parts', 'cases', expectedtype=list, default=[]);
for caseindex, case in enumerate(cases):
DisplayCase(caseindex);
command = GetAttribute(case, 'command', expectedtype=str, default='');
inputs = GetAttribute(case, 'inputs', expectedtype=dict, default={});
checks = GetAttribute(case, 'check', expectedtype=bool, default=True);
RestartCounter();
if command == 'algorithm-search-sequential':
L, x = inputs['L'], inputs['x'];
DisplayStartOfAlgorithm('Sequenziellsuche', L=L, x=x);
p = SequentialSearch(L=L, x=x);
value = L[p] if p >= 0 else None;
if checks:
assert value == x, 'Der Algorithmus hat versagt.';
DisplayEndOfAlgorithm(p = p);
elif command == 'algorithm-search-binary':
L, x = inputs['L'], inputs['x'];
DisplayStartOfAlgorithm('Binärsuche', L=L, x=x);
if checks:
assert L == sorted(L), 'Ungültiger Input: L muss aufsteigend sortiert sein!';
p = BinarySearch(L=L, x=x);
value = L[p] if p >= 0 else None;
if checks:
assert value == x, 'Der Algorithmus hat versagt.';
DisplayEndOfAlgorithm(p = p);
elif command == 'algorithm-search-interpolation':
L, x = inputs['L'], inputs['x'];
u, v = 0, len(L)-1;
DisplayStartOfAlgorithm('Interpolationssuche', L=L, x=x, u=u, v=v);
if checks:
assert L == sorted(L), 'Ungültiger Input: L muss aufsteigend sortiert sein!';
p = InterpolationSearch(L=L, x=x, u=u, v=v);
value = L[p] if p >= 0 else None;
if checks:
assert value == x, 'Der Algorithmus hat versagt.';
DisplayEndOfAlgorithm(p = p);
elif command == 'algorithm-search-jump':
L, x, m = inputs['L'], inputs['x'], inputs['m'];
DisplayStartOfAlgorithm('SprungSuche', L=L, x=x, m=m);
if checks:
assert L == sorted(L), 'Ungültiger Input: L muss aufsteigend sortiert sein!';
assert L == list(sorted(set(L))), 'Ungültiger Input: L darf keine Duplikate enthalten!';
p = JumpSearchLinear(L=L, x=x, m=m);
value = L[p] if p >= 0 else None;
if checks:
assert value == x, 'Der Algorithmus hat versagt.';
DisplayEndOfAlgorithm(p = p);
else:
raise ValueError('Command \033[1m{}\033[0m nicht erkannt'.format(command));
2021-10-22 15:29:01 +02:00
return;
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# EXECUTE CODE
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if __name__ == '__main__':
sys.tracebacklimit = 0;
cli_args = sys.argv[1:];
parser = argparse.ArgumentParser(
prog='code/main.py',
description=r'Code-Projekt, um verschiedene Algorithmen aus dem Kurs auszutesten.'
);
parser.add_argument('all', nargs='?', help='Führt alle Testfälle in der config.yml Datei durch.');
parser.add_argument('--path', nargs=1, type=str, help='Pfad zur alternativen Configdatei.');
parser.add_argument('config', nargs='?', help='Führt alle Testfälle in der config.yml Datei durch.');
parser.add_argument('--debug', action='store_true', help='Debugging Messages stummschalten.')
parser.add_argument('-q', '--quiet', action='store_true', help='Alle console-messages bis auf Errors stummschalten.')
enter(parser.parse_args(cli_args));