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

mulhern amulhern at redhat.com
Tue Sep 30 12:27:29 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 8cabe24..0e8b500 100644
--- a/blivet/size.py
+++ b/blivet/size.py
@@ -66,23 +66,22 @@ _emptyPrefix = _Prefix(1, "".decode("utf-8"), "".decode("utf-8"))
 _bytes = [N_(b'B'), N_(b'b'), N_(b'byte'), N_(b'bytes')]
 _prefixes = _binaryPrefixes + _decimalPrefixes
-# 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 _binaryPrefixes)
+    return (_xlated_prefix(p) for p in _binaryPrefixes)
-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 _decimalPrefixes]
+def _xlated_decimal_prefixes():
+    return (_xlated_prefix(p) for p in _decimalPrefixes)
-    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):

More information about the anaconda-patches mailing list