# # https://mastodon.social/@GeePawHill/110817412199036363 # # This is baby's first type annotated Python. Expect mistakes, or at # least things that aren't ideal. It does pass mypy! import typing # this is the type annotation type of a test log entry, which is used # for both input to appendTestRun() and the result it returns (a # generator of these tuples). A real version would have a restricted # result type. logEntryType = tuple[str, typing.Any] class TestResults: def __init__(self) -> None: # This stores the list of known test names. New test names are # appended to the end. self.known_tests: list[str] = [] # This may not be in complete adherence to GeePawHill's API, # but this is more Pythonic. Usage is: # for name, result in trs.appendTestRun(log): # .... # # result is None if the test was not run this time, and otherwise # whatever we were passed. def appendTestRun(self, testlist: list[logEntryType]) -> typing.Generator[logEntryType, None, None]: # Generate a dict of the current results, mapping test name to # results. #resdict = {x[0]:x[1] for x in testlist} resdict = dict(testlist) # Determine the new test names by generating a set of the test # names in this run then removing the set of known tests. newtests = {x[0] for x in testlist} - set(self.known_tests) # If there are any new test names, sort them alphabetically and # add them to the end of the list of known test names. if newtests: l = list(newtests) l.sort() self.known_tests.extend(l) # Iterate through the known test names, looking up the current # results; if there are no current results, we yield None for # them. for testname in self.known_tests: yield (testname, resdict.get(testname, None))