Format: application/x-python-code
⬇ Download File (9.7 KB)
   1  o
   2  
   3  ��h�2�@spdZddlZddlZddlZddlZddlmZddl	m
   4  Z
   5  mZddlm
   6  Z
   7  Gdd�de�ZGdd	�d	e�ZdS)
   8  zaDisplayhook for IPython.
   9  
  10  This defines a callable class that IPython uses for `sys.displayhook`.
  11  �N)�Configurable)�Instance�Float)�warncs�eZdZdZeddd�Zeddd�Zed�Zd+�fd	d
  12  �	Z	e
  13  dd��Zd
  14  d�Zdd�Z
  15  edd��Zdd�Zdd�Zdd�ZdZd,d-dd�Zdd�Zdd �Zd!d"�Zd#d$�Zd,d%d&�Zd'd(�Zd)d*�Z�ZS).�DisplayHookz�The custom IPython displayhook to replace sys.displayhook.
  16  
  17      This class does many things, but the basic idea is that it is a callable
  18      that gets called anytime user code returns a value.
  19      z1IPython.core.interactiveshell.InteractiveShellABCT)�
  20  allow_nonez-IPython.core.interactiveshell.ExecutionResultg�������?N��cs�tt|�jd	d|i|��d}|dkrd|_d}n||kr+d|_d}td|dd�nd|_||_||_d\|_|_|_	|j|j|j	d�}|jj
  21  �|�dS)
  22  N�shell�rz6caching was disabled (min value for cache size is %s).)�
  23  stacklevel���rr��_�__�___�)�superr�__init__�
  24  do_full_cacher�
  25  cache_sizer	rrr�user_ns�update)�selfr	r�kwargsZcache_size_minZ
  26  to_user_ns��	__class__r�Nc:\Users\shubh\Videos\agent\nova\Lib\site-packages\IPython/core/displayhook.pyr$s&��zDisplayHook.__init__cCs|jjS�N)r	�execution_count�rrrr�prompt_count=szDisplayHook.prompt_countcCsPdtjvr&z|jjd}||jurWdS|jjd=WdSty%YdSwdS)z3Check if the user has set the '_' variable by hand.rN)�builtin_mod�__dict__r	rr�KeyError)rZ
  27  user_valuerrr�check_for_underscoreFs
  28  
  29  ��z DisplayHook.check_for_underscorecCs2z	|jjjd}Wn
  30  tyYdSw|�|�S)z2Should we silence the display hook because of ';'?�����F)r	�history_managerZinput_hist_parsed�
  31  IndexError�semicolon_at_end_of_expression)r�cellrrr�quietTs�
  32  zDisplayHook.quietcCsnt�|�}tt�|j��}t|�D]#}|dtjtjtj	tj
  33  fvr"q|dtjkr2|ddkr2dSdSdS)z=Parse Python expression and detects whether last token is ';'rr�;TFN)�_io�StringIO�list�tokenize�generate_tokens�readline�reversed�	ENDMARKER�NL�NEWLINE�COMMENT�OP)Z
  34  expression�sio�tokens�tokenrrrr*`s
  35  �z*DisplayHook.semicolon_at_end_of_expressioncCsdS)z.Start the displayhook, initializing resources.Nrr!rrr�start_displayhookoszDisplayHook.start_displayhookcCs8tj�|jj�d�|jj�}|jrtj�|�dSdS)zyWrite the output prompt.
  36  
  37          The default implementation simply writes the prompt to
  38          ``sys.stdout``.
  39          z	Out[{}]: N)�sys�stdout�writer	Zseparate_out�formatr r)rZ	outpromptrrr�write_output_promptss
  40  �zDisplayHook.write_output_promptcCs|jj�|�S)a�Compute format data of the object to be displayed.
  41  
  42          The format data is a generalization of the :func:`repr` of an object.
  43          In the default implementation the format data is a :class:`dict` of
  44          key value pair where the keys are valid MIME types and the values
  45          are JSON'able data structure containing the raw data for that MIME
  46          type. It is up to frontends to determine pick a MIME to to use and
  47          display that data in an appropriate manner.
  48  
  49          This method only computes the format data for the object and should
  50          NOT actually print or write that to a stream.
  51  
  52          Parameters
  53          ----------
  54          result : object
  55              The Python object passed to the display hook, whose format will be
  56              computed.
  57  
  58          Returns
  59          -------
  60          (format_dict, md_dict) : dict
  61              format_dict is a :class:`dict` whose keys are valid MIME types and values are
  62              JSON'able raw data for that MIME type. It is recommended that
  63              all return values of this should always include the "text/plain"
  64              MIME type representation of the object.
  65              md_dict is a :class:`dict` with the same MIME type keys
  66              of metadata associated with each output.
  67  
  68          )r	�display_formatterrA�r�resultrrr�compute_format_dataszDisplayHook.compute_format_dataF�returncCsld|vrdS|d}d|vr|jsd|}zt|�WdSty5t|�tjjd��tjj��YdSw)aWrite the format data dict to the frontend.
  69  
  70          This default version of this method simply writes the plain text
  71          representation of the object to ``sys.stdout``. Subclasses should
  72          override this method to send the entire `format_dict` to the
  73          frontends.
  74  
  75          Parameters
  76          ----------
  77          format_dict : dict
  78              The format dict for the object passed to `sys.displayhook`.
  79          md_dict : dict (optional)
  80              The metadata dict to be associated with the display data.
  81          �
  82  text/plainN�
  83  �backslashreplace)�prompt_end_newline�print�UnicodeEncodeError�encoder>r?�encoding�decode)r�format_dict�md_dictZresult_reprrrr�write_format_data�s$�zDisplayHook.write_format_datacCs|jr�||jjdur�t|jjd�|jkr|jr|��d}dd�tdd�D�D]}||jjvr2q)t||�|jj�|�ur@d}q)|j	|_
  84  |j|_	||_dtj
  85  vrb|rb|jj|j|j	|j
  86  d	�dd
  87  �i}|jr�d|j}|||<|jj|dd
  88  �||jjd|j<dSdSdSdS)
  89  z7Update user_ns with various things like _, __, _1, etc.�_ohTcSsg|]}d|�qS)rr)�.0�irrr�
  90  <listcomp>�sz.DisplayHook.update_user_ns.<locals>.<listcomp>r�Frr)�interactivez_%sN)rr	r�lenr�
  91  cull_cache�range�getattr�getrrrr#r$�pushr")rrEZ
  92  update_undersZundersZto_mainZ
  93  new_resultrrr�update_user_ns�s8�
  94  ��
  95  �zDisplayHook.update_user_nscCs|jdur||j_dSdSr)�exec_resultrErDrrr�fill_exec_result�s
  96  �zDisplayHook.fill_exec_resultcCsBd|vrdS|jjjr|jj�|dd�|d|jjj|j<dS)zLog the output.rHN�output)r	�logger�
  97  log_outputZ	log_writer(Zoutput_hist_reprsr")rrQrrrre�s
  98  �zDisplayHook.log_outputcCstj�|jj�tj��dS)z%Finish up all displayhook activities.N)r>r?r@r	Z
  99  separate_out2�flushr!rrr�finish_displayhook�szDisplayHook.finish_displayhookcCsx|��|dur8|��s:|��|��|�|�\}}|�|�|�|�|r2|�||�|�|�|�	�dSdSdS)z�Printing with history cache management.
 100  
 101          This is invoked every time the interpreter needs to print, and is
 102          activated by setting the variable sys.displayhook to it.
 103          N)
 104  r&r,r=rBrFr`rbrSrerg�rrErQrRrrr�__call__s
 105  
 106  
 107  �zDisplayHook.__call__cCs�|jj�di�}t|�}tt||j�d�}tdj||d��t	t
 108  |��D]\}}||kr0dS|jj�d|d�|�|d�q%dS)z-Output cache is full, cull the oldest entriesrT�zVOutput cache limit (currently {sz} entries) hit.
 109  Flushing oldest {cull_count} entries.)�sz�
 110  cull_countz_%iN)r	rr^rZ�max�int�
 111  cull_fractionrrA�	enumerate�sorted�pop)r�ohrkrlrV�nrrrr[s��zDisplayHook.cull_cachec	Cs�|jstd��td|jd�D])}dt|�}z|jj|=Wn	ty'Ynwz|jj|=Wqty8Yqw|jj�	dd�}|durI|�
 112  �d\|_|_|_
 113  dtjvrd|jj�|j|j|j
 114  d��ddl}tjdkrs|��dSdS)	NzJYou shouldn't have reached the cache flush if full caching is not enabled!rrrTr
 115  rr�cli)r�
 116  ValueErrorr\r"�reprr	Zuser_ns_hiddenr%rr^�clearrrrr#r$r�gcr>�platformZcollect)rrt�keyrsryrrrrf#s0��
 117  
 118  �zDisplayHook.flush)Nrr)rGN)�__name__�
 119  __module__�__qualname__�__doc__rr	rarror�propertyr"r&r,�staticmethodr*r=rBrFrKrSr`rbrergrir[rf�
 120  __classcell__rrrrrs8��
 121  
 122  !)$
 123  
 124  rc@s eZdZddd�Zddd�ZdS)�CapturingDisplayHookNcCs||_|dur	g}||_dSr)r	�outputs)rr	r�rrrrFs
 125  zCapturingDisplayHook.__init__cCs4|durdS|jj�|�\}}|j�||d��dS)N)�data�metadata)r	rCrAr��appendrhrrrriLszCapturingDisplayHook.__call__r)r|r}r~rrirrrrr�Es
 126  r�)r�builtinsr#r>�ior.r1Ztraitlets.config.configurablerZ	traitletsrr�warningsrr�objectr�rrrr�<module>s0