From 1636543bdaaa11cc27f6fb06a6787c7145860544 Mon Sep 17 00:00:00 2001 From: Erik Rose Date: Mon, 28 Nov 2011 16:07:13 -0800 Subject: Dispense with explicit cache dict, and just add attrs to the Terminal instance instead. This saves a __getattr__ call and a hash lookup and simplifies the code. --- blessings/__init__.py | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/blessings/__init__.py b/blessings/__init__.py index 7ca582d..8846f9c 100644 --- a/blessings/__init__.py +++ b/blessings/__init__.py @@ -93,13 +93,9 @@ class Terminal(object): # somewhere. setupterm(kind or environ.get('TERM', 'unknown'), init_descriptor) - - # Cache capability codes, because IIRC tigetstr requires a - # conversation with the terminal. [Now I can't find any evidence of - # that.] At any rate, save redoing the work of _resolve_formatter(). - self._codes = {} + self._is_null = False else: - self._codes = NullDict(lambda: NullCallableString()) + self._is_null = True self.stream = stream @@ -162,11 +158,9 @@ class Terminal(object): Return values are always Unicode. """ - if attr not in self._codes: - # Store sugary names under the sugary keys to save a hash lookup. - # Fall back to '' for codes not supported by this terminal. - self._codes[attr] = self._resolve_formatter(attr) - return self._codes[attr] + resolution = NullCallableString() if self._is_null else self._resolve_formatter(attr) + setattr(self, attr, resolution) # Cache capability codes. + return resolution @property def height(self): @@ -325,12 +319,6 @@ class NullCallableString(unicode): return arg # TODO: Force even strs in Python 2.x to be unicodes? Nah. How would I know what encoding to use to convert it? -class NullDict(defaultdict): - """A ``defaultdict`` that pretends to contain all keys""" - def __contains__(self, key): - return True - - def height_and_width(): """Return a tuple of (terminal height, terminal width).""" # tigetnum('lines') and tigetnum('cols') apparently don't update while -- cgit v1.2.1