
    wdX                     h    d Z ddlmZmZmZ ddlZddlZ G d de          Zd Z	d Z
d Zd	 Zd
 ZdS )z
Utility functions that are unrelated to datashape

Do not import datashape modules into this module.  See util.py in that case
    )print_functiondivisionabsolute_importNc                   "    e Zd ZdZdZd Zd ZdS )IndexCallablez Provide getitem syntax for functions

    >>> def inc(x):
    ...     return x + 1

    >>> I = IndexCallable(inc)
    >>> I[3]
    4
    fnc                     || _         d S Nr   )selfr	   s     8lib/python3.11/site-packages/datashape/internal_utils.py__init__zIndexCallable.__init__   s        c                 ,    |                      |          S r   r   )r   keys     r   __getitem__zIndexCallable.__getitem__   s    wws||r   N)__name__
__module____qualname____doc__	__slots__r   r    r   r   r   r      sC          I      r   r   c                 *     t           fd|          S )Nc                      |            S r   r   )x	predicates    r   <lambda>zremove.<locals>.<lambda>!   s    		!, r   )filter)r   seqs   ` r   remover        s    ,,,,c222r   c                 |    i }| D ]6}| |         D ]+}|                     |t                                |fz   ||<   ,7|S )a  Reverses direction of dependence dict

    >>> d = {'a': (1, 2), 'b': (2, 3), 'c':()}
    >>> reverse_dict(d)  # doctest: +SKIP
    {1: ('a',), 2: ('a', 'b'), 3: ('b',)}

    :note: dict order are not deterministic. As we iterate on the
        input dict, it make the output of this function depend on the
        dict order. So this function output order should be considered
        as undeterministic.

    )gettuple)dresultr   vals       r   reverse_dictr'   &   sY     F = =S6 	= 	=C **S%''22cW<F3KK	=Mr   c                    t          |           t          d                                 D                       t          fd| D                       }g }|r|                                }|                    |           |                     |d          D ]F}||         v sJ |                             |           |         s|                    |           G|t          fd| D                       rt          d          |S )a   Topological sort algorithm by Kahn [1] - O(nodes + vertices)

    inputs:
        edges - a dict of the form {a: {b, c}} where b and c depend on a
    outputs:
        L - an ordered list of nodes that satisfy the dependencies of edges

    >>> _toposort({1: (2, 3), 2: (3, )})
    [1, 2, 3]

    Closely follows the wikipedia page [2]

    [1] Kahn, Arthur B. (1962), "Topological sorting of large networks",
    Communications of the ACM
    [2] http://en.wikipedia.org/wiki/Toposort#Algorithms
    c              3   >   K   | ]\  }}|t          |          fV  d S r   )set).0kr&   s      r   	<genexpr>z_toposort.<locals>.<genexpr>N   s0      MMFAs1c#hh-MMMMMMr   c              3   $   K   | ]
}|v|V  d S r   r   r+   vincoming_edgess     r   r-   z_toposort.<locals>.<genexpr>O   s-      991.!89Q999999r   r   c              3   B   K   | ]}                     |          V  d S r   )r"   r/   s     r   r-   z_toposort.<locals>.<genexpr>Z   s1      
0
0Q>a  
0
0
0
0
0
0r   zInput has cycles)r'   dictitemsr*   popappendr"   r    addany
ValueError)edgesSLnmr1   s        @r   	_toposortr?   <   s3   " "%((NMMn6J6J6L6LMMMMMN9999999::A
A
 EEGG	1b!! 	 	Aq)))))1$$Q'''!!$ a   
0
0
0
0%
0
0
000 -+,,,Hr   c                     t                      }|D ]=} | |          }||vrt                      ||<   ||                             |           >|S )a   Group a collection by a key function

    >>> names = ['Alice', 'Bob', 'Charlie', 'Dan', 'Edith', 'Frank']
    >>> groupby(len, names) # doctest: +SKIP
    {3: ['Bob', 'Dan'], 5: ['Alice', 'Edith', 'Frank'], 7: ['Charlie']}

    >>> iseven = lambda x: x % 2 == 0
    >>> groupby(iseven, [1, 2, 3, 4, 5, 6, 7, 8])
    {False: [1, 3, 5, 7], True: [2, 4, 6, 8]}

    See Also:
        ``countby``
    )r3   listr6   )funcr   r$   itemr   s        r   groupbyrD   a   s`     	A  d4jja< 	VVAcF	#dHr   c                 X    t          j        |           pt          j        d|           d uS )Nz^[_a-zA-Z][_a-zA-Z0-9]*$)keyword	iskeywordrematch)ss    r   isidentifierrK   y   s1    a   AH0!44D@Br   )r   
__future__r   r   r   rF   rH   objectr   r    r'   r?   rD   rK   r   r   r   <module>rN      s     A @ @ @ @ @ @ @ @ @  				    F   &3 3 3  ,     J  0B B B B Br   