CSIT-1110: Add files for PyPI migration
[csit.git] / PyPI / jumpavg / jumpavg / BitCountingGroup.py
1 # Copyright (c) 2018 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 holding BitCountingGroup class."""
15
16 from RunGroup import RunGroup
17
18
19 class BitCountingGroup(RunGroup):
20     """RunGroup with BitCountingMetadata.
21
22     Support with_run_added() method to simplify extending the group.
23     As bit content has to be re-counted, metadata factory is stored.
24     """
25
26     def __init__(self, metadata_factory, values=[]):
27         """Create the group from metadata factory and values.
28
29         :param metadata_factory: Factory object to create metadata with.
30         :param values: The runs belonging to this group.
31         :type metadata_factory: BitCountingMetadataFactory
32         :type values: Iterable of float or of AvgStdevMetadata
33         """
34         self.metadata_factory = metadata_factory
35         metadata = metadata_factory.from_data(values)
36         super(BitCountingGroup, self).__init__(metadata, values)
37
38     def with_run_added(self, value):
39         """Create and return a new group with one more run that self.
40
41         :param value: The run value to add to the group.
42         :type value: float or od AvgStdevMetadata
43         :returns: New group with the run added.
44         :rtype: BitCountingGroup
45         """
46         values = list(self.values)
47         values.append(value)
48         return BitCountingGroup(self.metadata_factory, values)
49         # TODO: Is there a good way to save some computation
50         # by copy&updating the metadata incrementally?