Anna Arhipova | 7cdcc85 | 2023-11-15 18:20:45 +0100 | [diff] [blame] | 1 | from parse import parse |
Anna Arhipova | 32dd8ce | 2024-01-20 15:30:47 +0100 | [diff] [blame^] | 2 | from typing import Dict, List, Callable, Any |
| 3 | from functools import wraps |
| 4 | from django.core.cache import cache |
| 5 | import difflib |
Anna Arhipova | 7cdcc85 | 2023-11-15 18:20:45 +0100 | [diff] [blame] | 6 | |
| 7 | |
| 8 | def parse_title(test_name): |
| 9 | # Sometimes id can be without the closing ] symbol |
| 10 | if "[" in test_name and "]" not in test_name: |
| 11 | test_name += "]" |
| 12 | token_count = test_name.split(".").__len__() |
| 13 | |
| 14 | if test_name.startswith("=="): |
| 15 | return test_name |
| 16 | |
| 17 | if test_name.startswith(".setUp") or test_name.startswith(".tearDown"): |
| 18 | fmt = "{test_title}(" + "{}." * (token_count - 2) + "{class_name})" |
| 19 | r = parse(fmt, test_name) |
| 20 | return f"{r['class_name']}.{r['test_title']}".strip() |
| 21 | try: |
| 22 | fmt = "{}." * (token_count - 2) + "{class_name}.{test_title}[{id}]" |
| 23 | r = parse(fmt, test_name) |
| 24 | return f"{r['test_title']}[{r['id']}]" |
| 25 | except TypeError: |
| 26 | # return file_name.test_class.test_name in other complicated cases |
| 27 | return '.'.join(test_name.split(".")[:3]) |
| 28 | |
| 29 | |
| 30 | def short_names_for_dict(_dict): |
| 31 | __dict = {} |
| 32 | for _k in _dict.keys(): |
| 33 | __k = parse_title(_k) |
| 34 | # Replace only those keys which are absent in the dict or empty |
| 35 | # (defined as "No result found") |
| 36 | if __dict.get(__k) == "No result found" or not __dict.get(__k): |
| 37 | __dict[__k] = _dict[_k] |
| 38 | return __dict |
| 39 | |
| 40 | |
| 41 | def get_dict_diff(dict1: dict, |
| 42 | dict2: dict, |
| 43 | compare_by_key=None) -> Dict[str, List]: |
| 44 | all_keys = sorted(set(list(dict1.keys()) + list(dict2.keys()))) |
| 45 | |
| 46 | result = dict() |
| 47 | for k in all_keys: |
| 48 | if compare_by_key: |
| 49 | if dict1.get(k, {}).get(compare_by_key) == dict2.get(k, {}).get( |
| 50 | compare_by_key): |
| 51 | continue |
| 52 | else: |
| 53 | if dict1.get(k) == dict2.get(k): |
| 54 | continue |
| 55 | result[k] = [dict1.get(k), dict2.get(k)] |
| 56 | return result |
| 57 | |
| 58 | |
Anna Arhipova | 32dd8ce | 2024-01-20 15:30:47 +0100 | [diff] [blame^] | 59 | def replace_all(text: str, olds: str, new: str) -> str: |
| 60 | r = text |
| 61 | for _s in olds: |
| 62 | r = r.replace(_s, new) |
| 63 | return r |
| 64 | |
| 65 | |
| 66 | def cached(timeout: int = None) -> Callable: |
| 67 | def decorator(func: Callable) -> Callable: |
| 68 | def wrapper(*args, **kwargs) -> Any: |
| 69 | cache_key = f'{func.__name__}_{args}_{kwargs}' |
| 70 | cache_key = replace_all(cache_key, "{}()\'\" .,:", "_") |
| 71 | result = cache.get(cache_key) |
| 72 | if result is None: |
| 73 | print(f"{func.__name__} MISS") |
| 74 | result = func(*args, **kwargs) |
| 75 | cache.set(cache_key, result, timeout=timeout) |
| 76 | return result |
| 77 | |
| 78 | print(f"{func.__name__} hit") |
| 79 | |
| 80 | # # FIXME Assert to test the caching mechanism |
| 81 | # _result = func(*args, **kwargs) |
| 82 | # for d in difflib.ndiff(str(result), str(_result)): |
| 83 | # if not d.startswith(" "): |
| 84 | # print(d) |
| 85 | # assert result == _result |
| 86 | # # ENDFIXME |
| 87 | |
| 88 | return result |
| 89 | return wrapper |
| 90 | return decorator |
| 91 | |
| 92 | |
Anna Arhipova | 7cdcc85 | 2023-11-15 18:20:45 +0100 | [diff] [blame] | 93 | if __name__ == "__main__": |
| 94 | pass |