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.
20 from os.path import join
25 """Calculate mean value from the items.
27 :param items: Mean value is calculated from these items.
33 return float(sum(items)) / len(items)
37 """Calculate stdev from the items.
39 :param items: Stdev is calculated from these items.
46 variance = [(x - avg) ** 2 for x in items]
47 stddev = sqrt(mean(variance))
51 def relative_change(nr1, nr2):
52 """Compute relative change of two values.
54 :param nr1: The first number.
55 :param nr2: The second number.
58 :returns: Relative change of nr1.
62 return float(((nr2 - nr1) / nr1) * 100)
65 def remove_outliers(input_data, outlier_const):
68 :param input_data: Data from which the outliers will be removed.
69 :param outlier_const: Outlier constant.
70 :type input_data: list
71 :type outlier_const: float
72 :returns: The input list without outliers.
76 data = np.array(input_data)
77 upper_quartile = np.percentile(data, 75)
78 lower_quartile = np.percentile(data, 25)
79 iqr = (upper_quartile - lower_quartile) * outlier_const
80 quartile_set = (lower_quartile - iqr, upper_quartile + iqr)
82 for y in data.tolist():
83 if quartile_set[0] <= y <= quartile_set[1]:
92 def get_files(path, extension=None, full_path=True):
93 """Generates the list of files to process.
95 :param path: Path to files.
96 :param extension: Extension of files to process. If it is the empty string,
97 all files will be processed.
98 :param full_path: If True, the files with full path are generated.
101 :type full_path: bool
102 :returns: List of files to process.
107 for root, _, files in walk(path):
108 for filename in files:
110 if filename.endswith(extension):
112 file_list.append(join(root, filename))
114 file_list.append(filename)
116 file_list.append(join(root, filename))
121 def get_rst_title_char(level):
122 """Return character used for the given title level in rst files.
124 :param level: Level of the title.
126 :returns: Character used for the given title level in rst files.
129 chars = ('=', '-', '`', "'", '.', '~', '*', '+', '^')
130 if level < len(chars):