Exemplo n.º 1
0
def test_group_f1():
    with pytest.raises(ValueError):
        unit_group('not-existing')
Exemplo n.º 2
0
def test_group_f1():
    unit_group('not-existing')
Exemplo n.º 3
0
def test_group(tbl):
    assert unit_group('kg', tbl) == 'mass'
    assert unit_group('eV', tbl) == 'energy'
    assert unit_group('N', tbl) == 'force'
Exemplo n.º 4
0
    def get(self, label, unit=None, default=None, with_unit=False):
        """ Retrieve fdf-keyword from the file

        Parameters
        ----------
        label : str
            the fdf-label to search for
        unit : str, optional
            unit of the physical quantity to return
        default : optional
            if the label is not found, this will be the returned value (default to ``None``)
        with_unit : bool, optional
            whether the physical quantity gets returned with the found unit in the fdf file.

        Returns
        -------
        value : the value of the fdf-label. If the label is a block, a `list` is returned, for
                a real value a `float` (or if the default is of `float`), for an integer, an
                `int` is returned.
        unit : if `with_unit` is true this will contain the associated unit if it is specified

        Examples
        --------
        >>> print(open(...).readlines()) # doctest: +SKIP
        LabeleV 1. eV # doctest: +SKIP
        LabelRy 1. Ry # doctest: +SKIP
        Label name # doctest: +SKIP
        FakeInt 1 # doctest: +SKIP
        %block Hello # doctest: +SKIP
        line 1 # doctest: +SKIP
        line2 # doctest: +SKIP
        %endblock # doctest: +SKIP
        >>> fdf.get('LabeleV') == 1. # default unit is eV # doctest: +SKIP
        >>> fdf.get('LabelRy') == unit.siesta.unit_convert('Ry', 'eV') # doctest: +SKIP
        >>> fdf.get('LabelRy', 'Ry') == 1. # doctest: +SKIP
        >>> fdf.get('LabelRy', with_unit=True) == (1., 'Ry') # doctest: +SKIP
        >>> fdf.get('FakeInt', default='0') == '1' # doctest: +SKIP
        >>> fdf.get('LabeleV', with_unit=True) == (1., 'eV') # doctest: +SKIP
        >>> fdf.get('Label', with_unit=True) == 'name' # no unit present on line # doctest: +SKIP
        >>> fdf.get('Hello') == ['line 1', 'line2'] # doctest: +SKIP
        """
        # Try and read a line
        value = self._read_label(label)

        # Simply return the default value if not found
        if value is None:
            return default

        # Figure out what it is
        t = self._type(value)

        # We will only do something if it is a real, int, or physical.
        # Else we simply return, as-is
        if t == 'r':
            if default is None:
                return float(value)
            t = type(default)
            return t(value)

        elif t == 'i':
            if default is None:
                return int(value)
            t = type(default)
            return t(value)

        elif t == 'p':
            value = value.split()
            if with_unit:
                # Simply return, as is. Let the user do whatever.
                return float(value[0]), value[1]
            if unit is None:
                default = unit_default(unit_group(value[1]))
            else:
                if unit_group(value[1]) != unit_group(unit):
                    raise ValueError(
                        "Requested unit for {} is not the same type. "
                        "Found/Requested {}/{}'".format(label, value[1], unit))
                default = unit
            return float(value[0]) * unit_convert(value[1], default)

        elif t == 'b':
            return value.lower() in _LOGICAL_TRUE

        return value