[blivet:master 3/4] Factor out commonalities in xlated_*_prefix() methods.

mulhern amulhern at redhat.com
Wed Oct 1 13:17:30 UTC 2014


Use in _xlated_prefixes().

_xlated_prefixes() evaluated everything to a list. The point of making
_xlated_binary_prefix() to evaluate to a lazy comprehension was to delay
evaluation, but _xlated_prefixes() evaluated everything, so there was little
point. Now evaluation is actually delayed until needed.

Signed-off-by: mulhern <amulhern at redhat.com>
---
 blivet/size.py | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)

diff --git a/blivet/size.py b/blivet/size.py
index 1443b59..1cfad83 100644
--- a/blivet/size.py
+++ b/blivet/size.py
@@ -69,23 +69,22 @@ _BINARY_PREFIXES = [
 _BYTES = [N_(b'B'), N_(b'b'), N_(b'byte'), N_(b'bytes')]
 _PREFIXES = _BINARY_PREFIXES + _DECIMAL_PREFIXES
 
-# Translated versions of the byte and prefix arrays
+# Translated versions of the byte and prefix arrays as lazy comprehensions
 # All strings are decoded as utf-8 so that locale-specific upper/lower functions work
 def _xlated_bytes():
-    """Return a translated version of the bytes list as a list of unicode strings"""
-    return [_(b).decode("utf-8") for b in _BYTES]
+    return (_(b).decode("utf-8") for b in _BYTES)
+
+def _xlated_prefix(p):
+    return _Prefix(p.factor, _(p.prefix).decode("utf-8"), _(p.abbr).decode("utf-8"))
 
 def _xlated_binary_prefixes():
-    return (_Prefix(p.factor, _(p.prefix).decode("utf-8"), _(p.abbr).decode("utf-8")) \
-            for p in _BINARY_PREFIXES)
+    return (_xlated_prefix(p) for p in _BINARY_PREFIXES)
 
-def _xlated_prefixes():
-    """Return translated prefixes as unicode strings"""
-    xlated_binary = list(_xlated_binary_prefixes())
-    xlated_decimal = [_Prefix(p.factor, _(p.prefix).decode("utf-8"), _(p.abbr).decode("utf-8")) \
-                      for p in _DECIMAL_PREFIXES]
+def _xlated_decimal_prefixes():
+    return (_xlated_prefix(p) for p in _DECIMAL_PREFIXES)
 
-    return xlated_binary + xlated_decimal
+def _xlated_prefixes():
+    return itertools.chain(_xlated_binary_prefixes(), _xlated_decimal_prefixes())
 
 _ASCIIlower_table = string.maketrans(string.ascii_uppercase, string.ascii_lowercase)
 def _lowerASCII(s):
-- 
1.9.3



More information about the anaconda-patches mailing list