o
    8Va4                     @   s  d Z ddlmZ ddlZddlZddlZddlZddlm	Z	m
Z
 ddlmZ ddlmZ G dd deZd*d
dZd+ddZdd Zeedd d ZeejddZg adadd Zdd Zeddddd,ddZd-d d!Zd"d# Z d$d% Z!d.d&d'Z"d(d) Z#dS )/z:Miscellaneous stuff that doesn't really fit anywhere else.    )ListN)filldedent)as_int)
deprecatedc                   @   s   e Zd ZdS )UndecidableN)__name__
__module____qualname__ r   r   6/usr/lib/python3/dist-packages/sympy/utilities/misc.pyr      s    r   F   c                 C   s   dt tt| d|d S )a`  
    Strips leading and trailing empty lines from a copy of `s`, then dedents,
    fills and returns it.

    Empty line stripping serves to deal with docstrings like this one that
    start with a newline after the initial triple quote, inserting an empty
    line at the beginning of the string.

    See Also
    ========
    strlines, rawlines
    
)width)r   r   strstrip)swr   r   r   
filldedent   s   r   @   Fc                 C   s   t | tur
tdd| v rt| S t| drdnd}|fd }d| v r3d| }d| }|d	8 }nd
| }d| }|d8 }g }| rT|| d|  | |d } | sC|rft|dkrf||d   d S ||	| S )aX  Return a cut-and-pastable string that, when printed, is
    equivalent to the input.  The lines will be surrounded by
    parentheses and no line will be longer than c (default 64)
    characters. If the line contains newlines characters, the
    `rawlines` result will be returned.  If ``short`` is True
    (default is False) then if there is one line it will be
    returned without bounding parentheses.

    Examples
    ========

    >>> from sympy.utilities.misc import strlines
    >>> q = 'this is a long string that should be broken into shorter lines'
    >>> print(strlines(q, 40))
    (
    'this is a long string that should be b'
    'roken into shorter lines'
    )
    >>> q == (
    ... 'this is a long string that should be b'
    ... 'roken into shorter lines'
    ... )
    True

    See Also
    ========
    filldedent, rawlines
    zexpecting string inputr   "'   \z(
r%s%%s%s
)z%s
r%s   z(
%s%%s%s
)z%s
%sN   r   )
typer   
ValueErrorrawlinesrepr
startswithappendlen
splitlinesjoin)r   cshortqmjoutr   r   r   strlines$   s*   

r+   c                 C   s   |  d}t|dkrt|d S d| v d| v g}tdd |D s*d| v s*t|r^g }| d}t|d }t|D ]\}}||ksE|rO|t|d  q;|t| q;d	d
| S d
|}|d rkd| S d| S )ax  Return a cut-and-pastable string that, when printed, is equivalent
    to the input. Use this when there is more than one line in the
    string. The string returned is formatted so it can be indented
    nicely within tests; in some cases it is wrapped in the dedent
    function which has to be imported from textwrap.

    Examples
    ========

    Note: because there are characters in the examples below that need
    to be escaped because they are themselves within a triple quoted
    docstring, expressions below look more complicated than they would
    be if they were printed in an interpreter window.

    >>> from sympy.utilities.misc import rawlines
    >>> from sympy import TableForm
    >>> s = str(TableForm([[1, 10]], headings=(None, ['a', 'bee'])))
    >>> print(rawlines(s))
    (
        'a bee\n'
        '-----\n'
        '1 10 '
    )
    >>> print(rawlines('''this
    ... that'''))
    dedent('''\
        this
        that''')

    >>> print(rawlines('''this
    ... that
    ... '''))
    dedent('''\
        this
        that
        ''')

    >>> s = """this
    ... is a triple '''
    ... """
    >>> print(rawlines(s))
    dedent("""\
        this
        is a triple '''
        """)

    >>> print(rawlines('''this
    ... that
    ...     '''))
    (
        'this\n'
        'that\n'
        '    '
    )

    See Also
    ========
    filldedent, strlines
    r   r   r   z'''z"""c                 s   s    | ]}| d V  qdS ) N)endswith).0lir   r   r   	<genexpr>   s    zrawlines.<locals>.<genexpr>r   z
(
    %s
)z
    zdedent("""\
    %s""")zdedent('''\
    %s'''))	splitr"   r   anyallr-   	enumerater!   r$   )r   linesZtriplervZtrailingZlastir/   r   r   r   r   X   s"   
<"

r   P   z-bithash_randomizationc                    s.   ddl m} |s
 S dd  fdd}|S )zIf SYMPY_DEBUG is True, it will print a nice execution tree with
    arguments and results of all decorated functions, else do nothing.
    r   SYMPY_DEBUGc                 _   s|   t }g a td7 add }| |i |}td8 ad| j||f }t g kr)||t 7 }|a t | tdkr<tt d  g a |S )Nr   c                 S   s\   ddd}t | dkrdS g }| d d D ]	}||| q||| d d d|S )	Nr   c                 S   sX   |  d}d|d  }|dd  D ]}|dkrq|dkr#|d| 7 }q|d| 7 }q|S )Nr   z+-%s
r   r    z| %s
z  %s
)r1   )r   r   xrar   r   r   indent   s   
z?debug_decorator.<locals>.maketree.<locals>.tree.<locals>.indentr   r=   r   )r   )r"   r!   r$   )ZsubtreesrA   fr@   r   r   r   tree   s   

z/debug_decorator.<locals>.maketree.<locals>.treez
%s%s = %s
r   )
_debug_tmp_debug_iterr   r!   print)rC   argskwZoldtmprD   r?   r   r   r   r   maketree   s   
z!debug_decorator.<locals>.maketreec                     s    g| R i |S Nr   )rH   kwargsfuncrJ   r   r   	decorated   s   z"debug_decorator.<locals>.decorated)sympyr<   )rN   r<   rO   r   rM   r   debug_decorator   s   /rQ   c                  G   s(   ddl m} |rt| dtji dS dS )zB
    Print ``*args`` if SYMPY_DEBUG is True, else do nothing.
    r   r;   fileN)rP   r<   rG   sysstderr)rH   r<   r   r   r   debug   s   rU   z%the builtin ``shutil.which`` functioniL  z1.7)Z
useinsteadZissueZdeprecated_since_versionc           
      C   s   |du r	t jd }|t j}dg}t jdkr&t j| \}}|s%| d } n tjdkrFt jd 	 t j}t j| \}}|	 |vrF|}|D ](}| | }t j
|rX|  S |D ]}t j||}	t j
|	ro|	    S qZqHdS )zTry to find 'executable' in the directories listed in 'path' (a
    string listing directories separated by 'os.pathsep'; defaults to
    os.environ['PATH']).  Returns the complete filename or None if not
    found
    NPATHr=   Zos2z.exewin32PATHEXT)osenvironr1   pathsepnamepathsplitextrS   platformlowerisfiler$   )

executabler]   pathsZextlistbaseextpathextZexecnameprC   r   r   r   find_executable   s2   



rh   c                 C   s   ddddddd}t | }t|dr#t|d	d
 d	d }nt|dr8t|d	d
 d	d }tt| d| d|}d|v rM|dd }|rU|||}|S )a  Return function name of `x` (if defined) else the `type(x)`.
    If short is True and there is a shorter alias for the result,
    return the alias.

    Examples
    ========

    >>> from sympy.utilities.misc import func_name
    >>> from sympy import Matrix
    >>> from sympy.abc import x
    >>> func_name(Matrix.eye(3))
    'MutableDenseMatrix'
    >>> func_name(x < 1)
    'StrictLessThan'
    >>> func_name(x < 1, short=True)
    'Lt'
    ZGeZGtZLeZLtZEqZNe)ZGreaterThanZStrictGreaterThanZLessThanZStrictLessThanZEqualityZ
Unequalityz<type 'r   r   r   z<class 'rN   r   .rB   )r   r   r    r1   getattrget)r>   r&   aliastypr6   r   r   r   	func_name   s$   rn   c                    sH   sdd S fdd t ddd  D t j fddS )aU  Return a function that can make the replacements, given in
    ``reps``, on a string. The replacements should be given as mapping.

    Examples
    ========

    >>> from sympy.utilities.misc import _replace
    >>> f = _replace(dict(foo='bar', d='t'))
    >>> f('food')
    'bart'
    >>> f = _replace({})
    >>> f('food')
    'food'
    c                 S   s   | S rK   r   )r>   r   r   r   <lambda>W  s    z_replace.<locals>.<lambda>c                    s    |  d S )Nr   )group)matchrepsr   r   ro   X  s    |c                 S   s   g | ]	\}}t |qS r   )_reescape)r.   kvr   r   r   
<listcomp>Z  s    z_replace.<locals>.<listcomp>c                    s     | S rK   )sub)string)Dpatternr   r   ro   [  s    )ru   compiler$   itemsMrr   r   )r|   r}   rs   r   _replaceG  s   r   c                 G   sD   t |dkr|d }t|tu r|}n	| j| S t|}t|| S )ac  Return ``string`` with all keys in ``reps`` replaced with
    their corresponding values, longer strings first, irrespective
    of the order they are given.  ``reps`` may be passed as tuples
    or a single mapping.

    Examples
    ========

    >>> from sympy.utilities.misc import replace
    >>> replace('foo', {'oo': 'ar', 'f': 'b'})
    'bar'
    >>> replace("spamham sha", ("spam", "eggs"), ("sha","md5"))
    'eggsham md5'

    There is no guarantee that a unique answer will be
    obtained if keys in a mapping overlap (i.e. are the same
    length and have some identical sequence at the
    beginning/end):

    >>> reps = [
    ...     ('ab', 'x'),
    ...     ('bc', 'y')]
    >>> replace('abc', *reps) in ('xc', 'ay')
    True

    References
    ==========

    .. [1] https://stackoverflow.com/questions/6116978/python-replace-multiple-strings
    r   r   )r"   r   dictreplacer   )r{   rs   Zkvr   r   r   r   ^  s   
r   c           	      C   s  i }|du r|durt d| |du r| S |}d }}nMt|tu r^i }t| D ]}t|dkrBt|| dkrB||||< q+|}|}|rYdd tt|  D \}}nd }}nt|t|krjt d|rxt	
dd|}| |} t| |} t	
||}| |S )a  Return ``s`` where characters have been replaced or deleted.

    SYNTAX
    ======

    translate(s, None, deletechars):
        all characters in ``deletechars`` are deleted
    translate(s, map [,deletechars]):
        all characters in ``deletechars`` (if provided) are deleted
        then the replacements defined by map are made; if the keys
        of map are strings then the longer ones are handled first.
        Multicharacter deletions should have a value of ''.
    translate(s, oldchars, newchars, deletechars)
        all characters in ``deletechars`` are deleted
        then each character in ``oldchars`` is replaced with the
        corresponding character in ``newchars``

    Examples
    ========

    >>> from sympy.utilities.misc import translate
    >>> abc = 'abc'
    >>> translate(abc, None, 'a')
    'bc'
    >>> translate(abc, {'a': 'x'}, 'c')
    'xb'
    >>> translate(abc, {'abc': 'x', 'a': 'y'})
    'x'

    >>> translate('abcd', 'ac', 'AC', 'd')
    'AbC'

    There is no guarantee that a unique answer will be
    obtained if keys in a mapping overlap are the same
    length and have some identical sequences at the
    beginning/end:

    >>> translate(abc, {'ab': 'x', 'bc': 'y'}) in ('xc', 'ay')
    True
    Nz6c should be None when a=None is passed, instead got %sr=   r   c                 S   s   g | ]}d  |qS )r=   )r$   )r.   r7   r   r   r   ry     s    ztranslate.<locals>.<listcomp>z,oldchars and newchars have different lengths)r   r   r   listkeysr"   popzipr   r   	maketrans	translater   )	r   r@   br%   mrr&   rw   valnr   r   r   r     s6   *
 



r   c                 C   sx   t | }t|d }d|  krdkrn nd}n|d dkr"d}n|d dkr+d	}n|d d
kr4d}nd}t|| S )z=Return ordinal number string of num, e.g. 1 becomes 1st.
    d         th
   r   str   Zndr   Zrd)r   absr   )Znumr   rw   suffixr   r   r   ordinal  s   r   )r   )r   FrK   )F)NN)$__doc__typingr   rS   rY   reru   structtextwrapr   r   Zsympy.core.compatibilityr   Zsympy.core.decoratorsr   r   r   r   r+   r   r   ZcalcsizeZARCHrj   flagsZHASH_RANDOMIZATIONrE   rF   rQ   rU   rh   rn   r   r   r   r   r   r   r   r   <module>   s<    

4R>	
"'
*I