a
    zn„fw‡  ã                   @  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ZddlZddl	Z	ddl
mZ ddlmZ ddlm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ZddlZdd	„ Zd
dœdd„Zd
dœdd„Zddœdd„Zejejej ffdd„Z!dad
d
d
d
dœdd„Z"ddœdd„Z#dd
dœdd„Z$dd „ Z%d
dœd!d"„Z&e 'd#¡Z(d$d%„ Z)d&d'„ Z*e 'd(¡Z+d)d*„ Z,e 'd+ej-¡Z.d,d-„ Z/dd.d/œd0d1„Z0d2d3d4d5œd6d7„Z1e2e3e4ƒ�rÊd8e3v �rÀe3d8 Z5ne3d9 Z5ne6e3d8ƒ�rÞe3j7Z5ne3j8Z5dbd:d;„Z9dcd<dœd=d>„Z:d?d@„ Z;dAdB„ Z<i Z=dCdD„ Z>dEdF„ Z?dGdH„ Z@dddIdJ„ZAdKdL„ ZBdMdN„ ZCdOdP„ ZDe 'dQ¡ZEe 'dR¡ZFdSdT„ ZGdUdV„ ZHdWdX„ ZIdYdZ„ ZJd[d\„ ZKd]d^„ ZLG d_d`„ d`ƒZMeMƒ ZNdS )ezs
This module defines utilities for internal use. This is not a public interface
and may be changed without notice.
é    )ÚannotationsN)ÚIterable)ÚPath)ÚIO)Úurlparsec                 C  s   t | dƒr|  ¡ S |  ¡ S dS )z¢
    Calls ``array.tobytes()`` or its older equivalent, ``array.tostring()``,
    depending on what's available in this NumPy version. (tobytes added in 1.9)
    ÚtobytesN)Úhasattrr   Ztostring)Úarray© r
   úO/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/uproot/_util.pyr      s    
r   Úbool)Úreturnc                 C  s&   t | ttjtjfƒo$t | ttjfƒ S )zd
    Returns True if and only if ``x`` is an integer (including NumPy, not
    including bool).
    )Ú
isinstanceÚintÚnumbersÚIntegralÚnumpyÚintegerr   Úbool_©Úxr
   r
   r   Úisint*   s    
ÿr   c                 C  s(   t | tttjtjfƒo&t | ttjfƒ S )zb
    Returns True if and only if ``x`` is a number (including NumPy, not
    including bool).
    )	r   r   Úfloatr   ÚRealr   Únumberr   r   r   r
   r
   r   Úisnum4   s    
ÿr   Ústrc                 C  s:   t | tƒr| jdd�S t | tƒr$| S tdt| ƒ› �ƒ‚dS )zX
    Ensures that ``x`` is a string (decoding with 'surrogateescape' if necessary).
    Úsurrogateescape)Úerrorszexpected a string, not N)r   ÚbytesÚdecoder   Ú	TypeErrorÚtyper   r
   r
   r   Ú
ensure_str>   s
    

r#   c                 C  sÒ   ddl }|j ¡ }t ¡ �¤ t dttdtƒj¡ t	| |j
jƒrL| | ¡}nDzt | ¡}W n4 ttjfyŽ } ztdƒ|‚W Y d}~n
d}~0 0 t|jj|ƒs¬td|› �ƒ‚|W  d  ƒ S 1 sÄ0    Y  dS )zˆ
    Returns an ``np.ndarray`` if ``array`` can be converted to an array of the
    desired type and raises TypeError if it cannot.
    r   NÚerrorÚ
exceptionsz$cannot be converted to a NumPy arrayz-cannot be converted to a NumPy array of type )ÚuprootÚextrasÚawkwardÚwarningsÚcatch_warningsÚsimplefilterÚgetattrr   ZVisibleDeprecationWarningr   ÚcontentsZContentZto_numpyZasarrayÚ
ValueErrorr!   Ú
issubclassÚdtyper"   )r	   Útypesr&   r(   ÚoutÚerrr
   r
   r   Úensure_numpyJ   s    

