• R/O
  • SSH

Commit

Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#objective-cqt誰得cocoawindowspythonrubyphpgameguibathyscaphec翻訳omegat計画中(planning stage)frameworktwittertestdombtronvb.netdirectxarduinopreviewerゲームエンジン

A small kernel of code for playing with Galois fields of arbitrary characteristic


Commit MetaInfo

Révision0e11b6b32650f9120f975c9fa8e3ca402d4dfee1 (tree)
l'heure2021-07-19 07:56:59
AuteurEric Hopper <hopper@omni...>
CommiterEric Hopper

Message de Log

Make table printout use colors if colorama and blessings is available.

Change Summary

Modification

diff -r f08e01af6b80 -r 0e11b6b32650 numtheory_utils.py
--- a/numtheory_utils.py Sun Jul 18 13:32:33 2021 -0700
+++ b/numtheory_utils.py Sun Jul 18 15:56:59 2021 -0700
@@ -1,5 +1,10 @@
1-from collections.abc import Collection
2-from typing import Any, Callable, TypeVar
1+from typing import Any, Callable, TypeVar, Dict, Collection
2+try:
3+ import colorama
4+ import blessings
5+ nocolors = False
6+except ModuleNotFoundError:
7+ nocolors = True
38
49 __all__ = [
510 'extended_gcd', 'print_group_table', 'mult_inverse', 'print_mult_inverses'
@@ -62,18 +67,29 @@
6267 T = TypeVar('T')
6368 def print_group_table(
6469 elements: Collection[T],
65- op: Callable[[T, T], T]
70+ op: Callable[[T, T], T],
71+ highlight_map: Dict[T, int] = {}
6672 ):
73+ if not nocolors:
74+ colorama.init()
75+ term = blessings.Terminal()
6776 width = max(len(str(x)) for x in elements)
6877 print(f'{" ":{width}} |', end='')
78+ def element_str(a, width):
79+ s = f'{a:{width}}'
80+ spaces = ' ' * (len(s) - len(str(a)))
81+ if (not nocolors) and (a in highlight_map):
82+ return spaces + term.color(highlight_map[a])(str(a))
83+ else:
84+ return s
6985 for a in elements:
70- print(f' {a:{width}} |', end='')
86+ print(f' {element_str(a, width)} |', end='')
7187 print()
7288 for a in elements:
73- print(f'{a:{width}} |', end='')
89+ print(f'{element_str(a, width)} |', end='')
7490 for b in elements:
7591 result = op(a, b)
76- print(f' {result:{width}} |', end='')
92+ print(f' {element_str(result, width)} |', end='')
7793 print()
7894
7995