Skip to content

Commit e825f56

Browse files
committed
add function to parse canonical quantities (e.g. resources)
This utility function is useful to parse values like the 200m or 300Gi memory and cpu resources stored in kubernetes manifests. It uses Decimal as output format as it usually represents typical input values more accurately and reduces rounding errors.
1 parent 08fefc3 commit e825f56

File tree

3 files changed

+186
-0
lines changed

3 files changed

+186
-0
lines changed

kubernetes/test/test_quantity.py

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# coding: utf-8
2+
# Copyright 2019 The Kubernetes Authors.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
from __future__ import absolute_import
17+
18+
import unittest
19+
from kubernetes.utils import parse_quantity
20+
from decimal import Decimal
21+
22+
23+
class TestQuantity(unittest.TestCase):
24+
def test_parse(self):
25+
self.assertIsInstance(parse_quantity(2.2), Decimal)
26+
# input, expected output
27+
tests = [
28+
(0, 0),
29+
(2, 2),
30+
(2, Decimal("2")),
31+
(2., 2),
32+
(Decimal("2.2"), Decimal("2.2")),
33+
(2., Decimal(2)),
34+
(Decimal("2."), 2),
35+
("123", 123),
36+
("2", 2),
37+
("2n", Decimal("2") * Decimal(1000)**-3),
38+
("2u", Decimal("0.000002")),
39+
("2m", Decimal("0.002")),
40+
("0m", Decimal("0")),
41+
("0M", Decimal("0")),
42+
("223k", 223000),
43+
("002M", 2 * 1000**2),
44+
("2M", 2 * 1000**2),
45+
("4123G", 4123 * 1000**3),
46+
("2T", 2 * 1000**4),
47+
("2P", 2 * 1000**5),
48+
("2E", 2 * 1000**6),
49+
50+
("223Ki", 223 * 1024),
51+
("002Mi", 2 * 1024**2),
52+
("2Mi", 2 * 1024**2),
53+
("2Gi", 2 * 1024**3),
54+
("4123Gi", 4123 * 1024**3),
55+
("2Ti", 2 * 1024**4),
56+
("2Pi", 2 * 1024**5),
57+
("2Ei", 2 * 1024**6),
58+
59+
("2.34n", Decimal("2.34") * Decimal(1000)**-3),
60+
("2.34u", Decimal("2.34") * Decimal(1000)**-2),
61+
("2.34m", Decimal("2.34") * Decimal(1000)**-1),
62+
("2.34Ki", Decimal("2.34") * 1024),
63+
("2.34", Decimal("2.34")),
64+
(".34", Decimal("0.34")),
65+
("34.", 34),
66+
(".34M", Decimal("0.34") * 1000**2),
67+
68+
("2e2K", Decimal("2e2") * 1000),
69+
("2e2Ki", Decimal("2e2") * 1024),
70+
("2e-2Ki", Decimal("2e-2") * 1024),
71+
("2.34E1", Decimal("2.34E1")),
72+
(".34e-2", Decimal("0.34e-2")),
73+
]
74+
75+
for inp, out in tests:
76+
self.assertEqual(parse_quantity(inp), out)
77+
if isinstance(inp, (int, float, Decimal)):
78+
self.assertEqual(parse_quantity(-1 * inp), -out)
79+
else:
80+
self.assertEqual(parse_quantity("-" + inp), -out)
81+
self.assertEqual(parse_quantity("+" + inp), out)
82+
83+
def test_parse_invalid(self):
84+
self.assertRaises(ValueError, parse_quantity, [])
85+
self.assertRaises(ValueError, parse_quantity, "")
86+
self.assertRaises(ValueError, parse_quantity, "-")
87+
self.assertRaises(ValueError, parse_quantity, "i")
88+
self.assertRaises(ValueError, parse_quantity, "2i")
89+
self.assertRaises(ValueError, parse_quantity, "2mm")
90+
self.assertRaises(ValueError, parse_quantity, "2mmKi")
91+
self.assertRaises(ValueError, parse_quantity, "2KKi")
92+
self.assertRaises(ValueError, parse_quantity, "2e")
93+
self.assertRaises(ValueError, parse_quantity, "2.2i")
94+
self.assertRaises(ValueError, parse_quantity, "bla")
95+
self.assertRaises(ValueError, parse_quantity, "Ki")
96+
self.assertRaises(ValueError, parse_quantity, "M")
97+
self.assertRaises(ValueError, parse_quantity, "2ki")
98+
self.assertRaises(ValueError, parse_quantity, "2Ki ")
99+
self.assertRaises(ValueError, parse_quantity, "20Ki ")
100+
self.assertRaises(ValueError, parse_quantity, "20B")
101+
self.assertRaises(ValueError, parse_quantity, "20Bi")
102+
self.assertRaises(ValueError, parse_quantity, "20.2Bi")
103+
self.assertRaises(ValueError, parse_quantity, "2MiKi")
104+
self.assertRaises(ValueError, parse_quantity, "2MK")
105+
self.assertRaises(ValueError, parse_quantity, "2MKi")
106+
self.assertRaises(ValueError, parse_quantity, "234df")
107+
self.assertRaises(ValueError, parse_quantity, "df234")
108+
self.assertRaises(ValueError, parse_quantity, tuple())
109+
110+
111+
if __name__ == '__main__':
112+
unittest.main()

