B
    ~‹dÔ  ã               @   sn   d Z ddlZddlZddlZddlmZ ddlmZ dZdd„ Z	e
j e	_ dd	„ Zd
d„ Zddd„Zdd„ ZdS )z Miscellaneous utilties for GWpy
é    N)ÚOrderedDict)Únullcontextz(Duncan Macleod <duncan.macleod@ligo.org>c              O   s,   |  dtj¡ |d }t| |Ž | ¡  d S )NÚfile)Ú
setdefaultÚsysÚstdoutÚprintÚflush)ÚvaluesÚkwargsÚfile_© r   ú\/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/gwpy/utils/misc.pyÚgprint    s    
r   c               C   s   t  dt¡ tƒ S )zNull context manager
    z§gwpy.utils.null_context is deprecated and will be removed in GWpy 3.1.0, please update your code to use contextlib.nullcontext from the Python standard library (>=3.7))ÚwarningsÚwarnÚDeprecationWarningr   r   r   r   r   Únull_context*   s    r   c             C   s   |dkrdS | |ƒS )zÄApply func to value if value is not None

    Examples
    --------
    >>> from gwpy.utils.misc import if_not_none
    >>> if_not_none(int, '1')
    1
    >>> if_not_none(int, None)
    None
    Nr   )ÚfuncÚvaluer   r   r   Úif_not_none6   s    r   é   c             C   sR   |dkrt j}n&|dkr t j}n|dk	r2tdƒ‚nt}t|ƒ||t  | |¡ƒ ƒS )a  Round a positive value to the nearest integer power of `base`

    Parameters
    ----------
    x : scalar
        value to round, must be strictly positive

    base : scalar, optional
        base to whose power `x` will be rounded, default: 2

    which : `str` or `NoneType`, optional
        which value to round to, must be one of `'lower'`, `'upper'`, or
        `None` to round to whichever is nearest, default: `None`

    Returns
    -------
    rounded : scalar
        the rounded value

    Notes
    -----
    The object returned will be of the same type as `base`.

    Examples
    --------
    >>> from gwpy.utils.misc import round_to_power
    >>> round_to_power(2)
    2
    >>> round_to_power(9, base=10)
    10
    >>> round_to_power(5, which='lower')
    4
    ÚlowerÚupperNz9'which' argument must be one of 'lower', 'upper', or None)ÚmathÚfloorÚceilÚ
ValueErrorÚroundÚtypeÚlog)ÚxÚbaseÚwhichÚselectorr   r   r   Úround_to_powerF   s    "
r%   c             C   s   t t | ¡ ¡ ƒS )zïReturn a version of the input list with unique elements,
    preserving order

    Examples
    --------
    >>> from gwpy.utils.misc import unique
    >>> unique(['b', 'c', 'a', 'a', 'd', 'e', 'd', 'a'])
    ['b', 'c', 'a', 'd', 'e']
    )Úlistr   ÚfromkeysÚkeys)Úlist_r   r   r   Úuniquet   s    
r*   )r   N)Ú__doc__r   r   r   Úcollectionsr   Ú
contextlibr   Ú
__author__r   r   r   r   r%   r*   r   r   r   r   Ú<module>   s   
.