speed up ==, hash, <, >, <=, and >= for plain_data
[nmutil.git] / src / nmutil / get_test_path.py
index ea302b45baf816d840b08c1c5c6ac8dbe9e66a93..f58ada8dbc7da1fedb9bd823bdabb89decf7a2c5 100644 (file)
@@ -1,28 +1,31 @@
 # SPDX-License-Identifier: LGPL-3-or-later
-# See Notices.txt for copyright information
+# Copyright 2021 Jacob Lifshay
+
+# Funded by NLnet Assure Programme 2021-02-052, https://nlnet.nl/assure part
+# of Horizon 2020 EU Programme 957073.
 
-from os import PathLike
-from typing import Dict, Tuple, Union
-import unittest
 import weakref
 from pathlib import Path
 
 
 class RunCounter:
-    __run_counts: Dict[str, int]
-
-    def __init__(self) -> None:
+    def __init__(self):
         self.__run_counts = {}
+        """dict mapping self.next() keys to the next int value returned by
+        self.next()"""
 
-    def next(self, k: str) -> int:
+    def next(self, k):
+        """get a incrementing run counter for a `str` key `k`. returns an `int`."""
         retval = self.__run_counts.get(k, 0)
         self.__run_counts[k] = retval + 1
         return retval
 
-    __RUN_COUNTERS: Dict[int, Tuple[weakref.ref, "RunCounter"]] = {}
+    __RUN_COUNTERS = {}
+    """dict mapping object ids (int) to a tuple of a weakref.ref to that
+    object, and the corresponding RunCounter"""
 
     @staticmethod
-    def get(obj: object) -> "RunCounter":
+    def get(obj):
         k = id(obj)
         t = RunCounter.__RUN_COUNTERS
         try:
@@ -36,9 +39,8 @@ class RunCounter:
             return retval
 
 
-_StrPath = Union[str, PathLike]
-
-
-def get_test_path(test_case: unittest.TestCase, base_path: _StrPath) -> Path:
+def get_test_path(test_case, base_path):
+    """get the `Path` for a particular unittest.TestCase instance
+    (`test_case`). base_path is either a str or a path-like."""
     count = RunCounter.get(test_case).next(test_case.id())
     return Path(base_path) / test_case.id() / str(count)