o
    Ìv›a–  ã                   @   s$   d Z ddlmZ G dd„ deƒZdS )zAttribute class.é    )Údatetimec                   @   sì   e Zd Zd)dd„Zdd„ Zdd„ Z	 ed	d
„ ƒZejdd
„ ƒZ	 edd„ ƒZ	e	jdd„ ƒZ		 edd„ ƒZ
e
jdd„ ƒZ
	 edd„ ƒZejdd„ ƒZejdd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zd*dd „Zd+d"d#„Zd+d$d%„Zd,d'd(„ZdS )-ÚGlancesAttributeÚ Nc                 C   s"   || _ || _d| _|| _g | _dS )a  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), ...]
        N)Ú_nameÚ_descriptionÚ_valueÚ_history_max_sizeÚ_history)ÚselfÚnameÚdescriptionZhistory_max_size© r   ú3/usr/lib/python3/dist-packages/glances/attribute.pyÚ__init__   s
   
zGlancesAttribute.__init__c                 C   ó   | j S ©N)Úvalue©r
   r   r   r   Ú__repr__(   s   zGlancesAttribute.__repr__c                 C   ó
   t | jƒS r   )Ústrr   r   r   r   r   Ú__str__+   ó   
zGlancesAttribute.__str__c                 C   r   r   ©r   r   r   r   r   r   1   ó   zGlancesAttribute.namec                 C   ó
   || _ d S r   r   )r
   Únew_namer   r   r   r   5   ó   
c                 C   r   r   ©r   r   r   r   r   r   <   r   zGlancesAttribute.descriptionc                 C   r   r   r   )r
   Znew_descriptionr   r   r   r   @   r   c                 C   s<   |   ¡ dkr| jd |  ¡ d  | jd |  ¡ d   S d S )Nr   é   )Úhistory_lenr   Úhistory_valuer   r   r   r   r   G   s   ,zGlancesAttribute.valuec                 C   s   t  ¡ |f| _|  | j¡ dS )zJSet a value.
        Value is a tuple: (<timestamp>, <new_value>)
        N)r   Znowr   Úhistory_add)r
   Z	new_valuer   r   r   r   N   s   c                 C   r   r   ©r	   r   r   r   r   ÚhistoryY   r   zGlancesAttribute.historyc                 C   r   r   r#   )r
   Znew_historyr   r   r   r$   ]   r   c                 C   s   | ` d S r   r#   r   r   r   r   r$   a   s   c                 C   s
   g | _ d S r   r#   r   r   r   r   Úhistory_resete   r   zGlancesAttribute.history_resetc                 C   s4   | j r|  ¡ | j kr| j d¡ | j |¡ dS dS )z#Add a value in the history
        r   N)r   r    r	   ÚpopÚappend)r
   r   r   r   r   r"   h   s
   ýzGlancesAttribute.history_addc                 C   r   )zHReturn the history size (maximum nuber of value in the history)
        ©Úlenr	   r   r   r   r   Úhistory_sizep   ó   
zGlancesAttribute.history_sizec                 C   r   )z*Return the current history lenght
        r(   r   r   r   r   r    u   r+   zGlancesAttribute.history_lenr   c                 C   s   | j |  S )z}Return the value in position pos in the history.
        Default is to return the latest value added to the history.
        r#   )r
   Úposr   r   r   r!   z   s   zGlancesAttribute.history_valuer   c                 C   s   | j | d… S )ú%Return the history in ISO JSON formatNr#   ©r
   Únbr   r   r   Úhistory_raw€   s   zGlancesAttribute.history_rawc                 C   s   dd„ | j | d… D ƒS )r-   c                 S   s    g | ]}|d    ¡ |d f‘qS )r   r   )Z	isoformat)Ú.0Úir   r   r   Ú
<listcomp>†   s     z1GlancesAttribute.history_json.<locals>.<listcomp>Nr#   r.   r   r   r   Úhistory_json„   s   zGlancesAttribute.history_jsoné   c                 C   s6   t | jŽ \}}t|| d… ƒt|d ||   ƒ S )z;Return the mean on the <nb> values in the history.
        Néÿÿÿÿ)Úzipr	   ÚsumÚfloat)r
   r/   Ú_Úvr   r   r   Úhistory_meanˆ   s   (zGlancesAttribute.history_mean)r   N)r   )r   )r5   )Ú__name__Ú
__module__Ú__qualname__r   r   r   Úpropertyr   Úsetterr   r   r$   Údeleterr%   r"   r*   r    r!   r0   r4   r<   r   r   r   r   r      sD    












r   N)Ú__doc__r   Úobjectr   r   r   r   r   Ú<module>   s   