Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
蜕变的菜鸟
glances
提交
13fbad5b
G
glances
项目概览
蜕变的菜鸟
/
glances
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
glances
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
13fbad5b
编写于
5月 28, 2016
作者:
N
nicolargo
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add new attribute class. Realy...
上级
396f3acf
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
143 addition
and
0 deletion
+143
-0
glances/attribute.py
glances/attribute.py
+143
-0
未找到文件。
glances/attribute.py
0 → 100644
浏览文件 @
13fbad5b
# -*- coding: utf-8 -*-
#
# This file is part of Glances.
#
# Copyright (C) 2015 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
time
import
time
class
GlancesAttribute
(
object
):
def
__init__
(
self
,
name
,
description
=
''
,
history_max_size
=
None
,
is_rate
=
False
):
"""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)
is_rate: If True then the value is manage like a rate (store timestamp in the history)
"""
self
.
_name
=
name
self
.
_description
=
description
self
.
_value
=
None
self
.
_history_max_size
=
history_max_size
self
.
_history
=
[]
self
.
is_rate
=
is_rate
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
.
is_rate
:
if
self
.
history_len
()
>
0
:
return
(
self
.
_value
[
1
]
-
self
.
history_value
()[
1
])
/
(
self
.
_value
[
0
]
-
self
.
history_value
()[
0
])
else
:
return
None
else
:
return
self
.
_value
@
value
.
setter
def
value
(
self
,
new_value
):
"""Set a value.
If self.is_rate is True, store a tuple with (<timestamp>, value)
else, store directly the value (wathever type is it)
"""
if
self
.
is_rate
:
new_value
=
(
time
(),
new_value
)
if
self
.
_value
is
not
None
:
self
.
history_add
(
self
.
_value
)
self
.
_value
=
new_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_mean
(
self
,
nb
=
5
):
"""Return the mean on the <nb> values in the history.
"""
if
self
.
is_rate
:
h_sum
=
map
(
sum
,
zip
(
*
self
.
_history
[
-
nb
:]))
return
h_sum
[
1
]
/
float
(
self
.
_history
[
-
1
][
0
]
-
self
.
_history
[
-
nb
][
0
])
else
:
return
sum
(
self
.
_history
[
-
nb
:])
/
float
(
nb
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录