HOME


sh-3ll 1.0
DIR:/usr/local/lib/python3.6/site-packages/xarray/core/__pycache__/
Upload File :
Current File : //usr/local/lib/python3.6/site-packages/xarray/core/__pycache__/utils.cpython-36.pyc
3

���hO0�@s�dZddlZddlZddlZddlZddlZddlZddlmZm	Z	ddl
ZddlZ
ddlmZddlmZmZd@dd	�Zd
d�Zdd
�Zdd�Zdd�Zdd�Zdd�Zefdd�Zefdd�Zdd�Zdd�Zdd�Zejejej fZ!d d!�Z"d"d#�Z#d$d%�Z$efd&d'�Z%efd(d)�Z&Gd*d+�d+e'�Z(Gd,d-�d-ee(�Z)d.d/�Z*Gd0d1�d1e	e(�Z+Gd2d3�d3e	e(�Z,Gd4d5�d5e'�Z-Gd6d7�d7e-�Z.ej/d8d9��Z0d:d;�Z1d<d=�Z2d>d?�Z3dS)Az(Internal utilties; not for external use
�N)�Mapping�MutableMapping�)�ops)�	iteritems�OrderedDict�cCstjd||ft|d�dS)Nz(%s has been deprecated and renamed to %s)�
stacklevel)�warnings�warn�
FutureWarning)�old_name�new_namer	�r�1/tmp/pip-build-5_djhm0z/xray/xarray/core/utils.py�
alias_warningsrcstj����fdd��}|S)Ncst��j��||�S)N)r�__name__)�args�kwargs)�objr
rr�wrapperszfunction_alias.<locals>.wrapper)�	functools�wraps)rr
rr)rr
r�function_aliassrcs$G���fdd�d����j�_�S)Ncs"eZdZ����fdd�Z�ZS)zclass_alias.<locals>.Wrappercs$t��j�t�|�j|f|�|�S)N)rr�super�__new__)�clsrr)�Wrapper�	__class__rr
rrr"sz$class_alias.<locals>.Wrapper.__new__)r�
__module__�__qualname__r�
__classcell__r)rrr
)rrr!sr)r)rr
r)rrr
r�class_alias sr"cCs`t|tj�r|}nJt|d�r&|j�}n6i}t|d�rH|jjdkrHt|d<tjtj	|�f|�}|S)a9Given an array, safely cast it to a pandas.Index.

    If it is already a pandas.Index, return it unchanged.

    Unlike pandas.Index, if the array has dtype=object or dtype=timedelta64,
    this function will not attempt to do automatic type conversion but will
    always return an index with dtype=object.
    �to_index�dtype�O)
�
isinstance�pd�Index�hasattrr#r$�kind�object�np�asarray)�array�indexrrrr�safe_cast_to_index)s	

r0cCs*t|tj�r"|j|jkr"|j|�S|SdS)z�Wrap a transformed array with __array_wrap__ is it can be done safely.

    This lets us treat arbitrary functions that take and return ndarray objects
    like ufuncs, as long as they return an array with the same shape.
    N)r&r,�ndarray�shapeZ__array_wrap__)�originalZ	new_arrayrrr�maybe_wrap_array>s
r4cCs8t|tj�st|tj�r$tj||�S||kp2||kSdS)zuCompare two objects for equivalence (identity or equality), using
    array_equiv if either object is an ndarray
    N)r&r,r1rZarray_equiv)�first�secondrrr�
equivalentKsr7cCs"t|�}t|�}|tj|g|�fS)zxReturns the first value from iterable, as well as a new iterable with
    the same content as the original iterable
    )�iter�next�	itertools�chain)�iterable�gen�peekrrr�peek_atUsr?cCs>x8t|�D],\}}||kr
||||�r
td|��q
WdS)aHCheck the safety of updating one dictionary with another.

    Raises ValueError if dictionaries have non-compatible values for any key,
    where compatibility is determined by identity (they are the same item) or
    the `compat` function.

    Parameters
    ----------
    first_dict, second_dict : dict-like
        All items in the second dictionary are checked against for conflicts
        against items in the first dictionary.
    compat : function, optional
        Binary operator to determine if two values are compatible. By default,
        checks for equivalence.
    zJunsafe to merge dictionaries without overriding values; conflicting key %rN)r�
ValueError)�
first_dict�second_dict�compat�k�vrrr�update_safety_check^srFcCs@x:t|�D].}||ks2||kr
|||||�r
||=q
WdS)a�Remove incompatible items from the first dictionary in-place.

    Items are retained if their keys are found in both dictionaries and the
    values are compatible.

    Parameters
    ----------
    first_dict, second_dict : dict-like
        Mappings to merge.
    compat : function, optional
        Binary operator to determine if two values are compatible. By default,
        checks for equivalence.
    N)�list)rArBrCrDrrr�remove_incompatible_itemsts