ÿ r4   F)ÚreadableÚwritableÚseekabler   c                   sd   t ‡ fdd„dD ƒƒob| s0tˆ dƒ s0ˆ  ¡ ob| sJtˆ dƒ sJˆ  ¡ ob| pbtˆ dƒ pbˆ  ¡ S )Nc                 3  s   | ]}t tˆ |d ƒƒV  qd S ©N©Úcallabler,   ©Ú.0Úattr©Úobjr
   r   Ú	<genexpr>f   s   ÿzis_file_like.<locals>.<genexpr>)ÚreadÚwriteÚseekÚtellÚflushr5   r6   r7   )Úallr   r5   r6   r7   )r?   r5   r6   r7   r
   r>   r   Úis_file_likeb   s    þüûùrG   ©Úversionc                 C  s   t j | ¡S )zÑ
    Converts a semver string into a Version object that can be compared with
    ``<``, ``>=``, etc.

    Currently implemented using ``packaging.Version``
    (exposing that library in the return type).
    )Ú	packagingrI   ÚparserH   r
   r
   r   Úparse_versionp   s    rL   )Úmodule_namer   c                 C  sT   zt | ƒ ¡ }W n ty$   Y dS 0 |D ]$}|j|ksH|j |d ¡r* dS q*dS )aG  
    Returns True if ``obj`` is an instance of a class from a module
    given by name.

    This is like ``isinstance`` (in that it searches the whole ``mro``),
    except that the module providing the type to check against doesn't
    have to be imported and doesn't get imported (as a side effect) by
    this function.
    FÚ.T)r"   Úmror!   Ú
__module__Ú
startswith)r?   rM   rO   Útr
   r
   r   Úfrom_module{   s    
rS   c                 C  sˆ   d}| D ]z}|dkr |t j7 }q|dkr4|t j7 }q|dkrH|t j7 }q|dkr\|t j7 }q|dkrp|t j7 }q|dkr|t j7 }q|S )Nr   ÚiÚLÚmÚsÚur   )ÚreÚIrU   ÚMÚSÚUÚX)ÚflagsZ	flagsbyteÚflagr
   r
   r   Ú_regularize_filter_regex_flags�   s    ra   c                 C  s   dS )z?
    A filter that accepts anything (always returns True).
    Tr
   r   r
   r
   r   Ú	no_filter¢   s    rb   z^/(.*)/([iLmsux]*)$c                   sÎ   ˆ du rt S tˆ ƒrˆ S tˆ tƒrŽt ˆ ¡}|dur\| ¡ \}}t |t	|ƒ¡‰‡fdd„S dˆ v stdˆ v stdˆ v r€‡ fdd„S ‡ fdd„S n<tˆ t
ƒr¼tˆ tƒs¼d	d
„ ˆ D ƒ‰‡fdd„S tdˆ ›�ƒ‚dS )z�
    Convert None, str, iterable of str, wildcards, and regular expressions into
    the standard form for a filter: a callable returning True or False.
    Nc                   s   ˆ   | ¡d uS r8   )Úmatchr   )Úmatcherr
   r   Ú<lambda>º   ó    z#regularize_filter.<locals>.<lambda>Ú*ú?ú[c                   s   t j | ˆ ¡S r8   )ÚglobÚfnmatchÚfnmatchcaser   ©Úfilterr
   r   re   ¼   rf   c                   s   | ˆ kS r8   r
   r   rm   r
   r   re   ¾   rf   c                 S  s   g | ]}t |ƒ‘qS r
   )Úregularize_filter©r<   Úfr
   r
   r   Ú
<listcomp>À   rf   z%regularize_filter.<locals>.<listcomp>c                   s   t ‡ fdd„ˆD ƒƒS )Nc                 3  s   | ]}|ˆ ƒV  qd S r8   r
   rp   r   r
   r   r@   Á   rf   z6regularize_filter.<locals>.<lambda>.<locals>.<genexpr>)Úanyr   )Úfiltersr   r   re   Á   rf   zVfilter must be None, callable, a regex string between slashes, or a glob pattern, not )rb   r:   r   r   Ú_regularize_filter_regexrc   ÚgroupsrY   Úcompilera   r   r   r!   )rn   rV   Úregexr_   r
   )rn   rt   rd   r   ro   ¬   s*    

