Skip to content

Commit a5cb3d7

Browse files
authored
Merge pull request #855 from juliantaylor/parse-quantity
add function to parse canonical quantities (e.g. resources)
2 parents 08fefc3 + 8b385a8 commit a5cb3d7

File tree

3 files changed

+188
-0
lines changed

3 files changed

+188
-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

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
See https://github.com/kubernetes/apimachinery/blob/master/pkg/api/resource/quantity.go
25+
26+
Input:
27+
quanity: string. kubernetes canonical form quantity
28+
29+
Returns:
30+
Decimal
31+
32+
Raises:
33+
ValueError on invalid or unknown input
34+
"""
35+
if isinstance(quantity, (int, float, Decimal)):
36+
return Decimal(quantity)
37+
38+
exponents = {"n": -3, "u": -2, "m": -1, "K": 1, "k": 1, "M": 2,
39+
"G": 3, "T": 4, "P": 5, "E": 6}
40+
41+
quantity = str(quantity)
42+
number = quantity
43+
suffix = None
44+
if len(quantity) >= 2 and quantity[-1] == "i":
45+
if quantity[-2] in exponents:
46+
number = quantity[:-2]
47+
suffix = quantity[-2:]
48+
elif len(quantity) >= 1 and quantity[-1] in exponents:
49+
number = quantity[:-1]
50+
suffix = quantity[-1:]
51+
52+
try:
53+
number = Decimal(number)
54+
except InvalidOperation:
55+
raise ValueError("Invalid number format: {}".format(number))
56+
57+
if suffix is None:
58+
return number
59+
60+
if suffix.endswith("i"):
61+
base = 1024
62+
elif len(suffix) == 1:
63+
base = 1000
64+
else:
65+
raise ValueError("{} has unknown suffix".format(quantity))
66+
67+
# handly SI inconsistency
68+
if suffix == "ki":
69+
raise ValueError("{} has unknown suffix".format(quantity))
70+
71+
if suffix[0] not in exponents:
72+
raise ValueError("{} has unknown suffix".format(quantity))
73+
74+
exponent = Decimal(exponents[suffix[0]])
75+
return number * (base ** exponent)

0 commit comments

Comments
 (0)