a
    yn„f¢Y  ã                   @  s˜  U d dl mZ d dlZd dlZd dlZd dlmZ d dlmZm	Z	m
Z
 d dlmZ d dlm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mZ d dlmZmZmZ d dlm Z m!Z!m"Z"m#Z#m$Z$m%Z% dZ&e '¡ Z(e '¡ Z)e*h d£ƒZ+de,d< dddœdd„Z-dddœdd„Z.dddœdd„Z/d/dd„Z0G dd „ d ƒZ1d!d"d#œd$d%„Z2e( 3d&¡e( 3d'¡e( 3d(¡e( 3d)¡e( 3d*¡d+œZ4d,e,d-< G d.d„ deƒZ5dS )0é    )ÚannotationsN)Údefaultdict)ÚCallableÚIterableÚMapping)Úfnmatchcase)Úescape)ÚNumpyBackend)ÚMeta)ÚNumpyMetadata)Ú	ShapeItemÚunknown_length)Úparameters_are_equalÚparameters_unionÚtype_parameters_equal)ÚAnyÚDTypeÚFinalÚIteratorÚJSONMappingÚSelf)Ú	from_dictÚ	from_typeÚ	from_jsonÚreserved_nominal_parametersÚForm>   )Ú	__array__Úchar)r   Úcategorical)r   Ú
