Python float or none

13 Python code examples are found related to " float or none". You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: types.py    From bhmm with GNU Lesser General Public License v3.0 6 votes vote down vote up
def ensure_float_vector_or_None(F, require_order=False):
    """Ensures that F is either None, or a numpy array of floats

    If F is already either None or a numpy array of floats, F is returned (no copied!)
    Otherwise, checks if the argument can be converted to an array of floats and does that.

    Parameters
    ----------
    F: float, list of float or 1D-ndarray of float

    Returns
    -------
    arr : ndarray(n)
        numpy array with the floats contained in the argument

    """
    if F is None:
        return F
    else:
        return ensure_float_vector(F, require_order=require_order) 
Example 2
Source File: types.py    From espnet with Apache License 2.0 6 votes vote down vote up
def float_or_none(value: str) -> Optional[float]:
    """float_or_none.

    Examples:
        >>> import argparse
        >>> parser = argparse.ArgumentParser()
        >>> _ = parser.add_argument('--foo', type=float_or_none)
        >>> parser.parse_args(['--foo', '4.5'])
        Namespace(foo=4.5)
        >>> parser.parse_args(['--foo', 'none'])
        Namespace(foo=None)
        >>> parser.parse_args(['--foo', 'null'])
        Namespace(foo=None)
        >>> parser.parse_args(['--foo', 'nil'])
        Namespace(foo=None)

    """
    if value.strip().lower() in ("none", "null", "nil"):
        return None
    return float(value) 
Example 3
Source File: converters.py    From neutron-lib with Apache License 2.0 6 votes vote down vote up
def convert_to_positive_float_or_none(val):
    """Converts a value to a python float if the value is positive.

    :param val: The value to convert to a positive python float.
    :returns: The value as a python float. If the val is None, None is
        returned.
    :raises ValueError, InvalidInput: A ValueError is raised if the 'val'
        is a float, but is negative. InvalidInput is raised if 'val' can't be
        converted to a python float.
    """
    # NOTE(salv-orlando): This conversion function is currently used by
    # a vendor specific extension only at the moment  It is used for
    # port's RXTX factor in neutron.plugins.vmware.extensions.qos.
    # It is deemed however generic enough to be in this module as it
    # might be used in future for other API attributes.
    if val is None:
        return
    try:
        val = float(val)
        if val < 0:
            raise ValueError()
    except (ValueError, TypeError):
        msg = _("'%s' must be a non negative decimal") % val
        raise n_exc.InvalidInput(error_message=msg)
    return val 
Example 4
Source File: utils.py    From tvalacarta with GNU General Public License v3.0 5 votes vote down vote up
def float_or_none(v, scale=1, invscale=1, default=None):
    if v is None:
        return default
    try:
        return float(v) * invscale / scale
    except (ValueError, TypeError):
        return default 
Example 5
Source File: utils.py    From youtube-dl-GUI with MIT License 5 votes vote down vote up
def float_or_none(v, scale=1, invscale=1, default=None):
    if v is None:
        return default
    try:
        return float(v) * invscale / scale
    except ValueError:
        return default 
Example 6
Source File: validation.py    From ob2 with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def float_or_none(s):
    if not s:
        return None
    else:
        return float(s) 
Example 7
Source File: utils.py    From streams with MIT License 5 votes vote down vote up
def float_or_none(item):
    """
    Tries to convert ``item`` to :py:func:`float`. If it is not possible,
    returns ``None``.

    :param object item: Element to convert into :py:func:`float`.

    >>> float_or_none(1)
    ... 1.0
    >>> float_or_none("1")
    ... 1.0
    >>> float_or_none("smth")
    ... None
    """
    if isinstance(item, float):
        return item
    try:
        return float(item)
    except:
        return None


# noinspection PyBroadException 
Example 8
Source File: common.py    From artview with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def float_or_none(text):
    if text=="None" or text=="none" or text=="NONE":
        return None
    else:
        return float(text) 
Example 9
Source File: ch17_ex1.py    From Mastering-Object-Oriented-Python-Second-Edition with MIT License 5 votes vote down vote up
def float_or_none(text: str) -> Optional[float]:
    if len(text) == 0:
        return None
    return float(text)


# TestCase with only one test method 
Example 10
Source File: koradserial.py    From py-korad-serial with MIT License 5 votes vote down vote up
def float_or_none(value):
    try:
        return float(value)
    except (TypeError, ValueError):
        return None 
Example 11
Source File: args_utils.py    From mmvt with GNU General Public License v3.0 5 votes vote down vote up
def float_or_none(val):
    try:
        return float(val)
    except:
        return None 
Example 12
Source File: config_helper.py    From sagemaker-rl-container with Apache License 2.0 5 votes vote down vote up
def get_float_or_none(cls, params, key):
        """
        Retrieves a float value from configuration dictionary.
        Value must either be a float or be convertible to a float.
        Returns None if key is not present in the params dictionary.
        Raises a CustomerValueError if provided value is not a float or cannot be converted to a float.

        :param params: configuration dictionary
        :param key: key to use for value retrieval
        :return: integer value
        """
        try:
            return cls.get_float(params, key)
        except KeyError:
            return None