ÿÿro   c                 C  s   | S )zP
    A renaming function that keeps all names the same (identity function).
    r
   r   r
   r
   r   Ú	no_renameÉ   s    ry   z^s?/(.*)/(.*)/([iLmsux]*)$c                   s  ˆdu rt S tˆƒrˆS tˆtƒrjt ˆ¡}|dur`| ¡ \}‰}t |t	|ƒ¡‰ ‡ ‡fdd„S t
dƒ‚n¢tˆtƒr€‡fdd„S tˆtƒ�rtˆtƒ�sg ‰ˆD ]Z}t|tƒrôt |¡}|durê| ¡ \}‰}ˆ t |t	|ƒ¡ˆf¡ nt
dƒ‚q  �qq ‡fdd„}|S t
dˆ›�ƒ‚dS )	z�
    Convert None, dict, and regular expression mappings into the standard form
    for renaming: a callable that maps strings to strings.
    Nc                   s   ˆ   ˆ| ¡S r8   )Úsubr   )rd   Útransr
   r   re   ã   rf   z#regularize_rename.<locals>.<lambda>z6rename regular expressions must be in '/from/to/' formc                   s   ˆ   | | ¡S r8   )Úgetr   )Úrenamer
   r   re   è   rf   c                   s0   ˆ D ]&\}}|  | ¡d ur| || ¡  S q| S r8   )Úsearchrz   )r   rd   r{   )Úrulesr
   r   Ú
applyrulesÿ   s    z%regularize_rename.<locals>.applyrulesz_rename must be None, callable, a '/from/to/' regex, an iterable of regex rules, or a dict, not )ry   r:   r   r   Ú_regularize_filter_regex_renamerc   rv   rY   rw   ra   r!   Údictr   r   Úappend)r}   rV   rx   r_   r   r€   r
   )rd   r}   r   r{   r   Úregularize_renameÓ   sH    





þÿÿÿÿr„   z^((file|https?|root):/)([^/])c                 C  sv   t | ttddƒƒr&t dt | ¡¡} nLt| dƒrBt d|  ¡ ¡} n0| jj	dkrrddl
}t | |jƒrrt dt| ƒ¡} | S )zV
    Converts pathlib Paths into plain string paths (for all versions of Python).
    ÚPathLiker
   z\1/\3Ú
__fspath__Úpathlibr   N)r   r,   ÚosÚ_fix_url_pathrz   Úfspathr   r†   Ú	__class__rP   r‡   r   r   )Úpathr‡   r
   r
   r   Úregularize_path  s    
r�   ztuple[str, str | None])Úurlpathr   c                 C  sš   t | ƒ ¡ } d}d}|  |¡}t d¡}tt|ƒƒD ]R\}}| |¡}|r4t dd| 	d¡ ¡  
d¡¡ d¡}| 	d¡|| d <  qˆq4| |¡} | |fS )aŽ  
    Split a path with a colon into a file path and an object-in-file path.

    Args:
        urlpath: The path to split. Example: ``"https://localhost:8888/file.root:tree"``

    Returns:
        A tuple of the file path and the object-in-file path. If there is no
        object-in-file path, the second element is ``None``.
        Example: ``("https://localhost:8888/file.root", "tree")``
    Nz::z(.+\.root):(.*$)z/+ú/é   é   )r�   ÚstripÚsplitrY   rw   Ú	enumerateÚreversedrc   rz   ÚgroupÚlstripÚrstripÚjoin)rŽ   r?   Ú	separatorÚpartsZobject_regexrT   Úpartrc   r
   r
   r   Úfile_object_path_split$  s    


$
r�   zstr | Path | IOr‚   z1tuple[type[uproot.source.chunk.Source], str | IO])Úfile_path_or_objectÚoptionsr   c                   sÂ   t ˆ ƒ‰ |d }|dur@t|tƒr2t|tjjjƒs@td|›�ƒ‚t	‡ fdd„dD ƒƒrˆ|durz|tjj
jurztd|›�ƒ‚tjj
jˆ fS tˆ tƒr°|du r¤tjjjn|}|ˆ fS tdˆ ›�ƒ‚dS )	zÏ
    Use a file path to get the :doc:`uproot.source.chunk.Source` class that would read it.

    Returns a tuple of (class, file_path) where the class is a subclass of :doc:`uproot.source.chunk.Source`.
    ÚhandlerNz8'handler' is not a class object inheriting from Source: c                 3  s   | ]}t tˆ |d ƒƒV  qd S r8   r9   r;   ©rž   r
   r   r@   W  s   z,file_path_to_source_class.<locals>.<genexpr>)rA   rC   z6'handler' is not ObjectSource for a file-like object: z/file_path is not a string or file-like object: )r�   r   r"   r/   r&   ÚsourceÚchunkZSourcer!   rF   ÚobjectZObjectSourcer   ÚfsspecÚFSSpecSource)rž   rŸ   Z
source_clsr
   r¡   r   Úfile_path_to_source_classB  s8    	ÿþÿÿÿþÿ
ÿÿr§   ÚFileNotFoundErrorÚIOErrorc                 C  s.   |d u rdn
d| d }t d|› d| ›d�ƒS )NÚ z (ú)zfile not foundz

    aí  

Files may be specified as:
   * str/bytes: relative or absolute filesystem path or URL, without any colons
         other than Windows drive letter or URL schema.
         Examples: "rel/file.root", "C:\abs\file.root", "http://where/what.root"
   * str/bytes: same with an object-within-ROOT path, separated by a colon.
         Example: "rel/file.root:tdirectory/ttree"
   * pathlib.Path: always interpreted as a filesystem path or URL only (no
         object-within-ROOT path), regardless of whether there are any colons.
         Examples: Path("rel:/file.root"), Path("/abs/path:stuff.root")

Functions that accept many files (uproot.iterate, etc.) also allow:
   * glob syntax in str/bytes and pathlib.Path.
         Examples: Path("rel/*.root"), "/abs/*.root:tdirectory/ttree"
   * dict: keys are filesystem paths, values are objects-within-ROOT paths.
         Example: {"/data_v1/*.root": "ttree_v1", "/data_v2/*.root": "ttree_v2"}
   * already-open TTree objects.
   * iterables of the above.
)Ú_FileNotFoundError)ÚfilesÚmessager
   r
   r   Ú_file_not_foundz  s    ÿr¯   r   c                 C  s²  t | tƒ�rzt d| tj¡}|du�rzt| d¡ƒ| d¡ ¡  }}|dkrX|d9 }�n|dkrl|d9 }�n|d	kr~|d
9 }nô|dkr�|d9 }nâ|dkr¢|d9 }nÐ|dkr´|d9 }n¾|dkrÆ|d9 }n¬|dkrØ|d9 }nš|dkrê|d9 }nˆ|dkrü|d9 }nv|dk�r|d9 }nb|dk�r$|d9 }nN|dk�r8|d9 }n:|dk�rL|d 9 }n&|d!k�r`|d"9 }n|d#k�rr|d$9 }t|ƒS t	| ƒ�rŒt| ƒS |du �r¦t
d%| ›�ƒ‚nt
|ƒ‚dS )&zo
    Regularizes strings like '## kB' and plain integer number of bytes to
    an integer number of bytes.
    z?^\s*([+-]?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?)\s*([kmgtpezy]?b)\s*$Nr‘   é   ZKBiè  ÚMBi@B ÚGBi Êš;ÚTBl    J)£ÚPBl     �Iú5 ÚEBl     ÈNÎZoÚZBl     @=7M.cÚYBl      B¶3v^!< ZKIBi   ZMIBi   ZGIBi   @ZTIBl        ZPIBl           ZEIBl            ZZIBl            ZYIBl               zRnumber of bytes or memory size string with units (such as '100 MB') required, not )r   r   rY   rc   rZ   r   r–   Úupperr   r   r!   )ÚdataÚerror_messagerV   ÚtargetÚunitr
   r
   r   Úmemory_size—  sd    ý





















