Revert "fix(IPsecUtil): Delete keywords no longer used"
[csit.git] / resources / libraries / python / MLRsearch / trimmed_stat.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 TrimmedStat class."""
15
16 from __future__ import annotations
17
18 from dataclasses import dataclass
19
20 from .load_stats import LoadStats
21 from .target_spec import TargetSpec
22
23
24 @dataclass
25 class TrimmedStat(LoadStats):
26     """Load stats trimmed to a single target.
27
28     Useful mainly for reporting the overall results.
29     """
30
31     def __post_init__(self) -> None:
32         """Initialize load value and check there is one target to track."""
33         super().__post_init__()
34         if len(self.target_to_stat) != 1:
35             raise ValueError(f"No single target: {self.target_to_stat!r}")
36
37     @staticmethod
38     def for_target(stats: LoadStats, target: TargetSpec) -> TrimmedStat:
39         """Return new instance with only one target in the mapping.
40
41         :param stats: The load stats instance to trim.
42         :param target: The one target which should remain in the mapping.
43         :type stats: LoadStats
44         :type target: TargetSpec
45         :return: Newly created instance.
46         :rtype: TrimmedStat
47         """
48         return TrimmedStat(
49             rounding=stats.rounding,
50             int_load=stats.int_load,
51             target_to_stat={target: stats.target_to_stat[target]},
52         )