summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorErik Rose <erik@mozilla.com>2011-11-28 16:07:13 -0800
committerErik Rose <erik@mozilla.com>2011-11-28 17:50:01 -0800
commit1636543bdaaa11cc27f6fb06a6787c7145860544 (patch)
treecdef2b3211026d91ae9f2b77237dfe68d5055239
parent93d263666471b220880511a97aa3ee077bfd2910 (diff)
downloadblessings-1636543bdaaa11cc27f6fb06a6787c7145860544.tar.gz
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.
-rw-r--r--blessings/__init__.py22
1 files 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