ÿÿr½   c                 C  s   g }|d }t |dd… ƒD ]Þ\}}	||kr\||	kr\|| }
|| }| | | |
|… ¡ nš||  krp|	k rœn n(|| }
|	| }| | | |
|… ¡ nZ||  kr°|	krØn n$d}
|| }| | | |
|… ¡ n||	k rö||krö| | | ¡ |	}q|S )zi
    Trims the output from a basket_array function and outputs the un-concatenated list of elements.
    r   r‘   N)r”   rƒ   )Zbasket_arraysZentry_startZ
entry_stopZentry_offsetsZlibraryÚbranchZtrimmedÚstartZ
basket_numÚstopZlocal_startZ
local_stopr
   r
   r   Ú
trim_finalÒ  s&    rÁ   c                 C  s2   ddl }tt| ƒ||ƒ}d|_t|j|j|ƒ |S )zy
    Create a new class object with ``type(name, bases, members)`` and put it in
    the ``uproot.dynamic`` library.
    r   Nzuproot.dynamic)Zuproot.dynamicr"   r#   rP   ÚsetattrZdynamicÚ__name__)ÚnameÚbasesÚmembersr&   r2   r
   r
   r   Ú	new_classð  s
    rÇ   c           
      C  sX  ddl }|j ¡ }t| tjƒ�rH|  d¡} | tv�r@| t tj¡ksR| t t	¡krf|j
 d¡t| < �nÚ| t tj¡krŠ|j
 d¡t| < �n¶| t tj¡kr®|j
 d¡t| < �n’| t tj¡krÒ|j
 d¡t| < �nn| t tj¡krö|j
 d¡t| < �nJ| t tj¡k�r|j
 d	¡t| < �n$| t tj¡k�rB|j
 d