kubernetes/utils/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616

1717
from .create_from_yaml import (FailToCreateError, create_from_dict,
1818
create_from_yaml)
19+
from .quantity import parse_quantity

kubernetes/utils/quantity.py

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Copyright 2019 The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
from decimal import Decimal, InvalidOperation
15+
16+
17+
def parse_quantity(quantity):
18+
"""
19+
Parse kubernetes canonical form quantity like 200Mi to a decimal number.
20+
Supported SI suffixes:
21+
base1024: Ki | Mi | Gi | Ti | Pi | Ei
22+
base1000: n | u | m | "" | k | M | G | T | P | E
23+
24+
Input:
25+
quanity: string. kubernetes canonical form quantity
26+
27+
Returns:
28+
Decimal
29+
30+
Raises:
31+
ValueError on invalid or unknown input
32+
"""
33+
if isinstance(quantity, (int, float, Decimal)):
34+
return Decimal(quantity)
35+
36+
exponents = {"n": -3, "u": -2, "m": -1, "K": 1, "k": 1, "M": 2,
37+
"G": 3, "T": 4, "P": 5, "E": 6}
38+
39+
quantity = str(quantity)
40+
number = quantity
41+
suffix = None
42+
if len(quantity) >= 2 and quantity[-1] == "i":
43+
if quantity[-2] in exponents:
44+
number = quantity[:-2]
45+
suffix = quantity[-2:]
46+
elif len(quantity) >= 1 and quantity[-1] in exponents:
47+
number = quantity[:-1]
48+
suffix = quantity[-1:]
49+
50+
try:
51+
number = Decimal(number)
52+
except InvalidOperation:
53+
raise ValueError("Invalid number format: {}".format(number))
54+
55+
if suffix is None:
56+
return number
57+
58+
if suffix.endswith("i"):
59+
base = 1024
60+
elif len(suffix) == 1:
61+
base = 1000
62+
else:
63+
raise ValueError("{} has unknown suffix".format(quantity))
64+
65+
# handly SI inconsistency
66+
if suffix == "ki":
67+
raise ValueError("{} has unknown suffix".format(quantity))
68+
69+
if suffix[0] not in exponents:
70+
raise ValueError("{} has unknown suffix".format(quantity))
71+
72+
exponent = Decimal(exponents[suffix[0]])
73+
return number * (base ** exponent)

0 commit comments

Comments
 (0)