bytestring)r   Z
sorted_map)r   Úbyte)r   Ústringr   r   r   r   )ÚinputÚreturnc           	      C  sd  | d usJ ‚t | tƒr$tjj| d�S t | tƒs2J ‚|  dd ¡}|  dd ¡}| d dkrŒ| d }tdd„ |  d	g ¡D ƒƒ}tjj||||d
�S | d dkr¨tjj||d
�S | d dkrätjj	t
| d ƒ| d d u rÔtn| d ||d�S | d dv �rtjj| d | d t
| d ƒ||d�S | d dv �rFtjj| d t
| d ƒ||d�S | d dk�rd| v �r’t | d tƒ�rvtdƒ‚dd„ | d D ƒ}| d }n`t | d tƒ�rÜg }g }| d  ¡ D ]"\}}| t
|ƒ¡ | |¡ �q¶ndd„ | d D ƒ}d }tjj||||d�S | d dv �r4tjj| d  t
| d ƒ||d!�S | d d"v �rbtjj| d  t
| d ƒ||d!�S | d d#k�r–tjj| d$ t
| d ƒ| d% ||d&�S | d d'k�rÐtjj| d$ t
| d ƒ| d% | d( ||d)�S | d d*k�røtjjt
| d ƒ||d+�S | d d,v �r2tjj| d- | d  d.d„ | d D ƒ||d/�S | d d0k�rJtd1ƒ‚ntd2 t| d ƒ¡ƒ‚d S )3N)Ú	primitiveÚ
parametersÚform_keyÚclassZ
NumpyArrayr$   c                 s  s   | ]}|d u rt n|V  qd S ©N)r   ©Ú.0Úitem© r,   úU/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/awkward/forms/form.pyÚ	<genexpr>=   s   ÿzfrom_dict.<locals>.<genexpr>Úinner_shape©r%   r&   Z
EmptyArrayZRegularArrayÚcontentÚsize)r1   r2   r%   r&   )Z	ListArrayZListArray32ZListArrayU32ZListArray64ÚstartsÚstops)r3   r4   r1   r%   r&   )ZListOffsetArrayZListOffsetArray32ZListOffsetArrayU32ZListOffsetArray64Úoffsets)r5   r1   r%   r&   ZRecordArrayÚfieldsÚcontentsz2new-style RecordForm contents must not be mappingsc                 S  s   g | ]}t |ƒ‘qS r,   ©r   ©r*   r1   r,   r,   r-   Ú
<listcomp>k   ó    zfrom_dict.<locals>.<listcomp>c                 S  s   g | ]}t |ƒ‘qS r,   r8   r9   r,   r,   r-   r:   v   r;   )r7   r6   r%   r&   )ZIndexedArrayZIndexedArray32ZIndexedArrayU32ZIndexedArray64Úindex)r<   r1   r%   r&   )ZIndexedOptionArrayZIndexedOptionArray32ZIndexedOptionArray64ZByteMaskedArrayÚmaskÚ
valid_when)r=   r1   r>   r%   r&   ZBitMaskedArrayÚ	lsb_order)r=   r1   r>   r?   r%   r&   ZUnmaskedArray)r1   r%   r&   )Z
UnionArrayZUnionArray8_32ZUnionArray8_U32ZUnionArray8_64Útagsc                 S  s   g | ]}t |ƒ‘qS r,   r8   r9   r,   r,   r-   r:   »   r;   )r@   r<   r7   r%   r&   ZVirtualArrayz+Awkward 1.x VirtualArrays are not supportedz"input class: {} was not recognised)Ú
isinstanceÚstrÚakÚformsÚ	NumpyFormr   ÚgetÚtupleÚ	EmptyFormÚRegularFormr   r   ÚListFormÚListOffsetFormÚ	TypeErrorÚitemsÚappendÚ
RecordFormÚIndexedFormÚIndexedOptionFormÚByteMaskedFormÚBitMaskedFormÚUnmaskedFormÚ	UnionFormÚ
ValueErrorÚformatÚrepr)	r"   r%   r&   r$   r/   r7   r6   Úkeyr1   r,   r,   r-   r   2   sØ    

þÿ
ü
û
ü

ü
ü
ü
û
ú	
ýû
ÿr   rB   c                 C  s   t t | ¡ƒS r(   )r   ÚjsonÚloads)r"   r,   r,   r-   r   É   s    r   zak.types.Type)Útype_r#   c                 C  sà  |   d¡rˆ| jd u rd }n| j ¡ }| d¡ t| tjjƒrbt| j	ƒ}tj
jd|t|ddiƒd�S t| j|d�ƒ}tj
jd|ddid�S t| tjjƒrªtj
j| j| jd�S t| tjjƒrÒtj
jdt| j	ƒ| jd�S t| tjjƒ� rþtj
jt| j	ƒ| j| jd�S t| tjjƒ�r(tj
jdt| j	ƒ| jd�S t| tjjƒ�rZtj
jdd„ | jD ƒ| j| jd�S t| tjjƒ�rŒtj
jd	dd
d„ | jD ƒ| jd�S t| tjjƒ�r¬tj
j| jd�S t| tjjtjjfƒ�rÎt dƒ‚nt d| ›�ƒ‚d S )NZ__categorical__Úi64r   r   )r%   )r2   r%   c                 S  s   g | ]}t |ƒ‘qS r,   ©r   ©r*   Úcr,   r,   r-   r:   ú   r;   zfrom_type.<locals>.<listcomp>Úi8c                 S  s   g | ]}t |ƒ‘qS r,   r^   r_   r,   r,   r-   r:     r;   z“High-level types (ak.types.ArrayType, ak.types.ScalarType) do not have representations as Awkward forms. Instead the low level type should be used.zunsupported type )!Z	parameterÚ_parametersÚcopyÚpoprA   rC   ÚtypesZ
OptionTyper   r1   rD   rQ   r   rP   Z	NumpyTyperE   r$   ZListTyperK   ZRegularTyperI   r2   Z
RecordTyperO   r7   r6   Z	UnionTyperU   ZUnknownTyperH   Ú	ArrayTypeZ
ScalarTyperL   )r\   Znext_parametersZnext_contentr,   r,   r-   r   Í   sp    




ÿý
ÿÿýýýüÿr   c           	      #  s¼   |d u rt ƒ }dd„ t dˆ ¡D ƒd d d… }‡ fdd„|D ƒ}t|ƒdkrfˆ |vrZˆ V  | ˆ ¡ nRtj|Ž D ]F}tˆ ƒ}t||ƒD ]\\}}}||||…< q†t	d 
|¡|ƒE d H  qpd S )Nc                 S  s   g | ]}|  ¡ ‘qS r,   )Úspan)r*   Úmr,   r,   r-   r:     r;   z"_expand_braces.<locals>.<listcomp>z\{[^\{\}]*\}éÿÿÿÿc                   s*   g | ]"\}}ˆ |d  |d  …   d¡‘qS )é   ú,©Úsplit)r*   ÚstartÚstop©Útextr,   r-   r:     r;   r   Ú )ÚsetÚreÚfinditerÚlenÚaddÚ	itertoolsÚproductÚlistÚzipÚ_expand_bracesÚjoin)	rq   ÚseenÚspansZaltsZcomboZreplacedrn   ro   Úreplacementr,   rp   r-   r|     s     r|   c                   @  sL   e Zd Zddœdddœdd„Zeddœd	d
„ƒZddœddddœdd„ZdS )Ú_SpecifierMatcherF©Úmatch_if_emptyzIterable[list[str]]Úbool)Ú
specifiersrƒ   c          	      C  sv   t ƒ }t ƒ }ttƒ}|D ]@}|^}}t|ƒ|kr<| |¡ n
| |¡ |r||  |¡ q|| _|| _|| _|| _	d S r(   )
rs   r   rz   Úescape_globrw   rN   Ú_match_to_next_specifiersÚ_fixed_stringsÚ	_patternsÚ_match_if_empty)	Úselfr…   rƒ   Zfixed_stringsÚpatternsZmatch_to_next_specifiersr+   ÚparentÚchildr,   r,   r-   Ú__init__%  s    
z_SpecifierMatcher.__init__)r#   c                 C  s   | j p
| j S r(   )r‰   rˆ   ©r‹   r,   r,   r-   Úis_emptyC  s    z_SpecifierMatcher.is_empty)Únext_match_if_emptyrB   zSelf | None)Úfieldr’   r#   c                C  s|   d}g }|| j v r&d}| | j| ¡ | jD ]"}t||ƒr,d}| | j| ¡ q,|rdt| ƒ||d�S | jrt| jrt| S d S d S )NFTr‚   )rˆ   Úextendr‡   r‰   r   Útyper‘   rŠ   )r‹   r“   r’   Zhas_matchedZnext_specifiersÚpatternr,   r,   r-   Ú__call__G  s    


z_SpecifierMatcher.__call__N)Ú__name__Ú
__module__Ú__qualname__r�   Úpropertyr‘   r—   r,   r,   r,   r-   r�   $  s
   ÿr�   zstr | CallableúCallable[[Form, str], str])Ú
buffer_keyr#   c                   sH   t ˆ tƒr‡ fdd„}|S tˆ ƒr2‡ fdd„}|S tdtˆ ƒ› �ƒ‚d S )Nc                   s   ˆ j | j|d�S )N)r&   Ú	attribute)rW   r&   ©Úformrž   ©r�   r,   r-   Úgetkeya  s    z%regularize_buffer_key.<locals>.getkeyc                   s   ˆ | j || d�S )N)r&   rž   r    ©r&   rŸ   r¡   r,   r-   r¢   h  s    z/buffer_key must be a string or a callable, not )rA   rB   ÚcallablerL   r•   )r�   r¢   r,   r¡   r-   Úregularize_buffer_key^  s    
ÿr¥   z<i1z<u1z<i4z<u4z<i8)ra   Úu8Zi32Úu32r]   zFinal[dict[str, DType]]Úindex_to_dtypec                   @  sT  e Zd Zdddœdd„Zedd„ ƒZejdd„ ƒZd	d
„ ZdEdd„Zdd„ Z	dd„ Z
dd„ Zedd„ ƒZdFdd„ZdGddœddœdd„Zdd„ Zd d!„ Zdd"d#œd$d%„Zd&d"d'œd(d)„Zd*d+„ Zd,d-„ Zed.dd/œd0d1„Zed.dd/œd2d3„Zd4dd5d6œd7d8„ZdHd:d;„Zd.d.d<œd=dddd>œd?d@„ZeZd=dddd>œdAdB„Zd=dddd>œdCdD„ZdS )Ir   zJSONMapping | Nonez
str | Noner0   c                C  sd   |d ur*t |tƒs*tt| ƒj› d|›�ƒ‚|d urTt |tƒsTtt| ƒj› d|›�ƒ‚|| _|| _d S )Nz0 'parameters' must be of type dict or None, not z0 'form_key' must be of type string or None, not )rA   ÚdictrL   r•   r˜   rB   rb   Ú	_form_key)r‹   r%   r&   r,   r,   r-   Ú_init}  s    ÿÿz
Form._initc                 C  s   | j S r(   )rª   r�   r,   r,   r-   r&   Š  s    zForm.form_keyc                 C  s$   |d urt |tƒstdƒ‚|| _d S )Nz!form_key must be None or a string)rA   rB   rL   rª   )r‹   Úvaluer,   r,   r-   r&   Ž  s    c                 C  s   t j| jdd�dd�S )NF©Úverboseé   )Úindent©rZ   ÚdumpsÚto_dictr�   r,   r,   r-   Ú__str__”  s    zForm.__str__Tc                 C  s   | j |dd�S )NT)Útoplevel)Ú_to_dict_part)r‹   r®   r,   r,   r-   r³   —  s    zForm.to_dictc                 C  sB   |s| j d ur&t| j ƒdkr&| j|d< |s4| jd ur>| j|d< |S )Nr   r%   r&   )rb   rv   r%   rª   )r‹   Úoutr®   r,   r,   r-   Ú_to_dict_extraš  s
    

zForm._to_dict_extrac                 C  s   t  | jdd�¡S )NTr­   r±   r�   r,   r,   r-   Úto_json¡  s    zForm.to_jsonc                 C  sR   g }| j d ur0t| j ƒdkr0| dt| j ƒ ¡ | jd urN| dt| jƒ ¡ |S )Nr   zparameters=z	form_key=)rb   rv   rN   rX   rª   )r‹   r·   r,   r,   r-   Ú
_repr_args¤  s    
zForm._repr_argsc                 C  s   t ‚d S r(   ©ÚNotImplementedErrorr�   r,   r,   r-   r•   ¬  s    z	Form.typeNr,   c                 C  s   g }|   |||¡ |S r(   )Ú_columns)r‹   Úlist_indicatorZcolumn_prefixÚoutputr,   r,   r-   Úcolumns°  s    zForm.columns)Úprune_unions_and_recordsr„   c          	      C  s´   t |tƒr|h}|D ]"}t |tƒs*tdƒ‚|stdƒ‚q|rfg }|D ]}t|ƒD ]}| |¡ qPqD|}dd„ t|ƒD ƒ}t|dd�}|  |¡}|d usžJ dƒ‚|r¬| 	d¡S |S d S )Nz@a column-selection specifier must be a list of non-empty stringsc                 S  s"   g | ]}|d krg n|  d¡‘qS )rr   Ú.rl   r)   r,   r,   r-   r:   Í  r;   z'Form.select_columns.<locals>.<listcomp>Fr‚   z)top-level selections always return a Form)
rA   rB   rL   rV   r|   rN   rs   r�   Ú_select_columnsÚ_prune_columns)	r‹   Ú	specifierZexpand_bracesrÁ   r+   Znext_specifierÚresultÚmatch_specifierZ	selectionr,   r,   r-   Úselect_columnsµ  s0    

ÿÿ

zForm.select_columnsc                 C  s   |   ¡ S r(   )Ú_column_typesr�   r,   r,   r-   Úcolumn_types×  s    zForm.column_typesc                 C  s   t ‚d S r(   r»   )r‹   Úpathr¿   r¾   r,   r,   r-   r½   Ú  s    zForm._columnszForm | None)Úis_inside_record_or_unionr#   c                 C  s   t ‚d S r(   r»   )r‹   rÌ   r,   r,   r-   rÄ   Ý  s    zForm._prune_columnsr�   )rÇ   r#   c                 C  s   t ‚d S r(   r»   )r‹   rÇ   r,   r,   r-   rÃ   à  s    zForm._select_columnsc                 C  s   t ‚d S r(   r»   r�   r,   r,   r-   rÉ   ã  s    zForm._column_typesc                 C  s   t ‚d S r(   r»   )r‹   r®   rµ   r,   r,   r-   r¶   æ  s    zForm._to_dict_partF)ÚbackendÚ	highlevelÚbehaviorc                C  s6   |rt dƒ‚tjjj| dddid|tjjd|d dd�
S )Nz©The `highlevel=True` variant of `Form.length_zero_array` has been removed. Please use `ak.Array(form.length_zero_array(...), behavior=...)` if an `ak.Array` is required.r   rr   s           F©
r    ÚlengthÚ	containerr�   rÍ   Ú	byteorderrÎ   rÏ   ÚattrsÚsimplify©rV   rC   Ú
operationsZak_from_buffersÚ_implZ_utilZnative_byteorder)r‹   rÍ   rÎ   rÏ   r,   r,   r-   Úlength_zero_arrayé  s     ÿözForm.length_zero_arrayc                  s\   |rt dƒ‚ddddœdd„}i ‰ ‡ ‡fdd„‰tjjjˆ| dƒdˆ d	|tjjd
|d d
d�
S )Nz§The `highlevel=True` variant of `Form.length_one_array` has been removed. Please use `ak.Array(form.length_one_array(...), behavior=...)` if an `ak.Array` is required.r   )ÚthisÚthatr#   c                 S  s"   | t u r| S |t u r|S t| |ƒS r(   )r   Úmax)rÚ   rÛ   r,   r,   r-   Úmax_prefer_unknown
  s
    z1Form.length_one_array.<locals>.max_prefer_unknownc                   sL  dt ˆƒ› �}t| tjjtjjfƒrP| jr8dˆ  ˆ|< ndˆ  ˆ|< | j|d�S t| tjjƒrrdˆ|< | j|d�S t| tjj	ƒrŒt
dƒ‚�n¼t| tjjƒr®| jˆ| jˆ ƒd�S t| tjjtjjfƒrêddˆ   ˆ|< | jˆ| jˆ ƒ|d	�S t| tjjƒ�r$ddˆ d
   ˆ|< | jˆ| jˆ ƒ|d	�S t| tjjƒ�r`| j}|tu �rHd
}| jˆ| jˆ | ƒd�S t| tjjƒ�rÂtjj | j¡}ˆ |j }| jD ]}|tu�r�||9 }�q�d| ˆ|< | j|d�S t| tjjƒ�rð| j‡ ‡fdd„| jD ƒd�S t| tjjƒ�r:ddˆ   ˆ|< | jˆ| jd ˆ ƒg| jd
d …  |d�S td| ›�ƒ‚d S )Nznode-ó    ó   ÿr£   s   ÿÿÿÿÿÿÿÿz£cannot generate a length_one_array from a Form with an unknowntype that cannot be hidden (EmptyForm not within BitMaskedForm, ByteMaskedForm, or IndexedOptionForm))r1   é   )r1   r&   rj   c                   s   g | ]}ˆ|ˆ ƒ‘qS r,   r,   )r*   Úx)Ú
multiplierÚpreparer,   r-   r:   O  r;   z:Form.length_one_array.<locals>.prepare.<locals>.<listcomp>)r7   r   )r7   r&   znot a Form: )rv   rA   rC   rD   rS   rR   r>   rc   rQ   rH   rL   rT   r1   rP   rJ   rK   rI   r2   r   rE   re   Z	numpytypeZprimitive_to_dtypeZ
_primitiveÚitemsizer/   rO   r7   rU   ÚAssertionError)r    râ   r&   r2   Údtyperá   ©rÒ   rã   )râ   r-   rã     sb    ÿÿÿ



þû	z&Form.length_one_array.<locals>.preparerj   z
{form_key}FrÐ   rÖ   )r‹   rÍ   rÎ   rÏ   rÝ   r,   rç   r-   Úlength_one_arrayÿ  s&    ÿMözForm.length_one_arrayrœ   zIterator[tuple[str, DType]])r¢   Ú	recursiver#   c                 C  s   t ‚d S r(   r»   )r‹   r¢   ré   r,   r,   r-   Ú_expected_from_buffersm  s    zForm._expected_from_buffersú{form_key}-{attribute}c                 C  s   t |ƒ}t|  ||¡ƒS )až  
        Args:
            buffer_key (str or callable): Python format string containing
                `"{form_key}"` and/or `"{attribute}"` or a function that takes these
                as keyword arguments and returns a string to use as a key for a buffer
                in the `container`.
            recursive (bool): If True, recurse into subforms; otherwise, yield
                only the (buffer_key, dtype) pairs for this form object.

        Yield (buffer_key, dtype) pairs describing the expected buffer keys,
        and their corresponding dtypes, that a call to #ak.from_buffers would
        be expected to find from the `container` object.
        )r¥   r©   rê   )r‹   r�   ré   r¢   r,   r,   r-   Úexpected_from_buffersr  s    zForm.expected_from_buffers)Úall_parametersr&   r   )Úotherrí   r&   r#   c                C  s   |   |||¡S r(   )Ú_is_equal_to©r‹   rî   rí   r&   r,   r,   r-   Úis_equal_to…  s    zForm.is_equal_toc                 C  s   t ‚d S r(   r»   rð   r,   r,   r-   rï   Œ  s    zForm._is_equal_toc                 C  s:   |rt nt}t|t| ƒƒo8|o(| j|jk o8|| j|jƒS r(   )r   r   rA   r•   rª   rb   )r‹   rî   rí   r&   Zcompare_parametersr,   r,   r-   Ú_is_equal_to_generic�  s    
ÿÿýzForm._is_equal_to_generic)T)Nr,   )T)rë   T)r˜   r™   rš   r«   r›   r&   Úsetterr´   r³   r¸   r¹   rº   r•   rÀ   rÈ   rÊ   r½   rÄ   rÃ   rÉ   r¶   Únumpy_backendrÙ   rè   rê   rì   rñ   Ú__eq__rï   rò   r,   r,   r,   r-   r   |  sF   




 ÿÿ"ÿÿn ÿ
ÿ)N)6Ú
__future__r   rx   rZ   rt   Úcollectionsr   Úcollections.abcr   r   r   Úfnmatchr   Úglobr   r†   ZawkwardrC   Zawkward._backends.numpyr	   Zawkward._meta.metar
   Zawkward._nplikes.numpy_liker   Zawkward._nplikes.shaper   r   Zawkward._parametersr   r   r   Zawkward._typingr   r   r   r   r   r   Ú__all__ÚinstanceÚnprô   Ú	frozensetr   Ú__annotations__r   r   r   r|   r�   r¥   ræ   r¨   r   r,   r,   r,   r-   Ú<module>   sD    	ÿ C
:û	