¡t| < � nþ| t tj¡k�rf|j
 d¡t| < nÚ| t tj¡k�rŠ|j
 d¡t| < n¶| t tj¡k�r®|j
 d¡t| < n’| t tj¡k�rÒ|j
 d¡t| < nn| jdu�r*g }g }| j ¡ D ]*\}\}}	| |¡ | t|||ƒ¡ �qð|j
 ||¡S t| ›dt| ƒ› �ƒ‚t|  S |  ||¡S dS )zW
    Utility function to get an ``ak.forms.Form`` for a :doc:`uproot.model.Model`.
    r   Nú=z"bool"z"int8"z"uint8"z"int16"z"uint16"z"int32"z"uint32"z"int64"z"uint64"z	"float32"z	"float64"z: )r&   r'   r(   r   r   r0   ZnewbyteorderÚ_primitive_awkward_formr   r   ÚformsÚ	from_jsonZint8Zuint8Zint16Zuint16Zint32Zuint32Úint64Zuint64Zfloat32Úfloat64ÚfieldsÚitemsrƒ   Úawkward_formÚ
RecordFormÚAssertionErrorr"   )
ÚmodelÚfileÚcontextr&   r(   rÎ   r-   Úfieldr0   Ú_r
   r
   r   rÐ      sJ    



rÐ   c                 C  s¦   ddl }t||jjjƒrji }|j ¡ D ]\}}t| || |¡ƒ||< q&| j	j
t| ¡ ƒt| ¡ ƒ|jd�S t||jjjƒrž|jj ||¡rŽ|S |j | |¡S n|S dS )z°
    Given an interpretation of a TBranch, fixup any form corresponding to
    a component that cannot be read directly by awkward and needs to go
    through ak.from_iter
    r   N©Ú
