Test Failed
Push — develop ( abf64f...1d1151 )
by Nicolas
02:59
created

glances/attribute.py (20 issues)

1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of Glances.
4
#
5
# Copyright (C) 2019 Nicolargo <[email protected]>
6
#
7
# Glances is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU Lesser General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Glances is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU Lesser General Public License for more details.
16
#
17
# You should have received a copy of the GNU Lesser General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20
"""Attribute class."""
21
22
from datetime import datetime
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
23
24
25
class GlancesAttribute(object):
0 ignored issues
show
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
26
27
    def __init__(self, name, description='', history_max_size=None):
28
        """Init the attribute
29
        name: Attribute name (string)
30
        description: Attribute human reading description (string)
31
        history_max_size: Maximum size of the history list (default is no limit)
32
33
        History is stored as a list for tuple: [(date, value), ...]
34
        """
35
        self._name = name
36
        self._description = description
37
        self._value = None
38
        self._history_max_size = history_max_size
39
        self._history = []
40
41
    def __repr__(self):
42
        return self.value
43
44
    def __str__(self):
45
        return str(self.value)
46
47
    """
48
    Properties for the attribute name
49
    """
0 ignored issues
show
This string statement has no effect and could be removed.
Loading history...
50
    @property
51
    def name(self):
0 ignored issues
show
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
52
        return self._name
53
54
    @name.setter
55
    def name(self, new_name):
56
        self._name = new_name
57
58
    """
59
    Properties for the attribute description
60
    """
0 ignored issues
show
This string statement has no effect and could be removed.
Loading history...
61
    @property
62
    def description(self):
0 ignored issues
show
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
63
        return self._description
64
65
    @description.setter
66
    def description(self, new_description):
67
        self._description = new_description
68
69
    """
70
    Properties for the attribute value
71
    """
0 ignored issues
show
This string statement has no effect and could be removed.
Loading history...
72
    @property
73
    def value(self):
0 ignored issues
show
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
74
        if self.history_len() > 0:
0 ignored issues
show
Unnecessary "else" after "return"
Loading history...
75
            return (self._value[1] - self.history_value()[1]) / (self._value[0] - self.history_value()[0])
0 ignored issues
show
This line is too long as per the coding-style (106/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
division w/o __future__ statement
Loading history...
76
        else:
77
            return None
78
79
    @value.setter
80
    def value(self, new_value):
81
        """Set a value.
82
        Value is a tuple: (<timestamp>, <new_value>)
83
        """
84
        self._value = (datetime.now(), new_value)
85
        self.history_add(self._value)
86
87
    """
88
    Properties for the attribute history
89
    """
0 ignored issues
show
This string statement has no effect and could be removed.
Loading history...
90
    @property
91
    def history(self):
0 ignored issues
show
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
92
        return self._history
93
94
    @history.setter
95
    def history(self, new_history):
96
        self._history = new_history
97
98
    @history.deleter
99
    def history(self):
100
        del self._history
101
102
    def history_reset(self):
0 ignored issues
show
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
103
        self._history = []
104
105
    def history_add(self, value):
106
        """Add a value in the history
107
        """
108
        if self._history_max_size is None or self.history_len() < self._history_max_size:
0 ignored issues
show
This line is too long as per the coding-style (89/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
109
            self._history.append(value)
110
        else:
111
            self._history = self._history[1:] + [value]
112
113
    def history_size(self):
114
        """Return the history size (maximum nuber of value in the history)
115
        """
116
        return len(self._history)
117
118
    def history_len(self):
119
        """Return the current history lenght
120
        """
121
        return len(self._history)
122
123
    def history_value(self, pos=1):
124
        """Return the value in position pos in the history.
125
        Default is to return the latest value added to the history.
126
        """
127
        return self._history[-pos]
128
129
    def history_raw(self, nb=0):
0 ignored issues
show
Coding Style Naming introduced by
The name nb does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
130
        """Return the history in ISO JSON format"""
131
        return self._history[-nb:]
132
133
    def history_json(self, nb=0):
0 ignored issues
show
Coding Style Naming introduced by
The name nb does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
134
        """Return the history in ISO JSON format"""
135
        return [(i[0].isoformat(), i[1]) for i in self._history[-nb:]]
136
137
    def history_mean(self, nb=5):
0 ignored issues
show
Coding Style Naming introduced by
The name nb does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
138
        """Return the mean on the <nb> values in the history.
139
        """
140
        _, v = zip(*self._history)
0 ignored issues
show
Coding Style Naming introduced by
The name v does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
141
        return sum(v[-nb:]) / float(v[-1] - v[-nb])
0 ignored issues
show
division w/o __future__ statement
Loading history...
142