1 # Copyright (c) 2017 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:
6 # http://www.apache.org/licenses/LICENSE-2.0
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.
14 """General purpose utilities.
18 from os.path import join
23 """Calculate mean value from the items.
25 :param items: Mean value is calculated from these items.
31 return float(sum(items)) / len(items)
35 """Calculate stdev from the items.
37 :param items: Stdev is calculated from these items.
44 variance = [(x - avg) ** 2 for x in items]
45 stddev = sqrt(mean(variance))
49 def relative_change(nr1, nr2):
50 """Compute relative change of two values.
52 :param nr1: The first number.
53 :param nr2: The second number.
56 :returns: Relative change of nr1.
60 return float(((nr2 - nr1) / nr1) * 100)
63 def get_files(path, extension=None, full_path=True):
64 """Generates the list of files to process.
66 :param path: Path to files.
67 :param extension: Extension of files to process. If it is the empty string,
68 all files will be processed.
69 :param full_path: If True, the files with full path are generated.
73 :returns: List of files to process.
78 for root, _, files in walk(path):
79 for filename in files:
81 if filename.endswith(extension):
83 file_list.append(join(root, filename))
85 file_list.append(filename)
87 file_list.append(join(root, filename))
92 def get_rst_title_char(level):
93 """Return character used for the given title level in rst files.
95 :param level: Level of the title.
97 :returns: Character used for the given title level in rst files.
100 chars = ('=', '-', '`', "'", '.', '~', '*', '+', '^')
101 if level < len(chars):