parameters)Zuproot.interpretationr   ÚinterpretationÚgroupedZ	AsGroupedZsubbranchesrÏ   Ú$recursively_fix_awkward_form_of_iterÚcontentrÊ   rÑ   ÚlistÚvaluesÚkeysrÙ   ÚobjectsZ	AsObjectsZawkward_can_optimizeZ_utilÚawkward_form_of_iter)r(   rÚ   Úformr&   ÚfixedÚkeyZsubinterpretationr
   r
   r   rÜ   3  s"    ÿ


ýrÜ   c                   sÈ  t |ˆ jjƒr4ˆ jj|jtˆ |jƒ|j|j|jd�S t |ˆ jj	ƒrdˆ jj	|jtˆ |jƒ|j|jd�S t |ˆ jj
ƒr‚ˆ jj
|jd�S t |ˆ jjƒr®ˆ jj|jtˆ |jƒ|jd�S t |ˆ jjƒrÚˆ jj|jtˆ |jƒ|jd�S t |ˆ jjƒ�rˆ jj|j|jtˆ |jƒ|jd�S t |ˆ jjƒ�r:ˆ jj|jtˆ |jƒ|jd�S t |ˆ jjƒ�rÔ| d¡dv �r`|}nXˆ jj |j¡}t|jtjƒ�rŽt tj¡}nt|jtjƒ�rªt tj ¡}ˆ jj! "|¡}ˆ jj|j|j#|jd�}|S t |ˆ jj$ƒ�r ‡ fdd„|j% &¡ D ƒ}ˆ jj$t'| (¡ ƒt'| )¡ ƒ|jd�S t |ˆ jj*ƒ�rNˆ jj*tˆ |jƒ|j+|jd�S t |ˆ jj,ƒ�rˆˆ jj,|j-|j‡ fdd„|j%D ƒ|jd�S t |ˆ jj.ƒ�r²ˆ jj.tˆ |jƒ|jd�S t/dt|ƒ› �ƒ‚d	S )
z}
    Fix an ``ak.forms.Form`` object for a given iterable.

    (It might have been read with a different numeric type.)
    rØ   Z	__array__)ÚcharÚbytec                   s   i | ]\}}|t ˆ |ƒ“qS r
   ©râ   )r<   ÚkÚv©r(   r
   r   Ú
<dictcomp>’  s   z(awkward_form_of_iter.<locals>.<dictcomp>c                   s   g | ]}t ˆ |ƒ‘qS r
   rè   ©r<   r   rë   r
   r   rr   ¤  rf   z(awkward_form_of_iter.<locals>.<listcomp>zunrecognized form: N)0r   rÊ   ZBitMaskedFormÚmaskrâ   rÝ   Z
valid_whenZ	lsb_orderrÙ   ZByteMaskedFormZ	EmptyFormZIndexedFormÚindexZIndexedOptionFormZListFormZstartsÚstopsZListOffsetFormÚoffsetsZ	NumpyFormZ	parameterr1   Z	numpytypeZprimitive_to_dtypeZ	primitiver/   r"   r   r   r0   rÌ   ÚfloatingrÍ   Z	numpyformZ
from_dtypeZinner_shaperÑ   r-   rÏ   rÞ   rß   rà   ZRegularFormÚsizeZ	UnionFormÚtagsZUnmaskedFormÚRuntimeError)r(   rã   rq   Údr2   r-   r
   rë   r   râ   O  s®    
û
üÿ
ý
ý
ü
ýý
ÿ

ý
ýü
þrâ   c                   sZ  ‡ fdd„t t| ƒd ƒD ƒ}t t| ƒd ƒD ]}||| d< q.t tˆ ƒd ƒD ]}||d |< qPt dt| ƒd ƒD �]�}t dtˆ ƒd ƒD �]v}| |d  ˆ |d  kr°d}n*| |d   ¡ ˆ |d   ¡ krÖd}nd}t||d  | d || |d  d ||d  |d  | ƒ|| |< |dkrŒ|dkrŒ| |d   ¡ ˆ |d   ¡ krŒ| |d   ¡ ˆ |d   ¡ krŒ| |d  ˆ |d  k�rÖ| |d  ˆ |d  k�rÖt|| | ||d  |d  d ƒ|| |< qŒt|| | ||d  |d  d ƒ|| |< qŒqt|�s"|t| ƒ tˆ ƒ S t| ƒtˆ ƒ |t| ƒ tˆ ƒ t| ƒtˆ ƒ   S dS )	zÇ
    Calculates the Damerau-Levenshtein distance of two strings.

    Used by :doc:`uproot.exceptions.KeyInFileError` to return the most likely
    misspellings of a failed attempt to get a key.
    c                   s   g | ]}d gt ˆ ƒd  ‘qS )r   r‘   )Úlen)r<   r×   ©Úbr
   r   rr   º  rf   z'damerau_levenshtein.<locals>.<listcomp>r‘   r   g      à?r�   g      ø?N)Úranger÷   ÚlowerÚmin)Úarù   Úratior[   rT   ÚjZcostr
   rø   r   Údamerau_levenshtein°  s>    
 ýÿþýü402r   c              	   C  sD   t   | d@ d? d | d@ d? | d@ d? | d@ d	? | d
@ d? | d@ ¡S )z=
    Converts a ROOT datime code into a Python datetime.
    l      x é   éË  i  Àé   i  > é   i ð é   iÀ  é   é?   )Údatetime)Úcoder
   r
   r   Úcode_to_datetimeã  s    



úr
  c                 C  s<   | j d d> | jd> B | jd> B | jd> B | jd> B | jB S )z=
    Converts a Python datetime into a ROOT datime code.
    r  r  r  r  r  r  )ÚyearÚmonthÚdayÚhourÚminuteÚsecond)Údtr
   r
   r   Údatetime_to_codeñ  s    ÿþýüûÿr  c                 C  s6   t jt| ƒt  t¡d�}t| ƒD ]\}}|||< q |S )z¼
    Converts a sized iterable into a 1D object array

    This avoids ``numpy.array``'s default behavior of turning nested iterables
    into n-d arrays when the shape is rectangular
    )r0   )r   Úemptyr÷   r0   r¤   r”   )rÏ   r2   rT   r   r
   r
   r   Úobjectarray1dÿ  s    
r  z{([^}]*,)*([^}]*)}z[\*\?\[\]{}]c                 C  sø   t  | ¡}t| tƒs$t|jjt jƒs,tdƒ‚t	|j
ƒdkrnt	|ƒdksdt  |dd … |d d… k¡sÀtdƒ‚nRt	|j
ƒdkr¸|j
d dkr®t|d d …df |d d …df kƒsÀtdƒ‚ntdƒ‚t	|j
ƒdkrðt j|d d… |dd … fdd	�}| ¡ S )
NzT'files' argument's steps must be an iterable of integer offsets or start-stop pairs.r‘   r   éÿÿÿÿznif 'files' argument's steps are (one-dimensional) offsets, they must be non-empty and monotonically increasingr�   z‰if 'files' argument's steps are (two-dimensional) start-stop pairs, all stops must be greater than or equal to their corresponding startszo'files' argument's steps must be an iterable of integer offsets or a list of pairs of integer starts and stops.)Zaxis)r   r	   r   r‚   r/   r0   r"   r   r!   r÷   ÚshaperF   r.   ÚstackÚtolist)Ústepsr2   r
   r
   r   Úregularize_steps  s*    
ÿ*ÿ2ÿÿ"r  c                 k  s  t | ƒ}d }t|tƒr(t| tƒs(d}|} t| tƒ�rÀ|rFt| ƒ\}}	n
| d  }}	t|ƒ}
|
j}d|v r°|dvr°dd„ tj|fi tj	jj
 |¡¤ŽD ƒ}|D ]}||	|fV  qš�qtj |¡}t |¡d u rØ||	|fV  nätt |¡ƒ}t|ƒdkrú|g}n~g }tjdd„ |D ƒŽ D ]d}|}tt||ƒƒd d d… D ]6\}}|d | ¡ d … | || ¡ d	 d …  }�q2| |¡ �qtƒ }|D ]8}t |¡D ]&}||v�r�||	|fV  | |¡ �q��q‚�nBt| |ƒ�rÜ| d |fV  �n&t| tƒ�r˜|  ¡ D ]¤\}}t|td ƒttfƒ�st d
ƒ‚t|tƒ�r\| !dd ¡}| !dd ¡}	|d u�r`|�sRt dƒ‚t"|ƒ}n|}	t#|d|||fi |¤ŽD ]\}}}||	|fV  �qx�qðnjt| t$ƒ�rô| D ]H}|d  d	7  < t#|||||fi |¤ŽD ]\}}	}||	|fV  �qÔ�q¨nt d| ›�ƒ‚d S )NFz://)rÔ   Úlocalc                 S  s   g | ]
}|j ‘qS r
   )Z	full_name)r<   rÔ   r
   r
   r   rr   E  s   ÿz+_regularize_files_inner.<locals>.<listcomp>r   c                 S  s$   g | ]}|  d ¡dd…  d¡‘qS )r   r‘   r  ú,)r–   r“   )r<   rc   r
   r
   r   rr   ]  rf   r  r‘   z/object_path may only be a string, dict, or Noner  Úobject_pathzXunrecognized 'files' pattern for this function ('steps' are only allowed in uproot.dask)zÍ'files' must be a file path/URL (string or Path), possibly with a glob pattern (for local files), a dict of {path/URL: TTree/TBranch name}, actual TTree/TBranch objects, or an iterable of such things, not )%r�   r   r   r�   r   Úschemer¥   Z
