� ��g�'��8�dZddlmZGd�d��Zdd�ZdS)z3Tools for manipulation of expressions using paths. �)�Basicc�H�eZdZdZdZd�Zd�Zd�Zd�Zd�Z d�Z d d �Z d �Z d S) �EPatha3 Manipulate expressions using paths. EPath grammar in EBNF notation:: literal ::= /[A-Za-z_][A-Za-z_0-9]*/ number ::= /-?\d+/ type ::= literal attribute ::= literal "?" all ::= "*" slice ::= "[" number? (":" number? (":" number?)?)? "]" range ::= all | slice query ::= (type | attribute) ("|" (type | attribute))* selector ::= range | query range? path ::= "/" selector ("/" selector)* See the docstring of the epath() function. )�_path�_epathc��t|t��r|S|std���|}|ddkr |dd�}ntd���g}|�d��D�]�}|���}|std���d}|D] }|���s|dvr|dz }� g}g}|r�|d|�} ||d�}| �d ��D]o} | ���} | std ���| �d ��r|�| dd ����Z|�| ���pd} |d krn�|� d��r� |� d��} n#t$rtd���wxYw|d| �g} } d| vrt| ��} n\| �dd��D]<}|s| �d���| �t|�����=t| �} || dzd�}|rtd���|�||| f�����t�|��}||_||_|S)zConstruct new EPath. z empty EPathr�/�Nznon-root EPathzempty selector)�_�|�?r z empty elementr ������*�[�]zexpected ']', got EOL�:�ztrailing characters in selector)� isinstancer� ValueError�NotImplementedError�split�strip�isalnum�endswith�append� startswith�index�int�slice�object�__new__rr)�cls�pathr�epath�selectorr�c�attrs�types�elements�element�span�i�_span�elt�objs �i/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/sympy/simplify/epathtools.pyr!z EPath.__new__s��� �d�E� "� "� ��K�� ,��]�+�+� +��� ��7�c�>�>�����8�D�D�%�&6�7�7� 7���� � �3���= /�= /�H��~�~�'�'�H�� 3� �!1�2�2�2��E�� � ���9�9�;�;��!��"6�"6��Q�J�E�E���E��E�� .�#�F�U�F�+��#�E�F�F�+��'�~�~�c�2�2� .� .�G�%�m�m�o�o�G�"�:�(��9�9�9��'�'��,�,�.�� � �W�S�b�S�\�2�2�2�2�� � �W�-�-�-�-��D��3�����&�&�s�+�+�0�B�$�N�N�3�/�/����%�B�B�B�(�)@�A�A�A�B����#+�1�Q�3�-��4�E��%�'�'�"�5�z�z���#(�;�;�s�A�#6�#6�6�6�C�#&�6� $� � �D� 1� 1� 1� 1� $� � �C��H�H� 5� 5� 5� 5�$�d�|��'��A����/�H��H�$�%F�G�G�G� �L�L�%���-� .� .� .� .��n�n�S�!�!���� ��� �� s �3F � F#c�0�|jj�d|j�d�S)N�(�))� __class__�__name__r)�selfs r0�__repr__zEPath.__repr__ts���>�2�2�2�D�J�J�J�?�?�c�|�|jr|���S|jr|���S|jS)z)Sort ``expr.args`` using printing order. )�is_Add�as_ordered_terms�is_Mul�as_ordered_factors�args)r6�exprs r0�_get_ordered_argszEPath._get_ordered_argswsB�� �;� ��(�(�*�*� *� �[� ��*�*�,�,� ,��9� r8c�6�|D]}t||��sdS�dS)z(Check if ``expr`` has any of ``attrs``. FT)�hasattr)r6r?r'�attrs r0� _hasattrszEPath._hasattrs�s6��� � �D��4��&�&� ��u�u� ��tr8c��d�|j���D��}tt|���|����S)z'Check if ``expr`` is any of ``types``. c��g|] }|j�� S�)r5)�.0r"s r0� <listcomp>z#EPath._hastypes.<locals>.<listcomp>�s��A�A�A�C�3�<�A�A�Ar8)r4�mro�bool�set� intersection)r6r?r(�_typess r0� _hastypeszEPath._hastypes�sE��A�A�4�>�+=�+=�+?�+?�A�A�A���C��K�K�,�,�U�3�3�4�4�4r8c�z�|s|sdS|r|�||��rdS|r|�||��rdSdS)z3Apply ``_hasattrs`` and ``_hastypes`` to ``expr``. TF)rDrO)r6r?r'r(s r0�_hasz EPath._has�s_��� �� ��4� � �T�^�^�D�%�0�0� ��4� � �T�^�^�D�%�0�0� ��4��ur8Nc�\��������fd��|pd|pic�����fd�}��j||��S)aj Modify parts of an expression selected by a path. Examples ======== >>> from sympy.simplify.epathtools import EPath >>> from sympy import sin, cos, E >>> from sympy.abc import x, y, z, t >>> path = EPath("/*/[0]/Symbol") >>> expr = [((x, 1), 2), ((3, y), z)] >>> path.apply(expr, lambda expr: expr**2) [((x**2, 1), 2), ((3, y**2), z)] >>> path = EPath("/*/*/Symbol") >>> expr = t + sin(x + 1) + cos(x + y + E) >>> path.apply(expr, lambda expr: 2*expr) t + sin(2*x + 1) + cos(2*x + 2*y + E) c���|s ||��S|d|dd�}}|\}}}t|t��r!|js� �|��d}}n|St |d��r|d}}n|St |��}|�Ct|t ��r*t|�t|�����} n |g} ntt|����} | D]C} || } n#t$rY�wxYw� � | ||��r� || |��|| <�D|r |j |�S|� |��S)Nrr T�__iter__F)rr�is_Atomr@rB�listr�range�indices�len� IndexErrorrQ�funcr4)r#r?r[r%r'r(r+r>�basicrXr,�arg�_applyr6s ��r0r^zEPath.apply.<locals>._apply�s�����& 0��t�D�z�z�!�!%�a��$�q�r�r�(�$��%-�"��u�d��d�E�*�*� ��<�$�&*�&<�&<�T�&B�&B�D�e���#� ��T�:�.�.� �"&��%�D�D��K��D�z�z���#�!�$��.�.�)�"'����c�$�i�i�)@�)@�"A���#'�&���#�C��I�I�.�.�G� �:�:�A�!�"�1�g����%�!�!�!� ��!�����y�y��e�U�3�3�:�"(�&��s�D�"9�"9��Q����0�$�4�9�d�+�+��>�>�$�/�/�/s�)C2�2 C?�>C?rGc����|g��Ri���S)NrG)r?�_args�_kwargsr[s ���r0�<lambda>zEPath.apply.<locals>.<lambda>�s!���T�T�$�:��:�:�:�'�:�:�r8�r) r6r?r[r>�kwargs�_funcr^r`ras ` ` @@@r0�applyz EPath.apply�se�������0' 0�' 0�' 0�' 0�' 0�' 0�R���V�\�r���w�:�:�:�:�:�:���v�d�k�4��/�/�/r8c�@����g����fd����j|���S)a  Retrieve parts of an expression selected by a path. Examples ======== >>> from sympy.simplify.epathtools import EPath >>> from sympy import sin, cos, E >>> from sympy.abc import x, y, z, t >>> path = EPath("/*/[0]/Symbol") >>> expr = [((x, 1), 2), ((3, y), z)] >>> path.select(expr) [x, y] >>> path = EPath("/*/*/Symbol") >>> expr = t + sin(x + 1) + cos(x + y + E) >>> path.select(expr) [x, x, y] c���|s� �|��dS|d|dd�}}|\}}}t|t��r� �|��}nt |d��r|}ndS|�:t|t ��r ||}n ||g}n#t $rYdSwxYw|D]%}� �|||��r �||���&dS)Nrr rT)rrrr@rBrrZrQ) r#r?r%r'r(r+r>r]�_select�resultr6s ���r0rizEPath.select.<locals>._select�s&���� +�� � �d�#�#�#�#�#�!%�a��$�q�r�r�(�$��%-�"��u�d��d�E�*�*���1�1�$�7�7�D�D��T�:�.�.���D�D��F��#�!�$��.�.�#�#�D�z���#�$(��J�<�D�D��)�#�#�#�"�F�F�#���� �+�+�C��y�y��e�U�3�3�+����c�*�*�*��+�+s� B� B,�+B,rc)r6r?rirjs` @@r0�selectz EPath.select�sJ�����0�� +� +� +� +� +� +� +�6 ��� �T�"�"�"�� r8)NN) r5� __module__� __qualname__�__doc__� __slots__r!r7r@rDrOrQrfrkrGr8r0rrs���������($�I�U�U�U�n@�@�@�������5�5�5� � � �D0�D0�D0�D0�L6�6�6�6�6r8rNc��t|��}|�|S|�|�|��S|�||||��S)a  Manipulate parts of an expression selected by a path. Explanation =========== This function allows to manipulate large nested expressions in single line of code, utilizing techniques to those applied in XML processing standards (e.g. XPath). If ``func`` is ``None``, :func:`epath` retrieves elements selected by the ``path``. Otherwise it applies ``func`` to each matching element. Note that it is more efficient to create an EPath object and use the select and apply methods of that object, since this will compile the path string only once. This function should only be used as a convenient shortcut for interactive use. This is the supported syntax: * select all: ``/*`` Equivalent of ``for arg in args:``. * select slice: ``/[0]`` or ``/[1:5]`` or ``/[1:5:2]`` Supports standard Python's slice syntax. * select by type: ``/list`` or ``/list|tuple`` Emulates ``isinstance()``. * select by attribute: ``/__iter__?`` Emulates ``hasattr()``. Parameters ========== path : str | EPath A path as a string or a compiled EPath. expr : Basic | iterable An expression or a container of expressions. func : callable (optional) A callable that will be applied to matching parts. args : tuple (optional) Additional positional arguments to ``func``. kwargs : dict (optional) Additional keyword arguments to ``func``. Examples ======== >>> from sympy.simplify.epathtools import epath >>> from sympy import sin, cos, E >>> from sympy.abc import x, y, z, t >>> path = "/*/[0]/Symbol" >>> expr = [((x, 1), 2), ((3, y), z)] >>> epath(path, expr) [x, y] >>> epath(path, expr, lambda expr: expr**2) [((x**2, 1), 2), ((3, y**2), z)] >>> path = "/*/*/Symbol" >>> expr = t + sin(x + 1) + cos(x + y + E) >>> epath(path, expr) [x, x, y] >>> epath(path, expr, lambda expr: 2*expr) t + sin(2*x + 1) + cos(2*x + 2*y + E) )rrkrf)r#r?r[r>rdrs r0r$r$sI��H�4�[�[�F� �|�� � �|��}�}�T�"�"�"��|�|�D�$��f�5�5�5r8)NNNN)rn� sympy.corerrr$rGr8r0�<module>rrsr��9�9�������P�P�P�P�P�P�P�P�fK6�K6�K6�K6�K6�K6r8
Memory