Revert "fix(IPsecUtil): Delete keywords no longer used"
[csit.git] / resources / libraries / python / MLRsearch / discrete_result.py
1 # Copyright (c) 2023 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """Module defining DiscreteResult class."""
15
16 from __future__ import annotations
17
18 from dataclasses import dataclass
19
20 from .discrete_load import DiscreteLoad
21 from .trial_measurement import MeasurementResult
22
23
24 @dataclass
25 class DiscreteResult(MeasurementResult):
26     """A measurement result where intended load is also given as discrete load.
27
28     The discrete load has to be round and has to match the intended load.
29     """
30
31     # Must have default as superclass has fields with default values.
32     discrete_load: DiscreteLoad = None
33     """Intended load [tps]; discrete, round and equal to intended load."""
34
35     def __post_init__(self) -> None:
36         """Call super, verify intended and discrete loads are the same.
37
38         :raises TypeError: If discrete load is not DiscreteLoad.
39         :raises ValueError: If the discrete load is not round.
40         :raises ValueError: If the load does not match intended load.
41         """
42         super().__post_init__()
43         if not isinstance(self.discrete_load, DiscreteLoad):
44             raise TypeError(f"Not a discrete load: {self.discrete_load!r}")
45         if not self.discrete_load.is_round:
46             raise ValueError(f"Discrete load not round: {self.discrete_load!r}")
47         if float(self.discrete_load) != self.intended_load:
48             raise ValueError(f"Load mismatch: {self!r}")
49
50     @staticmethod
51     def with_load(
52         result: MeasurementResult, load: DiscreteLoad
53     ) -> DiscreteResult:
54         """Return result with added load.
55
56         :param result: A result, possibly without discrete load.
57         :param load: Discrete load to add.
58         :type result: MeasurementResult
59         :type load: DiscreteLoad
60         :returns: Equivalent result with matching discrete load.
61         :rtype: DiscreteResult
62         :raises TypeError: If discrete load is not DiscreteLoad.
63         :raises ValueError: If the discrete load is not round.
64         :raises ValueError: If the load does not match intended load.
65         """
66         return DiscreteResult(
67             intended_duration=result.intended_duration,
68             intended_load=result.intended_load,
69             offered_count=result.offered_count,
70             loss_count=result.loss_count,
71             forwarding_count=result.forwarding_count,
72             offered_duration=result.offered_duration,
73             duration_with_overheads=result.duration_with_overheads,
74             intended_count=result.intended_count,
75             discrete_load=load,
76         )