def combine_sp_values(self, other): """ sp_values * sp_values """ if self.values is None: ret = IdValues() else: ret = IdValues({ k: int(v) * int(other.values[k]) for k, v in self.values.items() }) return ret
def combine_numeric_values(self, other): """ numeric_values * sp_values """ if self.values is None: ret = IdValues() else: ret = sum([ IdValues({ k: int(v) * int(other.values[key]) for k, v in value.items() }) for key, value in self.values.items() ]) return ret
def sum_combined_sp_values(self, other): """ sum(sp_values * sp_values) """ if self.values is None: ret = IdValues() else: ret = IdValues({ '0': sum( int(x) for x in { k: int(v) * int(other.values[k]) for k, v in self.values.items() }.values()) }) return ret
def numeric_values(self): if self.values is None: ret = [] else: ret = [IdValues({k: int(v) for k, v in value.items()}) for value in self.values.values()] return ret
def sum_sp_values(self): """ return system level values (spa + spb) input: "values": { "spa": 385, "spb": 505 }, return: "values": { "0": 890 }, """ if self.values is None: ret = IdValues() else: ret = IdValues({'0': sum(int(x) for x in self.values.values())}) return ret
def sp_sum_values(self): """ return sp level values input: "values": { "spa": { "19": "385", "18": "0", "20": "0", "17": "0", "16": "0" }, "spb": { "19": "101", "18": "101", "20": "101", "17": "101", "16": "101" } }, return: "values": { "spa": 385, "spb": 505 }, """ if self.values is None: ret = IdValues() else: ret = IdValues({ k: sum(int(x) for x in v.values()) for k, v in self.values.items() }) return ret
def setUp(self): self.o1 = IdValues({'a': 2, 'b': 3}) self.o2 = IdValues({'a': 7, 'c': 13}) self.o3 = IdValues({'a': None, 'b': 17})
class IdValuesTest(TestCase): def setUp(self): self.o1 = IdValues({'a': 2, 'b': 3}) self.o2 = IdValues({'a': 7, 'c': 13}) self.o3 = IdValues({'a': None, 'b': 17}) def test_add_none(self): o1 = self.o1 r = o1 + None assert_that(o1['a'], equal_to(2)) assert_that(o1['b'], equal_to(3)) assert_that(r['a'], equal_to(2)) assert_that(r['b'], equal_to(3)) r.set('a', 5) assert_that(o1['a'], equal_to(2)) assert_that(r['a'], equal_to(5)) def test_sum(self): r = sum([self.o1, self.o2, self.o3]) assert_that(r['a'], equal_to(9)) assert_that(r['b'], equal_to(20)) assert_that(r['c'], equal_to(13)) def test_self_add(self): r = self.o1.copy() r += self.o2 assert_that(r['a'], equal_to(9)) assert_that(r['b'], equal_to(3)) assert_that(r['c'], equal_to(13)) def test_add_has_none(self): r = self.o1 + self.o3 assert_that(r['a'], equal_to(2)) assert_that(r['b'], equal_to(20)) def test_radd_none(self): o1 = self.o1 r = None + o1 assert_that(o1['a'], equal_to(2)) assert_that(o1['b'], equal_to(3)) assert_that(r['a'], equal_to(2)) assert_that(r['b'], equal_to(3)) r.set('a', 5) assert_that(o1['a'], equal_to(2)) assert_that(r['a'], equal_to(5)) def test_add_id_value(self): o1 = self.o1 o2 = self.o2 r = o1 + o2 assert_that(o1['a'], equal_to(2)) assert_that(o1['b'], equal_to(3)) assert_that(len(o1), equal_to(2)) assert_that(r['a'], equal_to(9)) assert_that(r['b'], equal_to(3)) assert_that(r['c'], equal_to(13)) assert_that(len(r), equal_to(3)) def test_add_int(self): o1 = self.o1 r = o1 + 5 assert_that(o1['a'], equal_to(2)) assert_that(o1['b'], equal_to(3)) assert_that(len(o1), equal_to(2)) assert_that(r['a'], equal_to(7)) assert_that(r['b'], equal_to(8)) assert_that(len(r), equal_to(2)) def test_radd_float(self): o1 = self.o1 r = 5.1 + o1 assert_that(o1['a'], equal_to(2)) assert_that(o1['b'], equal_to(3)) assert_that(len(o1), equal_to(2)) assert_that(r['a'], equal_to(7.1)) assert_that(r['b'], equal_to(8.1)) assert_that(len(r), equal_to(2)) def test_sub_id_values(self): r = self.o2 - self.o1 assert_that(self.o2['a'], equal_to(7)) assert_that(self.o2['c'], equal_to(13)) assert_that(r['a'], equal_to(5)) assert_that(r['b'], equal_to(-3)) assert_that(r['c'], equal_to(13)) def test_rsub_none(self): r = None - self.o1 assert_that(r['a'], equal_to(-2)) assert_that(r['b'], equal_to(-3)) def test_rsub_float(self): r = 9.5 - self.o1 assert_that(r['a'], equal_to(7.5)) assert_that(r['b'], equal_to(6.5)) def test_add_from_empty(self): r = IdValues() + self.o1 assert_that(r['a'], equal_to(2)) assert_that(r['b'], equal_to(3)) def test_div_id_value(self): r = self.o1 / self.o2 assert_that(r['a'], equal_to(2.0 / 7.0)) assert_that(r['b'], is_nan()) assert_that(r['c'], is_nan()) def test_div_numeric(self): r = self.o1 / 2 assert_that(r['a'], equal_to(1.0)) assert_that(r['b'], equal_to(1.5)) def test_div_by_zero(self): r = self.o1 / 0 assert_that(r['a'], is_nan()) assert_that(r['b'], is_nan()) def test_div_zero_dev(self): r = 0.0 / IdValues({'a': 0, 'b': 3}) assert_that(r['a'], equal_to(0.0)) assert_that(r['b'], equal_to(0.0)) def test_div_by_partial_zero(self): r = self.o1 / IdValues({'a': 2, 'b': 0}) assert_that(r['a'], equal_to(1.0)) assert_that(r['b'], is_nan()) def test_rdiv_numerical(self): r = 12 / self.o1 assert_that(r['a'], equal_to(6)) assert_that(r['b'], equal_to(4)) def test_rdiv_none(self): r = None / self.o1 assert_that(r['a'], is_nan()) assert_that(r['b'], is_nan()) def test_mul_none(self): r = self.o1 * None assert_that(r['a'], equal_to(2)) assert_that(r['b'], equal_to(3)) def test_rmul_none(self): r = None * self.o1 assert_that(r['a'], equal_to(2)) assert_that(r['b'], equal_to(3)) def test_mul_numerical(self): r = self.o1 * 3 assert_that(r['a'], equal_to(6)) assert_that(r['b'], equal_to(9)) def test_rmul_numerical(self): r = 3 * self.o1 assert_that(r['a'], equal_to(6)) assert_that(r['b'], equal_to(9)) def test_mul_id_values(self): r = self.o1 * self.o2 assert_that(r['a'], equal_to(14)) assert_that(r['b'], equal_to(3)) assert_that(r['c'], equal_to(13)) def test_mul_partial_none(self): r = self.o1 * self.o3 assert_that(r['a'], equal_to(2)) assert_that(r['b'], equal_to(51))
def test_div_by_partial_zero(self): r = self.o1 / IdValues({'a': 2, 'b': 0}) assert_that(r['a'], equal_to(1.0)) assert_that(r['b'], is_nan())
def test_div_zero_dev(self): r = 0.0 / IdValues({'a': 0, 'b': 3}) assert_that(r['a'], equal_to(0.0)) assert_that(r['b'], equal_to(0.0))
def test_add_from_empty(self): r = IdValues() + self.o1 assert_that(r['a'], equal_to(2)) assert_that(r['b'], equal_to(3))
def sp_values(self): if self.values is None: ret = IdValues() else: ret = IdValues({k: int(v) for k, v in self.values.items()}) return ret