U
    ç¥¡c_  ã                   @   sV   d Z ddlZddlZddlmZ G dd„ deƒZedkrReƒ Ze 	d¡ e
ejƒ dS )z$
Memory usage profiler for Python.

é    N)Úmuppyc                   @   s2   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ ZdS )Ú	MProfilera¤  A memory usage profiler class.

    Memory data for each function is stored as a 3-element list in the
    dictionary self.memories. The index is always a codepoint (see below).
    The following are the definitions of the members:

    [0] = The number of times this function was called
    [1] = Minimum memory consumption when this function was measured.
    [2] = Maximum memory consumption when this function was measured.

    A codepoint is a list of 3-tuple of the type
    (filename, functionname, linenumber). You can omit either element, which
    will cause the profiling to be triggered if any of the other criteria
    match. E.g.
    - (None, foo, None), will profile any foo function,
    - (bar, foo, None) will profile only the foo function from the bar file,
    - (bar, foo, 17) will profile only line 17 of the foo function defined
      in the file bar.

    Additionally, you can define on what events you want the profiling be
    triggered. Possible events are defined in
    http://docs.python.org/lib/debugger-hooks.html.

    If you do not define either codepoints or events, the profiler will
    record the memory usage in at every codepoint and event.

    Nc                 C   s   i | _ || _|| _dS )zÎ
        keyword arguments:
        codepoints -- a list of points in code to monitor (defaults to all
            codepoints)
        events -- a list of events to monitor (defaults to all events)
        N)ÚmemoriesÚ
codepointsÚevents)Úselfr   r   © r   ú4/tmp/pip-unpacked-wheel-8ad_c8mj/pympler/mprofile.pyÚ__init__(   s    zMProfiler.__init__c                 C   sb   | j dkrdS | j D ]H}d}tt|ƒƒD ](}|| dk	r(|| || kr(d} qRq(|s dS qdS )z9Check if codepoint matches any of the defined codepoints.NTF)r   ÚrangeÚlen)r   Ú	codepointÚcpZmismatchÚir   r   r	   Úcodepoint_included3   s    

zMProfiler.codepoint_includedc                 C   sò   | j dks|| j krît |¡}|d |d |d f}|  |¡rît ¡ }t |¡}|| jkr˜ddddg| j|< d| j| d< || j| d< || j| d< nV| j| d  d7  < | j| d |krÎ|| j| d< | j| d |k rî|| j| d< dS )z@Profiling method used to profile matching codepoints and events.Nr   é   é   )r   ÚinspectÚgetframeinfor   r   Zget_objectsÚget_sizer   )r   ÚframeÚeventÚargZ
frame_infor   ZobjectsÚsizer   r   r	   ÚprofileA   s     



zMProfiler.profilec              	   C   s*   t  | j¡ zt|ƒ W 5 t  d ¡ X | S )N)ÚsysÚ
setprofiler   Úexec)r   Úcmdr   r   r	   ÚrunU   s
    zMProfiler.run)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r   r   r   r   r   r   r	   r      s
   
r   Ú__main__zprint('hello'))r#   r   r   Zpymplerr   Úobjectr   r    Úpr   Úprintr   r   r   r   r	   Ú<module>   s   S
