Move debug code to debug_lib

This commit is contained in:
Cal Wing 2025-02-20 18:45:22 +01:00
parent 9124ead993
commit f64025eb35
2 changed files with 85 additions and 47 deletions

52
a1.py
View File

@ -148,12 +148,13 @@ def calc_heights_water_out(initial_height: float, final_height: float,
return water_height, water_mass
# See if I get what the task sheet wants
# [NOTE] It seems I am witing my own test suite :/ that wasn't the initention lol
def sheet_tasks():
import numpy as np
class NO_EVAL(type): pass # Whats an enum?
class TUPLE_EVAL(type): pass
from debug_lib import NO_EVAL, TUPLE_EVAL
from debug_lib import compair_outputs
TASKS = (
(determine_power_used, (5e6, 250, 8, 85), 500.9191176470588),
@ -164,50 +165,9 @@ def sheet_tasks():
(calc_heights_water_out, (40, 5, 40000, 1, 30), ((39.97898928844613, 39.95798409574207, 39.93698442188801), (838016.4324684857, 837796.3120398963, 837576.1916037091), TUPLE_EVAL)),
)
for i, (task, args, expected) in enumerate(TASKS):
print(f'Task {i+1}')
result = task(*args)
compair_outputs(TASKS)
# Number comparison
if isinstance(expected, (float, int)):
foo = np.isclose(result, expected, 0.001)
print(f'{task.__qualname__}{args} -> {result} {"~=" if foo else "!="} {expected}')
print(f'Task is{"" if foo else " NOT"} close enough to expected result.')
# Run the task and print out what is is expected on a new line
elif isinstance(expected, (list, tuple)) and expected[-1] is NO_EVAL:
print(f'{task.__qualname__}{args} -> {result}')
print(f'Expected: {expected[:-1]}')
# Do a tuple eval
# [TODO] Do this, not complete
elif isinstance(expected, (list, tuple)) and expected[-1] is TUPLE_EVAL:
expected_result = expected[:-1]
comp_lenths = tuple(len(z) for z in expected_result)
#flag = True
#for k, j in enumerate(comp_lenths):
# for jj in range(j):
# if not np.isclose(result[k][jj], expected_result[k][jj], 0.001): break
# else:
# continue
# flag = False
# break
print(f'{task.__qualname__}{args} -> {tuple(result[z][:comp_lenths[z]] for z in range(len(result)))}')
print(f'Expected: {expected[:-1]}')
# Literal Comparison
elif expected is not None:
print(f'{task.__qualname__}{args} -> {result} {"==" if result == expected else "!="} {expected}')
print(f'Task is{"" if result == expected else " NOT"} equal to expected result.')
# Just run the task
else:
print(f'{task.__qualname__}{args} -> {result}')
# Newline
print()
if __name__ == '__main__':
sheet_tasks()

View File

@ -1,7 +1,85 @@
""" debug_lib
A bunch of helpful debuging functions
Cal.W 2019
Cal.W 2025
"""
import numpy as np
# Consts
IS_CLOSE_VAR = 0.001
# Horriable ENUM types
class NO_EVAL(type): pass # Whats an enum?
class TUPLE_EVAL(type): pass
def compair_outputs(tasks):
"""
callable, args, expected, force comp type
tasks: Tuple[Tuple[callable, Tuple[...], Expacted, [TYPE]], ...]
tasks = (
(determine_power_used, (5e6, 250, 8, 85), 500.9191176470588),
(determine_water_released, (300, 250, 8, 85), 2994495.4128440367),
(determine_cost_to_pump, (300, 8, 0.02), 48),
(calc_speed_at_outlet, (30,), 24.261079942986875),
(calc_new_water_height, (20, 40000, 1, 30), (19.985143183382704, 592567.1021442247)),
(calc_heights_water_out, (40, 5, 40000, 1, 30), ((39.97898928844613, 39.95798409574207, 39.93698442188801), (838016.4324684857, 837796.3120398963, 837576.1916037091), TUPLE_EVAL)),
)
"""
for i, (task, args, expected) in enumerate(tasks):
print(f'Task {i+1}')
result = task(*args)
# Number comparison
if isinstance(expected, (float, int)):
foo = np.isclose(result, expected, IS_CLOSE_VAR)
print(f'{task.__qualname__}{args} -> {result} {"~=" if foo else "!="} {expected}')
print(f'Task is{"" if foo else " NOT"} close enough to expected result.')
# Run the task and print out what is is expected on a new line
elif isinstance(expected, (list, tuple)) and expected[-1] is NO_EVAL:
print(f'{task.__qualname__}{args} -> {result}')
print(f'Expected: {expected[:-1]}')
# Do a tuple eval
# [TODO] Do this, not complete
elif isinstance(expected, (list, tuple)) and expected[-1] is TUPLE_EVAL:
expected_result = expected[:-1]
comp_lenths = tuple(len(z) for z in expected_result)
flag = False
for ii, comp_res in enumerate(comp_lenths):
for iii in range(comp_res):
bar = isinstance(expected[ii][iii], (float, int))
baz = np.isclose(result[ii][iii], expected[ii][iii], IS_CLOSE_VAR) if bar else (result[ii][iii] == expected[ii][iii])
if not baz:
flag = True
break
if flag: break
print(f'{task.__qualname__}{args} -> {tuple(result[z][:comp_lenths[z]] for z in range(len(result)))}')
print(f'Expected: {expected[:-1]}')
if flag:
print(f"First Mismatch; result[{ii}][{iii}] -> {result[ii][iii]} != {expected[ii][iii]}")
print(f'Task is{" NOT" if flag else ""} close enough to expected result.')
# Literal Comparison
elif expected is not None:
print(f'{task.__qualname__}{args} -> {result} {"==" if result == expected else "!="} {expected}')
print(f'Task is{"" if result == expected else " NOT"} equal to expected result.')
# Just run the task
else:
print(f'{task.__qualname__}{args} -> {result}')
# Newline
print()
class auto_input:
"""Given a list automagicly enter a value."""
def __init__(self, input_list):