
    HR-eP                     @    d Z ddlZddlZddlZddZddZd ZddZdS )	zA Collection of useful miscellaneous functions.

misc.py:
  Collection of useful miscellaneous functions.

:Author: Hannes Breytenbach (hannes@saao.ac.za)
    Nc                     t          j        d          }nfd}t          t          |t	          |                     |          }|r|d         n|S )zKfind the first index position for the which the callable pred returns True.N   c                 &     | d                   S )Nr    xpreds    5lib/python3.11/site-packages/astropy/io/ascii/misc.py<lambda>z"first_true_index.<locals>.<lambda>   s    ad     r   )operator
itemgetternextfilter	enumerate)iterabler	   defaultfunciis    `   r
   first_true_indexr      s\    |"1%%####	fT9X..//	9	9B#2a55G#r   c                 N    t           j        }nfd}t          | ||          S )zLfind the first index position for the which the callable pred returns False.Nc                      |            S Nr   r   s    r
   r   z#first_false_index.<locals>.<lambda>   s    TT!WW r   )r   not_r   )r   r	   r   r   s    `  r
   first_false_indexr      s1    |}$$$$HdG444r   c                     t          | d                   }t          |          s| S |                    d          |                    d          	rd nd d t          t          j        j                  st          d          t          t          j        j                  rfd	}nWt          t                    rd
 D             fd}n/t          d
                    t                                        t          t          t          |            |          }d|v r*|d                             d          rt          |          }t          t!          t           t          |                     S )aV  
    Sorts any number of lists according to:
    optionally given item sorting key function(s) and/or a global sorting key function.

    Parameters
    ----------
    One or more lists

    Keywords
    --------
    globalkey : None
        revert to sorting by key function
    globalkey : callable
        Sort by evaluated value for all items in the lists
        (call signature of this function needs to be such that it accepts an
        argument tuple of items from each list.
        eg.: ``globalkey = lambda *l: sum(l)`` will order all the lists by the
        sum of the items from each list

    if key: None
        sorting done by value of first input list
        (in this case the objects in the first iterable need the comparison
        methods __lt__ etc...)
    if key: callable
        sorting done by value of key(item) for items in first iterable
    if key: tuple
        sorting done by value of (key(item_0), ..., key(item_n)) for items in
        the first n iterables (where n is the length of the key tuple)
        i.e. the first callable is the primary sorting criterion, and the
        rest act as tie-breakers.

    Returns
    -------
    Sorted lists

    Examples
    --------
    Capture sorting indices::

        l = list('CharacterS')
        In [1]: sortmore( l, range(len(l)) )
        Out[1]: (['C', 'S', 'a', 'a', 'c', 'e', 'h', 'r', 'r', 't'],
                 [0, 9, 2, 4, 5, 7, 1, 3, 8, 6])
        In [2]: sortmore( l, range(len(l)), key=str.lower )
        Out[2]: (['a', 'a', 'C', 'c', 'e', 'h', 'r', 'r', 'S', 't'],
                 [2, 4, 0, 5, 7, 1, 3, 8, 9, 6])
    r   	globalkeykeyNc                     dS Nr   r   r   s    r
   r   zsortmore.<locals>.<lambda>\       A r   c                     | S r   r   r!   s    r
   r   zsortmore.<locals>.<lambda>_   r"   r   c                      dS r    r   r!   s    r
   r   zsortmore.<locals>.<lambda>a   s    q r   zglobalkey needs to be callablec                 0     |   | d                   fS r    r   r   r   r   s    r
   r   zsortmore.<locals>.<lambda>g   s    yy!}cc!A$ii0 r   c              3   $   K   | ]}|r|nd  V  dS )c                     dS r    r   r!   s    r
   r   z$sortmore.<locals>.<genexpr>.<lambda>i   s    Q r   Nr   ).0ks     r
   	<genexpr>zsortmore.<locals>.<genexpr>i   s-      441A&qq;;444444r   c                 `     |  ft          d t          |           D                       z   S )Nc              3   2   K   | ]\  }} ||          V  d S r   r   )r)   fzs      r
   r+   z-sortmore.<locals>.<lambda>.<locals>.<genexpr>j   s.      .N.NAqqtt.N.N.N.N.N.Nr   )tuplezipr&   s    r
   r   zsortmore.<locals>.<lambda>j   s4    yy!}&.N.N#c1++.N.N.N)N)NN r   zIkw arg 'key' should be None, callable, or a sequence of callables, not {})r   order)descendreverse)listlenget
isinstancecollectionsabcCallable
ValueErrorr0   KeyErrorformattypesortedr1   
startswithreversedmap)argskwfirstr*   resr   r   s        @@r
   sortmorerH   #   s   ` aMMEu:: {##I
&&--C
{ 	+CC +C L	i!9:: ;9:::#{/00 

00000	C		 
44444NNNNNW^^S		 
 
 	
 c4j!!q
)
)
)C"}}g;!!"899 	 3--CT39%%&&&r   c                       sd  t          |d i} fd}t          j        t          | |          }d |D             }|S )zMExtends the itertools.groupby functionality to arbitrary number of iterators.c                     | S r   r   r!   s    r
   r   zgroupmore.<locals>.<lambda>}   s     r   r   c                 &     | d                   S r    r   )r   r   s    r
   r   zgroupmore.<locals>.<lambda>   s    dd1Q4jj r   c              3   2   K   | ]\  }}|t          | fV  d S r   )r1   )r)   r   groupss      r
   r+   zgroupmore.<locals>.<genexpr>   s/      >>Vc6l#>>>>>>r   )rH   	itertoolsgroupbyr1   )r   itsnfunczipperunzippers   `    r
   	groupmorerT   z   s`     {
C
"T
"
"C    EsCy%00F>>v>>>HOr   )NNr   )	__doc__collections.abcr9   rN   r   r   r   rH   rT   r   r   r
   <module>rW      s             $ $ $ $5 5 5 5T' T' T'n     r   