open_filesr&   r¢   r¦   Zextract_fsspec_optionsrˆ   rŒ   Ú
expanduserÚ_regularize_files_isglobr~   rÞ   Ú_regularize_files_bracesÚfinditerr÷   Ú	itertoolsÚproductÚzipÚspanrƒ   Úsetrj   Úaddr‚   rÏ   r"   r!   r|   r  Ú_regularize_files_innerr   )r­   Zparse_colonÚcounterÚHasBranchesÚsteps_allowedrŸ   Zfiles2Úmaybe_stepsÚ	file_pathr  Ú
parsed_urlr  Zfile_names_fullZfile_name_fullÚexpandedÚmatchesÚresultsZcombinationÚtmpÚcrV   ÚseenÚresultrc   rå   Zmaybe_object_pathr×   rÔ   r
   r
   r   r)  /  s¢    
ÿþþ	ÿ 0

ÿ
ûú
ÿÿýÿr)  c                 K  sÜ   ddl m} g }tƒ }dg}t| d|||fi |¤ŽD ]Ž\}}}	t|tƒr–|d ||f}
|
|vrÂ| ||f¡ |	durŠg |d ¢|	‘R |d< | |
¡ q4| ||f¡ |	dur4g |d ¢|	‘R |d< q4t|ƒdkrØt	| ƒ‚|S )z„
    Common code for regularizing the possible file inputs accepted by uproot so they can be used by uproot internal functions.
    r   )r+  TNr  )