rHcCst|d�ot|d�S)N�__getitem__�keys)r))�valuerrr�is_dict_like�srLcCst|t�o|td�kS)N)r&�slice)rKrrr�
is_full_slice�srNcCs8|dk	r0t|�std|��|r,td|��|S|SdS)Nz.the first argument to .%s must be a dictionaryz;cannot specify both keyword and positional arguments to .%s)rLr@)Z
pos_kwargsZ	kw_kwargs�	func_namerrr�combine_pos_and_kw_args�srPcCs0t|d�r|jdkStj|�p.t|t�p.|dkS)z�np.isscalar only works on primitive numeric types and (bizarrely)
    excludes 0-d ndarrays; this version does more comprehensive checks
    �ndimrN)r)rQr,Zisscalarr&�
_SCALAR_TYPES)rKrrr�	is_scalar�s




rScCs0ytj|�Wnttfk
r&dSXdSdS)NFT)r,r$�	TypeErrorr@)r$rrr�is_valid_numpy_dtype�s
rUcCs&tjdtd�}|g|dd�<f|_|S)Nr)r$)r)r,�emptyr+r2)rK�resultrrr�tuple_to_0darray�srXcCsLx,|D]$}||ks&|||||�rdSqWx|D]}||kr4dSq4WdS)a�Test equivalence of two dict-like objects. If any of the values are
    numpy arrays, compare them correctly.

    Parameters
    ----------
    first, second : dict-like
        Dictionaries to compare for equality
    compat : function, optional
        Binary operator to determine if two values are compatible. By default,
        checks for equivalence.

    Returns
    -------
    equals : bool
        True if the dictionaries are equal
    FTr)r5r6rCrDrrr�
dict_equiv�s

rYcCst|�}t|||�|S)a�Return the intersection of two dictionaries as a new OrderedDict.

    Items are retained if their keys are found in both dictionaries and the
    values are compatible.

    Parameters
    ----------
    first_dict, second_dict : dict-like
        Mappings to merge.
    compat : function, optional
        Binary operator to determine if two values are compatible. By default,
        checks for equivalence.

    Returns
    -------
    intersection : OrderedDict
        Intersection of the contents.
    )rrH)rArBrCZnew_dictrrr�ordered_dict_intersection�srZc@s eZdZdZdd�Zdd�ZdS)�SingleSlotPickleMixinz�Mixin class to add the ability to pickle objects whose state is defined
    by a single __slots__ attribute. Only necessary under Python 2.
    cCst||jd�S)Nr)�getattr�	__slots__)�selfrrr�__getstate__�sz"SingleSlotPickleMixin.__getstate__cCst||jd|�dS)Nr)�setattrr])r^�staterrr�__setstate__�sz"SingleSlotPickleMixin.__setstate__N)rrr �__doc__r_rbrrrrr[�sr[c@sFeZdZdZdgZdd�Zdd�Zdd�Zd	d
�Zdd�Z	d
d�Z
dS)�Frozenz�Wrapper around an object implementing the mapping interface to make it
    immutable. If you really want to modify the mapping, the mutable version is
    saved under the `mapping` attribute.
    �mappingcCs
||_dS)N)re)r^rerrr�__init__szFrozen.__init__cCs
|j|S)N)re)r^�keyrrrrIszFrozen.__getitem__cCs
t|j�S)N)r8re)r^rrr�__iter__szFrozen.__iter__cCs
t|j�S)N)�lenre)r^rrr�__len__	szFrozen.__len__cCs
||jkS)N)re)r^rgrrr�__contains__szFrozen.__contains__cCsdt|�j|jfS)Nz%s(%r))�typerre)r^rrr�__repr__szFrozen.__repr__N)rrr rcr]rfrIrhrjrkrmrrrrrd�srdcOstt||��S)N)rdr)rrrrr�FrozenOrderedDictsrnc@s`eZdZdZdgZddd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dS)�SortedKeysDictz�An wrapper for dictionary-like objects that always iterates over its
    items in sorted order by key but is otherwise equivalent to the underlying
    mapping.
    reNcCs|dkrin||_dS)N)re)r^rerrrrfszSortedKeysDict.__init__cCs
|j|S)N)re)r^rgrrrrI!szSortedKeysDict.__getitem__cCs||j|<dS)N)re)r^rgrKrrr�__setitem__$szSortedKeysDict.__setitem__cCs|j|=dS)N)re)r^rgrrr�__delitem__'szSortedKeysDict.__delitem__cCstt|j��S)N)r8�sortedre)r^rrrrh*szSortedKeysDict.__iter__cCs
t|j�S)N)rire)r^rrrrj-szSortedKeysDict.__len__cCs
||jkS)N)re)r^rgrrrrk0szSortedKeysDict.__contains__cCsdt|�j|jfS)Nz%s(%r))rlrre)r^rrrrm3szSortedKeysDict.__repr__cCst|�|jj��S)N)rlre�copy)r^rrrrs6szSortedKeysDict.copy)N)rrr rcr]rfrIrprqrhrjrkrmrsrrrrros
roc@sFeZdZdZdgZdd�Zdd�Zdd�Zd	d
�Zdd�Z	d
d�Z
dS)�ChainMapz�Partial backport of collections.ChainMap from Python>=3.3

    Don't return this from any public APIs, since some of the public methods
    for a MutableMapping are missing (they will raise a NotImplementedError)
    �mapscGs
||_dS)N)ru)r^rurrrrfBszChainMap.__init__cCs:x,|jD]"}y||Stk
r(YqXqWt|��dS)N)ru�KeyError)r^rgrerrrrIEs
zChainMap.__getitem__cCs||jd|<dS)Nr)ru)r^rgrKrrrrpMszChainMap.__setitem__cCst�dS)N)�NotImplementedError)r^rKrrrrqPszChainMap.__delitem__ccs@t�}x4|jD]*}x$|D]}||kr|V|j|�qWqWdS)N)�setru�add)r^�seenre�itemrrrrhSs
zChainMap.__iter__cCstt|���dS)N)rir8)r^rrrrj[szChainMap.__len__N)rrr rcr]rfrIrprqrhrjrrrrrt:srtc@s0eZdZdZedd��Zedd��Zdd�ZdS)	�NdimSizeLenMixinz�Mixin class that extends a class that defines a ``shape`` property to
    one that also defines ``ndim``, ``size`` and ``__len__``.
    cCs
t|j�S)N)rir2)r^rrrrQcszNdimSizeLenMixin.ndimcCsttj|j��S)N)�intr,�prodr2)r^rrr�sizegszNdimSizeLenMixin.sizecCs,y
|jdStk
r&td��YnXdS)Nrzlen() of unsized object)r2�
IndexErrorrT)r^rrrrjls
zNdimSizeLenMixin.__len__N)rrr rc�propertyrQrrjrrrrr|_sr|c@sBeZdZdZedd��Zedd��Zd
dd�Zd	d
�Zdd�Z	dS)�NDArrayMixinaMixin class for making wrappers of N-dimensional arrays that conform to
    the ndarray interface required for the data argument to Variable objects.

    A subclass should set the `array` property and override one or more of
    `dtype`, `shape` and `__getitem__`.
    cCs|jjS)N)r.r$)r^rrrr$zszNDArrayMixin.dtypecCs|jjS)N)r.r2)r^rrrr2~szNDArrayMixin.shapeNcCstj|d|d�S)N.)r$)r,r-)r^r$rrr�	__array__�szNDArrayMixin.__array__cCs
|j|S)N)r.)r^rgrrrrI�szNDArrayMixin.__getitem__cCsdt|�j|jfS)Nz%s(array=%r))rlrr.)r^rrrrm�szNDArrayMixin.__repr__)N)
rrr rcr�r$r2r�rIrmrrrrr�ss
r�ccs.y
dVWntk
r(|j��YnXdS)z�Context manager to ensure that a file opened by xarray is closed if an
    exception is raised before the user sees the file object.
    N)�	Exception�close)�frrr�close_on_error�s

r�cCsttjd|��S)Nz^https?\://)�bool�re�search)�pathrrr�
is_remote_uri�sr�cKs0tj|td�}tj|�}tj|j�|j�f|�S)z�Return True if values of an array are uniformly spaced and sorted.

    >>> is_uniform_spaced(range(5))
    True
    >>> is_uniform_spaced([-4, 0, 100])
    False

    kwargs are additional arguments to ``np.isclose``
    )r$)r,r.�floatZdiff�isclose�min�max)ZarrrZdiffsrrr�is_uniform_spaced�s

r�cCs&yt|�Wntk
r dSXdS)z$Determine whether `v` can be hashed.FT)�hashrT)rErrr�hashable�s
r�)r)4rc�
contextlib�datetimerr:r�r
�collectionsrrZnumpyr,Zpandasr'�rZpycompatrrrrr"r0r4r7r?rFrHrLrNrP�date�	timedeltarRrSrUrXrYrZr+r[rdrnrortr|r��contextmanagerr�r�r�r�rrrr�<module>sP
	

	
	#%