/usr/lib/python3/dist-packages/glances/attribute.py is in glances 2.7.1.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | # -*- coding: utf-8 -*-
#
# This file is part of Glances.
#
# Copyright (C) 2016 Nicolargo <nicolas@nicolargo.com>
#
# Glances is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Glances is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Attribute class."""
from datetime import datetime
class GlancesAttribute(object):
def __init__(self, name, description='', history_max_size=None):
"""Init the attribute
name: Attribute name (string)
description: Attribute human reading description (string)
history_max_size: Maximum size of the history list (default is no limit)
History is stored as a list for tuple: [(date, value), ...]
"""
self._name = name
self._description = description
self._value = None
self._history_max_size = history_max_size
self._history = []
def __repr__(self):
return self.value
def __str__(self):
return str(self.value)
"""
Properties for the attribute name
"""
@property
def name(self):
return self._name
@name.setter
def name(self, new_name):
self._name = new_name
"""
Properties for the attribute description
"""
@property
def description(self):
return self._description
@description.setter
def description(self, new_description):
self._description = new_description
"""
Properties for the attribute value
"""
@property
def value(self):
if self.history_len() > 0:
return (self._value[1] - self.history_value()[1]) / (self._value[0] - self.history_value()[0])
else:
return None
@value.setter
def value(self, new_value):
"""Set a value.
Value is a tuple: (<timestamp>, <new_value>)
"""
self._value = (datetime.now(), new_value)
self.history_add(self._value)
"""
Properties for the attribute history
"""
@property
def history(self):
return self._history
@history.setter
def history(self, new_history):
self._history = new_history
@history.deleter
def history(self):
del self._history
def history_reset(self):
self._history = []
def history_add(self, value):
"""Add a value in the history
"""
if self._history_max_size is None or self.history_len() < self._history_max_size:
self._history.append(value)
else:
self._history = self._history[1:] + [value]
def history_size(self):
"""Return the history size (maximum nuber of value in the history)
"""
return len(self._history)
def history_len(self):
"""Return the current history lenght
"""
return len(self._history)
def history_value(self, pos=1):
"""Return the value in position pos in the history.
Default is to return the latest value added to the history.
"""
return self._history[-pos]
def history_json(self, nb=0):
"""Return the history of last nb items (0 for all) In ISO JSON format"""
return [(i[0].isoformat(), i[1]) for i in self._history[-nb:]]
def history_mean(self, nb=5):
"""Return the mean on the <nb> values in the history.
"""
d, v = zip(*self._history)
return sum(v[-nb:]) / float(v[-1] - v[-nb])
|