3
½ìœ]p  ã               @   s„   d 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 G dd„ dejƒZG dd„ dejejƒZdS )z3
    Implements support for HDF5 dimension scales.
é    )Úabsolute_importNé   )Úh5ds)ÚH5pyDeprecationWarningé   )Úbase)ÚphilÚ	with_phil)ÚDatasetc               @   s®   e Zd ZdZeedd„ ƒƒZejedd„ ƒƒZedd„ ƒZedd„ ƒZ	ed	d
„ ƒZ
edd„ ƒZedd„ ƒZedd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zedd„ ƒZdS )ÚDimensionProxyz-
        Represents an HDF5 "dimension".
    c             C   s>   y t | jƒ}| j|jd | j ƒS  ttfk
r8   dS X dS )z& Get or set the dimension scale label ZDIMENSION_LABELSÚ N)r
   Ú_idÚ_dÚattrsÚ
_dimensionÚKeyErrorÚ
IndexError)ÚselfÚdset© r   ú-/tmp/pip-build-pcqn41bp/h5py/h5py/_hl/dims.pyÚlabel   s
    
zDimensionProxy.labelc             C   s   t j| j| j| j|ƒƒ d S )N)r   Z	set_labelr   r   Ú_e)r   Úvalr   r   r   r   *   s    c             C   s   || _ || _d S )N)r   r   )r   Zid_Z	dimensionr   r   r   Ú__init__0   s    zDimensionProxy.__init__c             C   s   t t| ƒ| j| jfƒS )N)ÚhashÚtyper   r   )r   r   r   r   Ú__hash__5   s    zDimensionProxy.__hash__c             C   s   t | ƒt |ƒkS )N)r   )r   Úotherr   r   r   Ú__eq__9   s    zDimensionProxy.__eq__c             c   s   x| j ƒ D ]
}|V  q
W d S )N)Úkeys)r   Úkr   r   r   Ú__iter__=   s    zDimensionProxy.__iter__c             C   s   t j| j| jƒS )N)r   Zget_num_scalesr   r   )r   r   r   r   Ú__len__B   s    zDimensionProxy.__len__c                sn   t ˆ tƒr0g }tjˆjˆj|jdƒ t|ˆ  ƒS ‡ ‡fdd„}tjˆjˆj|dƒ}|d krbtˆ ƒ‚t|ƒS d S )Nr   c                s   t j| ƒˆjˆ ƒkr| S dS )z- Iterate over scales to find a matching name N)r   Úget_scale_namer   )Zdsid)Úitemr   r   r   ÚfO   s    z%DimensionProxy.__getitem__.<locals>.f)	Ú
isinstanceÚintr   Úiterater   r   Úappendr
   r   )r   r%   Úscalesr&   Úresr   )r%   r   r   Ú__getitem__F   s    
zDimensionProxy.__getitem__c             C   s(   t � tj| j|j| jƒ W dQ R X dS )zo Attach a scale to this dimension.

        Provide the Dataset of the scale you would like to attach.
        N)r   r   Úattach_scaler   Úidr   )r   r   r   r   r   r.   Y   s    zDimensionProxy.attach_scalec             C   s(   t � tj| j|j| jƒ W dQ R X dS )zq Remove a scale from this dimension.

        Provide the Dataset of the scale you would like to remove.
        N)r   r   Údetach_scaler   r/   r   )r   r   r   r   r   r0   a   s    zDimensionProxy.detach_scalec                sH   t �: g }tˆ ƒdkr,tjˆ jˆ j|jdƒ ‡ fdd„|D ƒS Q R X dS )zX Get a list of (name, Dataset) pairs with all scales on this
        dimension.
        r   c                s$   g | ]}ˆ j tj|ƒƒt|ƒf‘qS r   )r   r   r$   r
   )Ú.0Úx)r   r   r   ú
<listcomp>v   s   z(DimensionProxy.items.<locals>.<listcomp>N)r   Úlenr   r)   r   r   r*   )r   r+   r   )r   r   Úitemsi   s    
zDimensionProxy.itemsc          	   C   s"   t � dd„ | jƒ D ƒS Q R X dS )z7 Get a list of names for the scales on this dimension. c             S   s   g | ]\}}|‘qS r   r   )r1   ÚkeyÚ_r   r   r   r3   }   s    z'DimensionProxy.keys.<locals>.<listcomp>N)r   r5   )r   r   r   r   r    z   s    zDimensionProxy.keysc          	   C   s"   t � dd„ | jƒ D ƒS Q R X dS )z5 Get a list of Dataset for scales on this dimension. c             S   s   g | ]\}}|‘qS r   r   )r1   r7   r   r   r   r   r3   ‚   s    z)DimensionProxy.values.<locals>.<listcomp>N)r   r5   )r   r   r   r   Úvalues   s    zDimensionProxy.valuesc             C   s"   | j s
dS d| j| jt| j ƒf S )Nz"<Dimension of closed HDF5 dataset>z)<"%s" dimension %d of HDF5 dataset at %s>)r   r   r   r/   )r   r   r   r   Ú__repr__„   s    zDimensionProxy.__repr__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úpropertyr	   r   Úsetterr   r   r   r"   r#   r-   r.   r0   r5   r    r8   r9   r   r   r   r   r      s"   r   c               @   sV   e Zd ZdZedd„ ƒZedd„ ƒZedd„ ƒZedd	„ ƒZed
d„ ƒZ	ddd„Z
dS )ÚDimensionManagerzÍ
        Represents a collection of dimension associated with a dataset.

        Like AttributeManager, an instance of this class is returned when
        accessing the ".dims" property on a Dataset.
    c             C   s   |j | _dS )z Private constructor.
        N)r/   r   )r   Úparentr   r   r   r   •   s    zDimensionManager.__init__c             C   s$   |t | ƒd krtdƒ‚t| j|ƒS )z# Return a Dimension object
        r   zIndex out of range)r4   r   r   r   )r   Úindexr   r   r   r-   ›   s    zDimensionManager.__getitem__c             C   s   t t| jƒjƒS )z3 Number of dimensions associated with the dataset. )r4   r
   r   Úshape)r   r   r   r   r#   £   s    zDimensionManager.__len__c             c   s$   xt t| ƒƒD ]}| | V  qW dS )z Iterate over the dimensions. N)Úranger4   )r   Úir   r   r   r"   ¨   s    zDimensionManager.__iter__c             C   s   | j s
dS dt| j ƒ S )Nz#<Dimensions of closed HDF5 dataset>z!<Dimensions of HDF5 object at %s>)r   r/   )r   r   r   r   r9   ®   s    zDimensionManager.__repr__r   c             C   s   t jdtdd� |j|ƒ dS )zo Create a new dimension, from an initial scale.

        Provide the dataset and a name for the scale.
        zTother_ds.dims.create_scale(ds, name) is deprecated. Use ds.make_scale(name) instead.r   )Ú
stacklevelN)ÚwarningsÚwarnr   Z
make_scale)r   r   Únamer   r   r   Úcreate_scale´   s    
zDimensionManager.create_scaleN)r   )r:   r;   r<   r=   r	   r   r-   r#   r"   r9   rJ   r   r   r   r   r@   Œ   s   r@   )r=   Ú
__future__r   rG   r   r   Zh5py_warningsr   r   r   r	   Zdatasetr
   ZCommonStateObjectr   ZMappingHDF5r@   r   r   r   r   Ú<module>   s   t