(st, color:str|None, background=False)
| 31 | def all_same(items:Sequence): return all(x == items[0] for x in items) # works for empty input |
| 32 | def all_int(t: Sequence[Any]) -> TypeGuard[tuple[int, ...]]: return all(isinstance(s, int) for s in t) |
| 33 | def colored(st, color:str|None, background=False): # replace the termcolor library |
| 34 | if NO_COLOR: return st |
| 35 | colors = ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'] |
| 36 | return f"\u001b[{10*background+60*(color.upper() == color)+30+colors.index(color.lower())}m{st}\u001b[0m" if color is not None else st |
| 37 | def colorize_float(x: float): return colored(f"{x:7.2f}x", 'green' if x < 0.75 else 'red' if x > 1.15 else 'yellow') |
| 38 | def time_to_str(t:float, w=8) -> str: return next((f"{t * d:{w}.2f}{pr}" for d,pr in [(1, "s "),(1e3, "ms")] if t > 10/d), f"{t * 1e6:{w}.2f}us") |
| 39 | def size_to_str(s:int) -> str: return next((f"{s / d:.2f} {pr}" for d,pr in [(1<<30, "GB"),(1<<20, "MB"),(1<<10, "KB")] if s >= d), f"{s} B") |
searching dependent graphs…