
\c           @  s   d  Z  d d l m Z m Z d d l m Z d d l m Z d d l	 m
 Z
 d Z d Z d Z e e d d	  Z d
   Z d   Z d   Z d g  e e d  Z d   Z d e d d d e e e d d d g  d  Z d S(   s+   Tools for setting up interactive sessions. i(   t   print_functiont   division(   t   LooseVersion(   t   import_module(   t   init_printings   from __future__ import division
from sympy import *
x, y, z, t = symbols('x y z t')
k, m, n = symbols('k m n', integer=True)
f, g, h = symbols('f g h', cls=Function)
init_printing()
sj   These commands were executed:
%(source)s
Documentation can be found at https://docs.sympy.org/%(version)s
s   Couldn't locate IPython. Having IPython installed is greatly recommended.
See http://ipython.scipy.org for more details. If you use Debian/Ubuntu,
just install the 'ipython' package and start isympy again.
c         C  s  d d l  m } d d l m } d d l m } d d l  m } d d l } d d l } | rb d Sd | j	 d	  }	 |  r d
 }
 n d }
 d | g } | j
 d  } | d k	 r | j   d k r | j d  n  | r | j d  n  |
 | |	 | d j |  f } d | } | d k r%t } n  d } x@ | j d  d  D]+ } | sX| d 7} q?| d | d 7} q?W| } d | k rd } n
 d | } | d t i | d 6| d 67} | S(   s,   Create a banner for an interactive session. i(   t   __version__(   t   GROUND_TYPES(   t   ARCH(   t   SYMPY_DEBUGNt    s   %d.%d.%di   t   IPythont   Pythons   ground types: %st   SYMPY_USE_CACHEt   nos
   cache: offs   debugging: ons   , s,   %s console for SymPy %s (Python %s-%s) (%s)
s   
s   >>> t   devs   %s/t   sourcet   version(   t   sympyR   t   sympy.polys.domainsR   t   sympy.utilities.miscR   R   t   syst   ost   version_infot   getenvt   Nonet   lowert   appendt   joint   preexec_sourcet   splitt   verbose_message(   t   ipythont   quietR   t   sympy_versionR   R   R   R   R   t   python_versiont
   shell_namet   infot   cachet   argst   messaget   _sourcet   linet   doc_version(    (    s8   lib/python2.7/site-packages/sympy/interactive/session.pyt   _make_message    sD    	
		
c         C  s   d d l  m } m } m } m } m } d d l m } d   } g  } | | |   j  }	 x| |	 D]t \ }
 } } } } |
 | k r | |  r | j	 | d f | d f | | f | d f g  qc | j
 |
 | f  qc W| |  S(   s  
    Wrap integer literals with Integer.

    This is based on the decistmt example from
    http://docs.python.org/library/tokenize.html.

    Only integer literals are converted.  Float literals are left alone.
    Examples
    ========

    >>> from __future__ import division
    >>> from sympy.interactive.session import int_to_Integer
    >>> from sympy import Integer
    >>> s = '1.2 + 1/2 - 0x12 + a1'
    >>> int_to_Integer(s)
    '1.2 +Integer (1 )/Integer (2 )-Integer (0x12 )+a1 '
    >>> s = 'print (1/2)'
    >>> int_to_Integer(s)
    'print (Integer (1 )/Integer (2 ))'
    >>> exec(s)
    0.5
    >>> exec(int_to_Integer(s))
    1/2
    i(   t   generate_tokenst
   untokenizet   NUMBERt   NAMEt   OP(   t   StringIOc         S  s8   d |  k s0 d |  j    k s0 d |  j    k r4 t St S(   s]   
        Returns true if string value num (with token NUMBER) represents an integer.
        t   .t   jt   e(   R   t   Falset   True(   t   num(    (    s8   lib/python2.7/site-packages/sympy/interactive/session.pyt   _is_intt   s    0t   Integert   (t   )(   t   tokenizeR,   R-   R.   R/   R0   t   sympy.core.compatibilityR1   t   readlinet   extendR   (   t   sR,   R-   R.   R/   R0   R1   R8   t   resultt   gt   toknumt   tokvalt   _(    (    s8   lib/python2.7/site-packages/sympy/interactive/session.pyt   int_to_IntegerX   s    (					c           s4   d d l    |  j     f d   } | |  _ d S(   sM   
    Allow IPython to automatically convert integer literals to Integer.
    iNc           sE   y   j  |   Wn t k
 r$ n Xt |   }   |  | |  d  S(   N(   t   parset   SyntaxErrorRF   (   t   cellR&   t   kwargs(   t   astt   old_run_cell(    s8   lib/python2.7/site-packages/sympy/interactive/session.pyt   my_run_cell   s    (   RK   t   run_cell(   t   shellRM   (    (   RK   RL   s8   lib/python2.7/site-packages/sympy/interactive/session.pyt"   enable_automatic_int_sympification   s    	c           sD   d d l  } | j d    d   f d  } |  j t f |  d S(   s>   Allow IPython to automatially create symbols (``isympy -a``). iNs8   name '(?P<symbol>[A-Za-z_][A-Za-z0-9_]*)' is not definedc           s  | t  k r | j r | j j r   j t |   } | d k	 r |  j d i | j d  d 6d t zJ y |  j d d } Wn t	 t
 f k
 r n X|  j | d t d SWd |  j d | j d  d t Xq n  |  j j | | | d | } |  j | | |  d S(	   sJ   Handle :exc:`NameError` exception and allow injection of missing symbols. s!   %(symbol)s = Symbol('%(symbol)s')t   symbolt   store_historyt   IniNs   del %st	   tb_offset(   t	   NameErrort   tb_nextt   matcht   strR   RN   t   groupR5   t   user_nst   KeyErrort
   IndexErrort   InteractiveTBt   structured_tracebackt   _showtraceback(   t   selft   etypet   valuet   tbRT   RW   t   codet   stb(   t   re_nameerror(    s8   lib/python2.7/site-packages/sympy/interactive/session.pyt   _handler   s"    "	 	(   t   ret   compileR   t   set_custom_excRU   (   RO   Rh   Rg   (    (   Rf   s8   lib/python2.7/site-packages/sympy/interactive/session.pyt   enable_automatic_symbols   s
    '	c         C  s   d d l  } t | j  d k r |  s t | j  d k rO d d l m } n d d l m } | j   } t | _ | j	 |  | j
 }  n  | r t |   n  | r t |   n  |  Sd d l m } | |  Sd S(   s   Construct new IPython session. iNs   0.11s   1.0(   t   ipapp(   t   make_IPython(   R
   t   VR   t   IPython.terminalRl   t   IPython.frontend.terminalt   TerminalIPythonAppR5   t   display_bannert
   initializeRO   Rk   RP   t   IPython.ShellRm   (   RO   t   argvt   auto_symbolst   auto_int_to_IntegerR
   Rl   t   appRm   (    (    s8   lib/python2.7/site-packages/sympy/interactive/session.pyt   init_ipython_session   s"    	c            s3   d d l  m   d   f   f d     Y}  |    S(   s   Construct new Python session. i(   t   InteractiveConsolet   SymPyConsolec             s   e  Z d  Z   f d   Z RS(   s.   An interactive console with readline support. c           s     j  |   y d d  l } Wn t k
 r0 n Xd d  l } d d  l } | j d  t | d  r | j j d  } y | j	 |  Wn t
 k
 r n X| j | j |  n  d  S(   Nis   tab: completet   read_history_files   ~/.sympy-history(   t   __init__R>   t   ImportErrorR   t   atexitt   parse_and_bindt   hasattrt   patht
   expanduserR|   t   IOErrort   registert   write_history_file(   R`   R>   R   R   t   history(   Rz   (    s8   lib/python2.7/site-packages/sympy/interactive/session.pyR}     s    (   t   __name__t
   __module__t   __doc__R}   (    (   Rz   (    s8   lib/python2.7/site-packages/sympy/interactive/session.pyR{     s   (   Rd   Rz   (   R{   (    (   Rz   s8   lib/python2.7/site-packages/sympy/interactive/session.pyt   init_python_session  s    c           sy  d d l  } t } |  t k	 r y d d l } Wn2 t k
 rb |  t k rY t d   n  d   n8 Xy d d l m } |     Wn t k
 r d   n Xt    } |  d k r | }  q n  |  t k r t	       j
 } n t   d | d | d |   t | j  d k rRd	   f d
    _ y   j d t  WqRt k
 rNqRXn  | sd  j } n  | r|  st | j  d k  rt d   n  | r|  st | j  d k  rt d   n  t }   j | d d	 t d | d | d | d | d   d | d |	 d |
  t |  | |  } | sOt |  |   | j d  n& t |  d d l } | j d    d S(   s  
    Initialize an embedded IPython or Python session. The IPython session is
    initiated with the --pylab option, without the numpy imports, so that
    matplotlib plotting can be interactive.

    Parameters
    ==========

    pretty_print: boolean
        If True, use pretty_print to stringify;
        if False, use sstrrepr to stringify.
    order: string or None
        There are a few different settings for this parameter:
        lex (default), which is lexographic order;
        grlex, which is graded lexographic order;
        grevlex, which is reversed graded lexographic order;
        old, which is used for compatibility reasons and for long expressions;
        None, which sets it to lex.
    use_unicode: boolean or None
        If True, use unicode characters;
        if False, do not use unicode characters.
    use_latex: boolean or None
        If True, use latex rendering if IPython GUI's;
        if False, do not use latex rendering.
    quiet: boolean
        If True, init_session will not print messages regarding its status;
        if False, init_session will print messages regarding its status.
    auto_symbols: boolean
        If True, IPython will automatically create symbols for you.
        If False, it will not.
        The default is False.
    auto_int_to_Integer: boolean
        If True, IPython will automatically wrap int literals with Integer, so
        that things like 1/2 give Rational(1, 2).
        If False, it will not.
        The default is False.
    ipython: boolean or None
        If True, printing will initialize for an IPython console;
        if False, printing will initialize for a normal console;
        The default is None, which automatically determines whether we are in
        an ipython instance or not.
    str_printer: function, optional, default=None
        A custom string printer function. This should mimic
        sympy.printing.sstrrepr().
    pretty_printer: function, optional, default=None
        A custom pretty printer. This should mimic sympy.printing.pretty().
    latex_printer: function, optional, default=None
        A custom LaTeX printer. This should mimic sympy.printing.latex()
        This should mimic sympy.printing.latex().
    argv: list of arguments for IPython
        See sympy.bin.isympy for options that can be used to initialize IPython.

    See Also
    ========

    sympy.interactive.printing.init_printing: for examples and the rest of the parameters.


    Examples
    ========

    >>> from sympy import init_session, Symbol, sin, sqrt
    >>> sin(x) #doctest: +SKIP
    NameError: name 'x' is not defined
    >>> init_session() #doctest: +SKIP
    >>> sin(x) #doctest: +SKIP
    sin(x)
    >>> sqrt(5) #doctest: +SKIP
      ___
    \/ 5
    >>> init_session(pretty_print=False) #doctest: +SKIP
    >>> sqrt(5) #doctest: +SKIP
    sqrt(5)
    >>> y + x + y**2 + x**2 #doctest: +SKIP
    x**2 + x + y**2 + y
    >>> init_session(order='grlex') #doctest: +SKIP
    >>> y + x + y**2 + x**2 #doctest: +SKIP
    x**2 + y**2 + x + y
    >>> init_session(order='grevlex') #doctest: +SKIP
    >>> y * x**2 + x * y**2 #doctest: +SKIP
    x**2*y + x*y**2
    >>> init_session(order='old') #doctest: +SKIP
    >>> x**2 + y**2 + x + y #doctest: +SKIP
    x + y + x**2 + y**2
    >>> theta = Symbol('theta') #doctest: +SKIP
    >>> theta #doctest: +SKIP
    theta
    >>> init_session(use_unicode=True) #doctest: +SKIP
    >>> theta # doctest: +SKIP
    \u03b8
    iNs'   IPython is not available on this system(   t   get_ipythonRu   Rv   Rw   s   0.11t   execc           s     j  |  t  S(   N(   RN   R5   (   t   srcRQ   (   t   ip(    s8   lib/python2.7/site-packages/sympy/interactive/session.pyt   <lambda>  R	   t
   import_allsK   automatic construction of symbols is possible only in IPython 0.11 or abovesQ   automatic int to Integer transformation is possible only in IPython 0.11 or aboveRQ   t   pretty_printt   ordert   use_unicodet	   use_latexR   t   str_printert   pretty_printert   latex_printers   Exiting ...c           S  s
   t  d  S(   Ns   Exiting ...
(   t   print(    (    (    s8   lib/python2.7/site-packages/sympy/interactive/session.pyR     R	   (   R   R5   R
   R~   R6   t   RuntimeErrorR   R   t   boolR   t   interactRy   Rn   R   t	   runsourcet   enable_pylabt	   Exceptiont   mainloopR   R   R+   R   t   exitR   R   (   R   R   R   R   R   R    Rv   Rw   R   R   R   Ru   R   t
   in_ipythonR
   R   R   t   _preexec_sourceR'   R   (    (   R   s8   lib/python2.7/site-packages/sympy/interactive/session.pyt   init_session-  s`    _

		""

N(   R   t
   __future__R    R   t   distutils.versionR   Rn   t   sympy.externalR   t   sympy.interactive.printingR   R   R   t
   no_ipythonR6   R5   R   R+   RF   RP   Rk   Ry   R   R   (    (    (    s8   lib/python2.7/site-packages/sympy/interactive/session.pyt   <module>   s"   	8	4		G!	!		