Coding style.
- Use new 'cached_property' in python3.8.
This commit is contained in:
parent
b6af395f81
commit
4511128c9f
2 changed files with 7 additions and 36 deletions
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -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):
|
|||
"<CharStyle: fg:(255, 255, 255) bg:(0, 0, 0) attr:>")
|
||||
|
||||
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")
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue