U
    祡cH)                     @   s   d Z ddlZddlZddlZddlmZ ddlmZ i add Ze  dd Z	d	d
 Z
d ddZd!ddZd"ddZedZedZd#ddZdd Zdd Zdd ZdS )$a  A collection of functions to summarize object information.

This module provides several function which will help you to analyze object
information which was gathered. Often it is sufficient to work with aggregated
data instead of handling the entire set of existing objects. For example can a
memory leak identified simple based on the number and size of existing objects.

A summary contains information about objects in a table-like manner.
Technically, it is a list of lists. Each of these lists represents a row,
whereas the first column reflects the object type, the second column the number
of objects, and the third column the size of all these objects. This allows a
simple table-like output like the	following:

=============  ============  =============
       types     # objects     total size
=============  ============  =============
<type 'dict'>             2            560
 <type 'str'>             3            126
 <type 'int'>             4             96
<type 'long'>             2             66
<type 'list'>             1             40
=============  ============  =============

Another advantage of summaries is that they influence the system you analyze
only to a minimum. Working with references to existing objects will keep these
objects alive. Most of the times this is no desired behavior (as it will have
an impact on the observations). Using summaries reduces this effect greatly.

output representation
---------------------

The output representation of types is defined in summary.representations.
Every type defined in this dictionary will be represented as specified. Each
definition has a list of different representations. The later a representation
appears in this list, the higher its verbosity level. From types which are not
defined in summary.representations the default str() representation will be
used.

Per default, summaries will use the verbosity level 1 for any encountered type.
The reason is that several computations are done with summaries and rows have
to remain comparable. Therefore information which reflect an objects state,
e.g. the current line number of a frame, should not be included. You may add
more detailed information at higher verbosity levels than 1.
    N)stringutils)	getsizeofc            	      C   s   t jdk rLdd g} | ttj< dd g}|ttj< dd dd g}|ttj< dd dd d	d g}|ttj< d
d dd g}|tt< dd dd g}|ttj	< dd dd g}|tt
< dd g}|ttj< dd dd g}|tt< d S )Ni  c                 S   s   dt |  S )Nzclassobj(%s))repr)c r   3/tmp/pip-unpacked-wheel-8ad_c8mj/pympler/summary.py<lambda><       z'_init_representations.<locals>.<lambda>c                 S   s   dt | j S )Nzinstance(%s))r   	__class__fr   r   r   r   @   r	   c                 S   s   dt | j S )Nzinstancemethod (%s))r   im_funcir   r   r   r   D   r	   c                 S   s   dt | jt | jf S )Nzinstancemethod (%s, %s))r   Zim_classr   r   r   r   r   r   E   s   
c                 S   s   d| j j S )Nzframe (codename: %s))f_codeco_namer   r   r   r   r   J   r	   c                 S   s   d| j j| j jf S )Nz"frame (codename: %s, codeline: %s))r   r   co_firstlinenor   r   r   r   r   K   s   c                 S   s   d| j j| j j| j jf S )Nz0frame (codename: %s, filename: %s, codeline: %s))r   r   co_filenamer   r   r   r   r   r   M   s   c                 S   s   t t| S Nstrtypedr   r   r   r   S   r	   c                 S   s   dt |  S )Nzdict, len=%slenr   r   r   r   r   T   r	   c                 S   s
   d| j  S )Nzfunction (%s))__name__r   r   r   r   r   X   r	   c                 S   s   d| j | jf S )Nzfunction (%s.%s))
__module__r   r   r   r   r   r   Y   r	   c                 S   s   t t| S r   r   lr   r   r   r   ]   r	   c                 S   s   dt |  S )Nzlist, len=%sr   r   r   r   r   r   ^   r	   c              
   S   s    dt | dt | ddt|   S )Nz
module(%s)r   __file__znameless, id: %d)getattrid)mr   r   r   r   a   s     c                 S   s   t t| S r   r   sr   r   r   r   f   r	   c                 S   s   dt |  S )Nzset, len=%sr   r$   r   r   r   r   g   r	   )sys
hexversionrepresentationstypesZ	ClassTypeZInstanceType
MethodType	FrameTypedictFunctionTypelist
ModuleTypeset)	ZclassobjinstanceZinstancemethodframe_dictfunctionZ_listmoduleZ_setr   r   r   _init_representations8   sD    







r6   c                 C   s   i }i }| D ]N}t |}||krF||  d7  < ||  t|7  < qd||< t|||< qg }|D ]}|||| || g qd|S )zSummarize an objects list.

    Return a list of lists, whereas each row consists of::
      [str(type), number of objects of this type, total size of these objects].

    No guarantee regarding the order is given.

       _reprr   append)ZobjectscountZ
total_sizeootyperowsr   r   r   	summarizeo   s    	r?   c           	      C   s   g }t dd |D }t dd | D }t|}|| |D ]~}||}||}|r|r|||d |d  |d |d  g q>|r|| q>|r>|||d  |d  g q>q>q>|S )a  Get the difference of two summaries.

    Subtracts the values of the right summary from the values of the left
    summary.
    If similar rows appear on both sides, the are included in the summary with
    0 for number of elements and total size.
    If the number of elements of a row of the diff is 0, but the total size is
    not, it means that objects likely have changed, but not there number, thus
    resulting in a changed size.

    c                 s   s   | ]}|d  |fV  qdS r   Nr   .0rr   r   r   	<genexpr>   s     zget_diff.<locals>.<genexpr>c                 s   s   | ]}|d  |fV  qdS r@   r   rA   r   r   r   rD      s     r7      )r,   r0   updategetr:   )	leftrightresZright_by_keyZleft_by_keykeyskeyrC   r   r   r   r   get_diff   s     


*rM      size
descendingc                    s$  g }| D ]}| t| qdddg kr>tdt ddg}||kr^tdt|  dkr|dkr|jd	d
 d q|dkr|jdd
 dd n@|dkr|j fdd
d n |dkr|j fdd
dd |d| }|D ]}t|d |d< q|ddddg t	|S )zFormat the rows as a summary.

    Keyword arguments:
    limit -- the maximum number of elements to be listed
    sort  -- sort elements by 'size', 'type', or '#'
    order -- sort 'ascending' or 'descending'
    r   #rO   zinvalid sort, should be one ofZ	ascendingrP   zinvalid order, should be one ofr   c                 S   s   t | d S Nr   r9   xr   r   r   r      r	   zformat_.<locals>.<lambda>)rL   c                 S   s   t | d S rR   rS   rT   r   r   r   r      r	   T)rL   reversec                    s   |    S r   indexrT   sortZsortbyr   r   r      r	   c                    s   |    S r   rW   rT   rY   r   r   r      r	   rE   r)   z	# objectsz
total size)
r:   r.   
ValueErrorr   rX   rZ   r   ppinsert_format_table)r>   limitrZ   orderZ	localrowsrowZordersr   rY   r   format_   s.    
rb   Tc                 #   s   d d}ddt jt jt jd  t|  }fdd|D }| fdd|D }| D ]2}|fd	dt||D V  |r`|V  d
}q`dS )zFormat a list of lists as a pretty table.

    Keyword arguments:
    header -- if True the first row is treated as a table header

    inspired by http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/267662
    =z | r7   rI   )rH   centerrI   c                    s"   g | ]}t  fd d|D qS )c                    s    g | ]}t t|d    qS )rE   )r   r   )rB   itempaddingr   r   
<listcomp>   s     z,_format_table.<locals>.<listcomp>.<listcomp>)max)rB   colrf   r   r   rh      s   z!_format_table.<locals>.<listcomp>c                    s   g | ]}|  qS r   r   )rB   w)borderr   r   rh      s     c                    s   g | ]\}} t ||qS r   )r   )rB   re   width)justifyr   r   rh      s   FN)r   ljustrd   rjustlowerzipjoin)r>   headerZvdelimcolsZ	colWidthsZ
borderlinera   r   )rl   rn   rg   r   r^      s,    

r^   c                 C   s"   t | |||dD ]}t| qdS )zPrint the rows as a summary.

    Keyword arguments:
    limit -- the maximum number of elements to be listed
    sort  -- sort elements by 'size', 'type', or '#'
    order -- sort 'ascending' or 'descending'

    )r_   rZ   r`   N)rb   print)r>   r_   rZ   r`   liner   r   r   print_   s    	rx   z^<(type|class) '(\S+)'>$z at 0x[0-9a-f]+r7   c                 C   sz   d}t | }|dks|tkr&t|}n8|d8 }tt| |krNtt| d }t| | | }td|}td|}|S )a/  Get meaning object representation.

    This function should be used when the simple str(o) output would result in
    too general data. E.g. "<type 'instance'" is less meaningful than
    "instance: Foo".

    Keyword arguments:
    verbosity -- if True the first row is treated as a table header

     r   r7   z\2)r   r(   r   r   addresssub	type_repr)r<   	verbosityrJ   tr   r   r   r9     s    
r9   c                 G   sB   || f|  | D ]*}||f|  |D ]}||f|  q(qdS )zTraverse all objects of a summary and call function with each as a
    parameter.

    Using this function, the following objects will be traversed:
    - the summary
    - each row
    - each item of a row
    Nr   )summaryr4   argsra   re   r   r   r   	_traverse  s
    	r   c                 C   s   d}t |dt|g}| D ]B}|d |d kr|d |d  |d |d   |d< |d< d}q|s| |d |d  |d  g | S )z:Remove object o from the summary by subtracting it's size.Fr7   r   rE   Tr8   )r   r<   foundra   rC   r   r   r   	_subtract/  s    * r   c                 C   s   dd | D S )zZRemove all rows in which the total size and the total number of
    objects is zero.

    c                 S   s(   g | ] }|d  dks |d dkr|qS )rE   r   r7   r   )rB   ra   r   r   r   rh   A  s       z_sweep.<locals>.<listcomp>r   )r   r   r   r   _sweep<  s    r   )rN   rO   rP   )T)rN   rO   rP   )r7   )__doc__rer&   r)   Zpympler.utilr   r   r(   r6   r?   rM   rb   r^   rx   compiler|   rz   r9   r   r   r   r   r   r   r   <module>   s&   -4"
&
!



