f423cd22a79ac7ee0de6b12624d48f814c62e8e9
[csit.git] / resources / tools / presentation / utils.py
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:
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 """General purpose utilities.
15 """
16
17 from os import walk
18 from os.path import join
19 from math import sqrt
20
21
22 def mean(items):
23     """Calculate mean value from the items.
24
25     :param items: Mean value is calculated from these items.
26     :type items: list
27     :returns: MEan value.
28     :rtype: float
29     """
30
31     return float(sum(items)) / len(items)
32
33
34 def stdev(items):
35     """Calculate stdev from the items.
36
37     :param items: Stdev is calculated from these items.
38     :type items: list
39     :returns: Stdev.
40     :rtype: float
41     """
42
43     avg = mean(items)
44     variance = [(x - avg) ** 2 for x in items]
45     stddev = sqrt(mean(variance))
46     return stddev
47
48
49 def relative_change(nr1, nr2):
50     """Compute relative change of two values.
51
52     :param nr1: The first number.
53     :param nr2: The second number.
54     :type nr1: float
55     :type nr2: float
56     :returns: Relative change of nr1.
57     :rtype: float
58     """
59
60     return (nr1 - nr2) / nr2 * 100
61
62
63 def get_files(path, extension=None, full_path=True):
64     """Generates the list of files to process.
65
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.
70     :type path: str
71     :type extension: str
72     :type full_path: bool
73     :returns: List of files to process.
74     :rtype: list
75     """
76
77     file_list = list()
78     for root, _, files in walk(path):
79         for filename in files:
80             if extension:
81                 if filename.endswith(extension):
82                     if full_path:
83                         file_list.append(join(root, filename))
84                     else:
85                         file_list.append(filename)
86             else:
87                 file_list.append(join(root, filename))
88
89     return file_list
90
91
92 def get_rst_title_char(level):
93     """Return character used for the given title level in rst files.
94
95     :param level: Level of the title.
96     :type: int
97     :returns: Character used for the given title level in rst files.
98     :rtype: str
99     """
100     chars = ('=', '-', '`', "'", '.', '~', '*', '+', '^')
101     if level < len(chars):
102         return chars[level]
103     else:
104         return chars[-1]