From 4511128c9f5e6a451057177e15a0f591bcab6110 Mon Sep 17 00:00:00 2001 From: Andrew Hamilton Date: Thu, 30 Apr 2020 00:35:44 +1000 Subject: [PATCH] Coding style. - Use new 'cached_property' in python3.8. --- eris/termstr.py | 22 ++++++---------------- tests/termstr_test.py | 21 +-------------------- 2 files changed, 7 insertions(+), 36 deletions(-) diff --git a/eris/termstr.py b/eris/termstr.py index 07f3f7f..4a3c362 100644 --- a/eris/termstr.py +++ b/eris/termstr.py @@ -24,16 +24,6 @@ def xterm_color_to_rgb(color_index): return eris.ColorMap._rgb(xterm_colormap.colors[color_index]) -def _cache_first_result(user_function): - def decorator(self, *args, **kwds): - try: - return self._cache - except AttributeError: - self._cache = user_function(self, *args, **kwds) - return self._cache - return decorator - - class Color: # https://en.wikipedia.org/wiki/Natural_Color_System @@ -113,7 +103,7 @@ class CharStyle: else: return terminal.rgb_color(color, is_foreground) - @_cache_first_result + @functools.cached_property def code_for_term(self): fg_termcode = terminal.ESC + self._color_code(self.fg_color, True) bg_termcode = terminal.ESC + self._color_code(self.bg_color, False) @@ -220,7 +210,7 @@ class TermStr(collections.UserString): def __hash__(self): return hash((self.data, self.style)) - @_cache_first_result + @functools.cached_property def _partition_style(self): if self.data == "": return [] @@ -238,8 +228,8 @@ class TermStr(collections.UserString): def __str__(self): return "".join(_join_lists( - [style.code_for_term(), str_] - for style, str_, position in self._partition_style()) + + [style.code_for_term, str_] + for style, str_, position in self._partition_style) + [terminal.ESC + terminal.normal]) def __repr__(self): @@ -327,7 +317,7 @@ class TermStr(collections.UserString): def transform_style(self, transform_func): new_style = tuple(_join_lists([transform_func(style)] * len(str_) for style, str_, position - in self._partition_style())) + in self._partition_style)) return self.__class__(self.data, new_style) def bold(self): @@ -368,7 +358,7 @@ class TermStr(collections.UserString): def as_html(self): result = [] styles = set() - for style, str_, position in self._partition_style(): + for style, str_, position in self._partition_style: styles.add(style) encoded = str(html.escape(str_).encode( "ascii", "xmlcharrefreplace"))[2:-1] diff --git a/tests/termstr_test.py b/tests/termstr_test.py index 114b22a..f0c389a 100755 --- a/tests/termstr_test.py +++ b/tests/termstr_test.py @@ -14,25 +14,6 @@ from eris.termstr import TermStr, CharStyle import eris.termstr as termstr -class CacheFirstResultTestCase(unittest.TestCase): - - def test_cache_first_result_decorator(self): - class A: - @termstr._cache_first_result - def a(self, foo): - return foo - a = A() - self.assertEqual(a.a(3), 3) - self.assertEqual(a.a(4), 3) - - class B: - @termstr._cache_first_result - def b(self, foo): - return foo - b = B() - self.assertEqual(b.b(5), 5) - - class CharStyleTests(unittest.TestCase): def setUp(self): @@ -55,7 +36,7 @@ class CharStyleTests(unittest.TestCase): "") def test_code_for_term(self): - self.assertEqual(self.style.code_for_term(), + self.assertEqual(self.style.code_for_term, "\x1b[m\x1b[38;2;255;255;255m\x1b[48;2;0;0;0m")