Coding style

- Sometimes use functools.cache.
This commit is contained in:
Andrew Hamilton 2022-03-12 14:36:58 +10:00
parent e54a07d038
commit 93a5cad003
2 changed files with 5 additions and 6 deletions

View file

@ -163,7 +163,7 @@ def fix_paths(root_path, paths):
def highlight_str(line, highlight_color, transparency): def highlight_str(line, highlight_color, transparency):
@functools.lru_cache() @functools.lru_cache(maxsize=500)
def blend_style(style): def blend_style(style):
fg_color = (style.fg_color if type(style.fg_color) == tuple fg_color = (style.fg_color if type(style.fg_color) == tuple
else termstr.XTERM_COLORS[style.fg_color]) else termstr.XTERM_COLORS[style.fg_color])
@ -918,13 +918,12 @@ class Screen:
_STATUS_BAR = highlight_chars(" *help *quit *t*a*b:focus *turn *log *edit *next *sort" _STATUS_BAR = highlight_chars(" *help *quit *t*a*b:focus *turn *log *edit *next *sort"
" *refresh *fullscreen *open", Log._GREEN_STYLE) " *refresh *fullscreen *open", Log._GREEN_STYLE)
@functools.lru_cache() @functools.cache
def _get_partial_bar_chars(self, bar_transparency): def _get_partial_bar_chars(self, bar_transparency):
bar_color = termstr.blend_color(termstr.Color.black, termstr.Color.white, bar_transparency) bar_color = termstr.blend_color(termstr.Color.black, termstr.Color.white, bar_transparency)
return [termstr.TermStr(char).fg_color(bar_color).bg_color(termstr.Color.black) return [termstr.TermStr(char).fg_color(bar_color).bg_color(termstr.Color.black)
for char in fill3.ScrollBar._PARTIAL_CHARS[1]] for char in fill3.ScrollBar._PARTIAL_CHARS[1]]
@functools.lru_cache(maxsize=2)
def _get_status_bar_appearance(self, width, progress_bar_size): def _get_status_bar_appearance(self, width, progress_bar_size):
bar_transparency = 0.7 bar_transparency = 0.7
bar = self._STATUS_BAR.center(width)[:width] bar = self._STATUS_BAR.center(width)[:width]

View file

@ -519,7 +519,7 @@ def dump_pickle_safe(object_, path, protocol=pickle.HIGHEST_PROTOCOL, open=open)
os.rename(tmp_path, path) os.rename(tmp_path, path)
@functools.lru_cache() @functools.cache
def compression_open_func(compression): def compression_open_func(compression):
return open if compression == "none" else importlib.import_module(compression).open return open if compression == "none" else importlib.import_module(compression).open
@ -619,7 +619,7 @@ for extensions, tool_names in tools_for_extensions:
(extensions, [globals()[tool_name] for tool_name in tool_names])) (extensions, [globals()[tool_name] for tool_name in tool_names]))
@functools.lru_cache(maxsize=1) @functools.cache
def _tools_for_extension(): def _tools_for_extension():
result = {} result = {}
for extensions, tools in TOOLS_FOR_EXTENSIONS: for extensions, tools in TOOLS_FOR_EXTENSIONS:
@ -661,7 +661,7 @@ def splitext(path):
return root, ext return root, ext
@functools.lru_cache() @functools.cache
def is_tool_available(tool): def is_tool_available(tool):
if (hasattr(tool, "command") and tool.command.startswith(f"{PYTHON_EXECUTABLE} -m ")): if (hasattr(tool, "command") and tool.command.startswith(f"{PYTHON_EXECUTABLE} -m ")):
try: try: