Coding style.

- Avoid using eval.
- Use more lru_caches.
This commit is contained in:
Andrew Hamilton 2021-12-09 23:32:14 +10:00
parent 2dba7a6920
commit d8f21d97ba

View file

@ -114,11 +114,13 @@ def deps(**kwargs):
def _syntax_highlight(text, lexer, style): def _syntax_highlight(text, lexer, style):
@functools.lru_cache(maxsize=500)
def _parse_rgb(hex_rgb): def _parse_rgb(hex_rgb):
if hex_rgb.startswith("#"): if hex_rgb.startswith("#"):
hex_rgb = hex_rgb[1:] hex_rgb = hex_rgb[1:]
return tuple(eval("0x"+hex_rgb[index:index+2]) for index in [0, 2, 4]) return tuple(int("0x" + hex_rgb[index:index+2], base=16) for index in [0, 2, 4])
@functools.lru_cache(maxsize=500)
def _char_style_for_token_type(token_type, default_bg_color, default_style): def _char_style_for_token_type(token_type, default_bg_color, default_style):
try: try:
token_style = style.style_for_token(token_type) token_style = style.style_for_token(token_type)