a
    ߙfb                      @   sx   d dl mZ d dlmZ dgZG dd dZeddd Zedd	d
 Z	eddd Z
eddd ZdS )    N)classproperty	Parameterc                   @   s   e Zd ZdZi Zd#dg dddddZ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dZdd Zedd Zdd Zdd Zed%dd Zd!d" ZdS )&r   a  Cosmological parameter (descriptor).

    Should only be used with a :class:`~astropy.cosmology.Cosmology` subclass.

    Parameters
    ----------
    fvalidate : callable[[object, object, Any], Any] or str, optional
        Function to validate the Parameter value from instances of the
        cosmology class. If "default", uses default validator to assign units
        (with equivalencies), if Parameter has units.
        For other valid string options, see ``Parameter._registry_validators``.
        'fvalidate' can also be set through a decorator with
        :meth:`~astropy.cosmology.Parameter.validator`.
    doc : str or None, optional
        Parameter description.
    unit : unit-like or None (optional, keyword-only)
        The `~astropy.units.Unit` for the Parameter. If None (default) no
        unit as assumed.
    equivalencies : `~astropy.units.Equivalency` or sequence thereof
        Unit equivalencies for this Parameter.
    fmt : str (optional, keyword-only)
        `format` specification, used when making string representation
        of the containing Cosmology.
        See https://docs.python.org/3/library/string.html#formatspec
    derived : bool (optional, keyword-only)
        Whether the Parameter is 'derived', default `False`.
        Derived parameters behave similarly to normal parameters, but are not
        sorted by the |Cosmology| signature (probably not there) and are not
        included in all methods. For reference, see ``Ode0`` in
        ``FlatFLRWMixin``, which removes :math:`\Omega_{de,0}`` as an
        independent parameter (:math:`\Omega_{de,0} \equiv 1 - \Omega_{tot}`).

    Examples
    --------
    For worked examples see :class:`~astropy.cosmology.FLRW`.
    defaultNz.3gF)unitequivalenciesfmtderivedc                C   s   t |r
nJ|| jv r | j| }n4t|tr@td| j  ntd| j  || _|| _|d urrt	
|nd | _|| _t|| _|| _d S )Nz `fvalidate`, if str, must be in z"`fvalidate` must be a function or )callable_registry_validators
isinstancestr
ValueErrorkeys	TypeError__doc__
_fvalidateuZUnit_unit_equivalencies_fmt_derived)self	fvalidatedocr   r   r   r    r   :lib/python3.9/site-packages/astropy/cosmology/parameter.py__init__1   s"    



zParameter.__init__c                 C   s   || _ d| | _d S )N_)
_attr_name_attr_name_private)r   	cosmo_clsnamer   r   r   __set_name__J   s    zParameter.__set_name__c                 C   s   | j S )zParameter name.)r   r   r   r   r   r!   O   s    zParameter.namec                 C   s   | j S )zParameter unit.)r   r#   r   r   r   r   T   s    zParameter.unitc                 C   s   | j S )z/Equivalencies used when initializing Parameter.)r   r#   r   r   r   r   Y   s    zParameter.equivalenciesc                 C   s   | j S )zString format specification.)r   r#   r   r   r   format_spec^   s    zParameter.format_specc                 C   s   | j S )z:Whether the Parameter is derived; true parameters are not.)r   r#   r   r   r   r   c   s    zParameter.derivedc                 C   s   |d u r| S t || jS )N)getattrr   )r   	cosmologyr    r   r   r   __get__k   s    zParameter.__get__c                 C   s2   t || jrtd| ||}t|| j| dS )zBAllows attribute setting once. Raises AttributeError subsequently.zcan't set attributeN)hasattrr   AttributeErrorvalidatesetattrr   r&   valuer   r   r   __set__q   s    zParameter.__set__c                 C   s   | j S )z:Function to validate a potential value of this Parameter..)r   r#   r   r   r   r   ~   s    zParameter.fvalidatec                 C   s&   t | || j| j| j| j| jd}|S )ao  Make new Parameter with custom ``fvalidate``.

        Note: ``Parameter.fvalidator`` must be the top-most descriptor decorator.

        Parameters
        ----------
        fvalidate : callable[[type, type, Any], Any]

        Returns
        -------
        `~astropy.cosmology.Parameter`
            Copy of this Parameter but with custom ``fvalidate``.
        )r   r   r   r   r   r   )typer   r$   r   r   r   )r   r   Zdescr   r   r   	validator   s    zParameter.validatorc                 C   s   |  || |S )af  Run the validator on this Parameter.

        Parameters
        ----------
        cosmology : `~astropy.cosmology.Cosmology` instance
        value : Any
            The object to validate.

        Returns
        -------
        Any
            The output of calling ``fvalidate(cosmology, self, value)``
            (yes, that parameter order).
        r   r,   r   r   r   r*      s    zParameter.validatec                    sB    j v rtdd|dur0| j < |S  fdd}|S )a  Decorator to register a new kind of validator function.

        Parameters
        ----------
        key : str
        fvalidate : callable[[object, object, Any], Any] or None, optional
            Value validation function.

        Returns
        -------
        ``validator`` or callable[``validator``]
            if validator is None returns a function that takes and registers a
            validator. This allows ``register_validator`` to be used as a
            decorator.
        z
validator z# already registered with Parameter.Nc                    s   |  j < | S )zRegister validator function.

            Parameters
            ----------
            fvalidate : callable[[object, object, Any], Any]
                Validation function.

            Returns
            -------
            ``validator``
            )r
   r1   clskeyr   r   register   s    
z.Parameter.register_validator.<locals>.register)r
   KeyError)r3   r4   r   r5   r   r2   r   register_validator   s    

zParameter.register_validatorc                 C   s   d| j dtt|  dS )Nz<Parameter z at >)r   hexidr#   r   r   r   __repr__   s    zParameter.__repr__)r   N)N)N)__name__
__module____qualname__r   r
   r   r"   propertyr!   r   r   r$   r   r'   r.   r   r0   r*   classmethodr7   r;   r   r   r   r   r   	   s2   %






,r   c                 C   sH   |j durDt|j t||j }W d   n1 s:0    Y  |S )z]
    Default Parameter value validator.
    Adds/converts units if Parameter has a unit.
    N)r   r   Zadd_enabled_equivalenciesr   ZQuantityr&   Zparamr-   r   r   r   _validate_with_unit   s    
,rB   floatc                 C   s   t | ||}t|S )z=Parameter value validator with units, and converted to float.)rB   rC   rA   r   r   r   _validate_to_float   s    rD   Zscalarc                 C   s&   t | ||}|js"t|j d|S ) z is a non-scalar quantity)rB   Zisscalarr   r!   rA   r   r   r   _validate_to_scalar   s    rF   znon-negativec                 C   s(   t | ||}|dk r$t|j d|S )z:Parameter value validator where value is a positive float.g        z cannot be negative.)rD   r   r!   rA   r   r   r   _validate_non_negative   s    rG   )Zastropy.unitsZunitsr   Zastropy.utils.decoratorsr   __all__r   r7   rB   rD   rF   rG   r   r   r   r   <module>   s    U


