[blivet:master 19/20] Add a new way of specifying Sizes().
mulhern
amulhern at redhat.com
Tue Dec 23 23:42:32 UTC 2014
This allows to specify Sizes with units reasonably succinctly, but at the
same time to avoid the long and costly _parseSpec() path.
Signed-off-by: mulhern <amulhern at redhat.com>
---
blivet/size.py | 38 ++++++++++++++++++++++----------------
tests/size_test.py | 4 ++++
2 files changed, 26 insertions(+), 16 deletions(-)
diff --git a/blivet/size.py b/blivet/size.py
index 1133508..de1d118 100644
--- a/blivet/size.py
+++ b/blivet/size.py
@@ -225,22 +225,24 @@ class Size(Decimal):
"""
def __new__(cls, value=0, context=None):
- """ Initialize a new Size object. Must pass a bytes or a spec value
- for size. The bytes value is a numerical value for the size
- this object represents, in bytes. The spec value is a string
- specification of the size using any of the size specifiers in the
- _DECIMAL_PREFIXES or _BINARY_PREFIXES lists combined with a 'b' or
- 'B'. For example, to specify 640 kilobytes, you could pass any of
- these spec parameters:
-
- "640kb"
- "640 kb"
- "640KB"
- "640 KB"
- "640 kilobytes"
-
- If you want to use a spec value to represent a bytes value,
- you can use the letter 'b' or 'B' or omit the size specifier.
+ """ Initialize a new Size object.
+
+ :param value: specification of the number of bytes
+ :type value: tuple, str, Size, or any number type
+
+ A tuple value must consists of a pair of a number and a unit
+ specifier like those as an argument to convertTo(). For example,
+ (640, size.KiB) represents 640 KiB.
+
+ A str value combines the number and the units into a simple string
+ specification, like "640 KiB". The string specification is
+ the most appropriate for user input size specifications, as it
+ can handle locale-specific unit specifications and long word
+ specifications like "640 kilobytes".
+
+ A Size value works like a copy constructor.
+
+ A numeric value is taken to represent the number of bytes.
"""
if isinstance(value, (six.string_types, bytes)):
size = _parseSpec(value)
@@ -248,6 +250,10 @@ class Size(Decimal):
size = Decimal(value)
elif isinstance(value, Size):
size = Decimal(value.convertTo())
+ elif isinstance(value, tuple) and len(value) == 2 and \
+ isinstance(value[0], (six.integer_types, float, Decimal)) and \
+ isinstance(value[1], _Prefix):
+ size = Decimal(value[0] * value[1].factor)
else:
raise ValueError("invalid value %s for size" % value)
diff --git a/tests/size_test.py b/tests/size_test.py
index a3ee311..b25678a 100644
--- a/tests/size_test.py
+++ b/tests/size_test.py
@@ -57,12 +57,16 @@ class SizeTestCase(unittest.TestCase):
u = size._makeSpec(unit.prefix, size._BYTES_WORDS[0], False)
s = Size("%ld %s" % (numunits, u))
+ t = Size((numunits, unit))
self.assertEquals(s, c)
+ self.assertEquals(t, c)
self.assertEquals(s.convertTo(unit), numunits)
u = size._makeSpec(unit.abbr, size._BYTES_SYMBOL, False)
s = Size("%ld %s" % (numunits, u))
+ t = Size((numunits, unit))
self.assertEquals(s, c)
+ self.assertEquals(t, c)
self.assertEquals(s.convertTo(unit), numunits)
def testPrefixes(self):
--
1.9.3
More information about the anaconda-patches
mailing list