A small kernel of code for playing with Galois fields of arbitrary characteristic
Revision | 77c204d9db398a5efe788d1372de67c20d6db6bf (tree) |
---|---|
Time | 2021-08-24 11:09:29 |
Author | Eric Hopper <hopper@omni...> |
Commiter | Eric Hopper |
Color output support now more flexible via dependency injection.
@@ -1,10 +1,12 @@ | ||
1 | -from typing import Any, Callable, TypeVar, Dict, Collection | |
1 | +from typing import Any, Callable, TypeVar, Dict, Collection, Optional | |
2 | 2 | try: |
3 | 3 | import colorama |
4 | 4 | import blessings |
5 | 5 | nocolors = False |
6 | + TermType = blessings.Terminal | |
6 | 7 | except ModuleNotFoundError: |
7 | 8 | nocolors = True |
9 | + TermType = None | |
8 | 10 | |
9 | 11 | __all__ = [ |
10 | 12 | 'extended_gcd', 'print_group_table', 'mult_inverse', 'print_mult_inverses' |
@@ -68,9 +70,10 @@ | ||
68 | 70 | def print_group_table( |
69 | 71 | elements: Collection[T], |
70 | 72 | op: Callable[[T, T], T], |
71 | - highlight_map: Dict[T, int] = {} | |
73 | + highlight_map: Dict[T, int] = {}, | |
74 | + term: Optional[TermType] = None | |
72 | 75 | ): |
73 | - if not nocolors: | |
76 | + if (not nocolors) and (term is None): | |
74 | 77 | colorama.init() |
75 | 78 | term = blessings.Terminal() |
76 | 79 | width = max(len(str(x)) for x in elements) |
@@ -78,7 +81,7 @@ | ||
78 | 81 | def element_str(a, width): |
79 | 82 | s = f'{a:{width}}' |
80 | 83 | spaces = ' ' * (len(s) - len(str(a))) |
81 | - if (not nocolors) and (a in highlight_map): | |
84 | + if (term is not None) and (a in highlight_map): | |
82 | 85 | return spaces + term.color(highlight_map[a])(str(a)) |
83 | 86 | else: |
84 | 87 | return s |