Refinement in tests Output

I use logging.debug, now
This commit is contained in:
Fedaya 2023-12-09 10:21:55 +01:00
parent f641fbac07
commit cdaa539b79
2 changed files with 22 additions and 18 deletions

View File

@ -1,6 +1,6 @@
from __future__ import annotations
from dataclasses import dataclass
import sys
import logging
from typing import List, Self
@ -29,9 +29,10 @@ class OASISLine:
]
def reduce(self) -> Self:
# sys.stderr.write("=".join(["" for i in range(40)])+"\n")
logging.debug(color.RED + f"reducing {self.sequence}" + color.END)
logging.debug(color.BLUE + "=".join(["" for i in range(40)]) + color.END)
line = self.sequence
# sys.stderr.write(str(line)+"\n")
logging.debug(str(line))
while not all(map(lambda i: i == 0, line)):
new_line = []
for idx, number in enumerate(line):
@ -39,44 +40,43 @@ class OASISLine:
new_line.append(number - line[idx - 1])
line = new_line
self.reduce_map.append(line)
# sys.stderr.write(str(line)+"\n")
# sys.stderr.write("=".join(["" for i in range(40)])+"\n")
logging.debug(str(line))
logging.debug(color.BLUE + "=".join(["" for i in range(40)]) + color.END)
return self
def extrapolate(self):
# sys.stderr.write("\n" + color.BLUE + "=".join(["" for i in range(40)]) + color.END + "\n")
logging.debug(color.RED + f"extrapolating {self.sequence}" + color.END)
logging.debug(color.BLUE + "=".join(["" for i in range(40)]) + color.END)
next_increment = 0
for line in reversed(self.reduce_map):
# sys.stderr.write(str(line))
if all(map(lambda i: i == line[0], line)):
next_increment = line[0]
else:
next_increment = line[len(line) - 1] + next_increment
# sys.stderr.write(color.BOLD + f" {next_increment}" + color.END + "\n")
# sys.stderr.write(color.BLUE + "=".join(["" for i in range(40)]) + color.END + "\n")
logging.debug(str(line) + " " + color.BOLD + color.GREEN + f" {next_increment}" + color.END + "\n")
logging.debug(color.BLUE + "=".join(["" for i in range(40)]) + color.END)
return next_increment
def extrapolate_back(self):
sys.stderr.write("\n" + color.BLUE + "=".join(["" for i in range(40)]) + color.END + "\n")
logging.debug(color.RED + f"extrapolating back {self.sequence}" + color.END)
logging.debug(color.BLUE + "=".join(["" for i in range(40)]) + color.END)
next_increment = 0
for line in reversed(self.reduce_map):
if all(map(lambda i: i == line[0], line)):
next_increment = line[0]
else:
next_increment = line[0] - next_increment
sys.stderr.write(color.BOLD + f"{next_increment} " + color.END + str(line) + "\n")
sys.stderr.write(color.BLUE + "=".join(["" for i in range(40)]) + color.END + "\n")
logging.debug(color.BOLD + color.GREEN + f"{next_increment} " + color.END + str(line))
logging.debug(color.BLUE + "=".join(["" for i in range(40)]) + color.END)
return next_increment
@classmethod
def parse(cls, input_file: str) -> List[OASISLine]:
# sys.stderr.write("\n")
the_list = []
with open(input_file) as input:
while line := input.readline():
line = line.rstrip("\n").rstrip()
# sys.stderr.write(color.RED + f'parsing "{line}"' + color.END + "\n")
logging.debug(color.RED + f'parsing "{line}"' + color.END)
the_list.append(OASISLine(line))
# sys.stderr.write(color.GREEN + f"parsed {the_list[len(the_list) - 1]}" + color.END + "\n")
logging.debug(color.GREEN + f"parsed {the_list[len(the_list) - 1].sequence}" + color.END)
return the_list

6
day9/test.py Normal file → Executable file
View File

@ -1,5 +1,7 @@
#!/usr/bin/env python3.11
import unittest
from common import OASISLine
import logging
class Day9Tests(unittest.TestCase):
@ -13,5 +15,7 @@ class Day9Tests(unittest.TestCase):
extrapolation = map(lambda o: o.reduce().extrapolate_back(), analysis)
self.assertEqual(sum(extrapolation), 2)
if __name__ == "__main__":
unittest.main()
logging.basicConfig(level=logging.DEBUG)
unittest.main(verbosity=2)