Úuproot.behaviors.TBranchr+  r'  r)  r   r   rƒ   r(  r÷   r¯   )r­   r,  rŸ   r+  r2   r5  r*  r.  r  r-  rå   r
   r
   r   Úregularize_files˜  s,    
ÿÿ
r8  c                 C  sà   ddl m}m} ddlm} t| |ƒr.|| ƒS || fdd|dœ|¤Žj}|du rÄ|jddd�}	t|	ƒdkr‚|rrdS t	d	| › �ƒ‚qÜt|	ƒd
krš||	d  S d 
dd„ |	D ƒ¡}
t	d|
› d| › �ƒ‚n|rÔ||vrÔdS || S dS )zD
    Returns the TTree object from given object and file paths.
    r   )r+  Ú_NoClose)ÚReadOnlyFileN)Zobject_cacheZarray_cacheÚcustom_classesZTTreeF)Zfilter_classnameÚcyclezno TTrees found
in file r‘   z, c                 s  s   | ]}t |ƒV  qd S r8   )Úreprrí   r
   r
   r   r@   Õ  rf   z)regularize_object_path.<locals>.<genexpr>z†TTree object paths must be specified in the 'files' as {"filenames*.root": "path"} if any files have more than one TTree

    TTrees: z


in file )r7  r+  r9  Zuproot.readingr:  r   Zroot_directoryrà   r÷   r.   r™   )r.  r  r;  Zallow_missingrŸ   r+  r9  r:  rÔ   ZtreesZ	ttree_strr
   r
   r   Úregularize_object_path·  s@    
ÿüûüúÿr>  c                 C  sj   |  d¡r|dd … }nF|  d¡r0|dd … }n.|  d¡rH|dd … }n|  d¡r^|dd … }t| j|ƒS )	N)Z32Z64éþÿÿÿZU32éýÿÿÿ)Z8_32Z8_64éüÿÿÿZ8_U32éûÿÿÿ)Úendswithr,   r-   )r(   rÄ   r
   r
   r   Ú_content_cls_from_nameæ  s    



rD  c                 C  s.   z| j jj}W n ty(   dd„ }Y n0 |S )Nc                 S  s   | j S r8   )Z
is_numericr   r
   r
   r   Úfunction÷  s    z,pandas_has_attr_is_numeric.<locals>.function)Úapir1   Zis_any_real_numeric_dtypeÚAttributeError)ZpandasrE  r
   r
   r   Úpandas_has_attr_is_numericò  s
    rH  c                   @  s   e Zd Zdd„ ZdS )Ú_Unsetc                 C  s
   t › d�S )Nz.unset)rÃ   )Úselfr
   r
   r   Ú__repr__þ  s    z_Unset.__repr__N)rÃ   rP   Ú__qualname__rK  r
   r
   r
   r   rI  ý  s   rI  )FFF)N)N)F)OÚ__doc__Ú
__future__r   r  rj   r#  r   rˆ   rY   r)   Úcollections.abcr   r‡   r   Útypingr   Úurllib.parser   r¥   r   Zpackaging.versionrJ   Zuproot.source.chunkr&   Zuproot.source.fsspecZuproot.source.objectr   r   r   r#   r   r   rò   r4   rG   rL   rS   ra   rb   rw   ru   ro   ry   r�   r„   rZ   r‰   r�   r�   r§   r   Ú__builtins__r‚   r¬   r   r¨   r©   r¯   r½   rÁ   rÇ   rÉ   rÐ   rÜ   râ   r   r
  r  r  r!  r   r  r)  r8  r>  rD  rH  rI  Úunsetr
   r
   r
   r   Ú<module>   s†   

 ÿ

;,



;3a
3

i/