Exemple #1
0
def test_parse_expected():
    """Test that we can parse the units that we expect to be able to."""
    expected = [
        "degC",
        "degF",
        "K",
        "g",
        "kg",
        "mg",
        "ton",
        "L",
        "mL",
        "inch",
        "ft",
        "mm",
        "um",
        "second",
        "ms",
        "hour",
        "minute",
        "ns",
        "g/cm^3",
        "g/mL",
        "kg/cm^3",
        _ureg("kg").u,
        "amu"  # A line that was edited
    ]
    for unit in expected:
        parse_units(unit)
Exemple #2
0
def test_parse_unexpected():
    """Test that we cannot parse the units that we do not expect to."""
    unexpected = [
        "gibberish",
        5,
        "cp",  # Removed because of risk of collision with cP
        "chain"  # Survey units eliminated
    ]
    for unit in unexpected:
        with pytest.raises(UndefinedUnitError):
            parse_units(unit)
Exemple #3
0
def _parse_value(val):
    """Example field-parsing logic."""
    # If the string is complicated, split it up and try to get uncertainty and/or units
    if isinstance(val, str) and len(val.split()) > 1:
        toks = val.split()
        mean = float(toks[0])
        std = -1
        if toks[1] in {"+-", "+/-"}:
            std = float(toks[2])

        try:
            unit = units.parse_units(toks[-1])
        except (ValueError, units.UndefinedUnitError):
            print("Couldn't find {}".format(toks[-1]))
            unit = ''

        if std >= 0:
            return NormalReal(mean=mean, std=std, units=unit)
        else:
            return NominalReal(mean, units=unit)
    # if it is just a number wrap it in a nominal value
    elif isinstance(val, (float, int)):
        return NominalReal(val, '')
    # if it is a single string, its either a single number of a category
    elif isinstance(val, str):
        try:
            num = float(val)
            return NominalReal(num, '')
        except ValueError:
            return DiscreteCategorical(val)
    else:
        raise ValueError("Couldn't parse {}".format(val))
def test_parse_expected():
    """Test that we can parse the units that we expect to be able to."""
    expected = [
        "degC", "degF", "K",
        "g", "kg", "mg", "ton",
        "L", "mL",
        "inch", "ft", "mm", "um",
        "second", "ms", "hour", "minute", "ns",
        "g/cm^3", "g/mL", "kg/cm^3",
        _ureg("kg").u,
        "amu",  # A line that was edited
        "Seconds",  # Added support for some title-case units
        "delta_Celsius / hour"  # Added to make sure pint version is right (>0.10)
    ]
    for unit in expected:
        parse_units(unit)
    assert parse_units("") == 'dimensionless'
def test_quantities():
    """Exercise the expressions on quantity in add_edge."""
    ing_one = add_edge(make_node("Input"),
                       make_node("Output"),
                       mass_fraction=0.1,
                       number_fraction=0.2,
                       volume_fraction=0.3,
                       absolute_quantity=0.4,
                       absolute_units='kg')

    assert ing_one.mass_fraction.nominal == 0.1, "Mass fraction got set."
    assert ing_one.number_fraction.nominal == 0.2, "Number fraction got set."
    assert ing_one.volume_fraction.nominal == 0.3, "Volume fraction got set."
    assert ing_one.absolute_quantity.nominal == 0.4, "Absolute quantity got set."
    assert ing_one.absolute_quantity.units == parse_units(
        'kg'), "Absolute units got set."

    ing_two = add_edge(make_node("Input"),
                       make_node("Output"),
                       mass_fraction=NominalReal(0.5, ''),
                       number_fraction=NominalReal(0.6, ''),
                       volume_fraction=NominalReal(0.7, ''),
                       absolute_quantity=NominalReal(0.8, 'liters'))

    assert ing_two.mass_fraction.nominal == 0.5, "Mass fraction got set."
    assert ing_two.number_fraction.nominal == 0.6, "Number fraction got set."
    assert ing_two.volume_fraction.nominal == 0.7, "Volume fraction got set."
    assert ing_two.absolute_quantity.nominal == 0.8, "Absolute quantity got set."
    assert ing_two.absolute_quantity.units == parse_units(
        'liters'), "Absolute units got set."

    with pytest.raises(ValueError):
        add_edge(make_node("Input"),
                 make_node("Output"),
                 absolute_quantity=0.4)
    with pytest.raises(ValueError):
        add_edge(make_node("Input"),
                 make_node("Output"),
                 absolute_quantity=NominalReal(0.8, 'liters'),
                 absolute_units='liters')
 def units(self, units: str):
     if units is None:
         raise ValueError(
             "Continuous values must have units. "
             "Use an empty string for a dimensionless quantity.")
     self._units = parse_units(units)
Exemple #7
0
 def default_units(self, default_units):
     if default_units is None:
         raise ValueError(
             "Real bounds must have units. "
             "Use an empty string for a dimensionless quantity.")
     self._default_units = units.parse_units(default_units)
def test_parse_none():
    """Test that None parses as None."""
    assert parse_units(None) is None
Exemple #9
0
def test_parse_unexpected():
    """Test that we cannot parse the units that we do not expect to."""
    unexpected = ["gibberish", 5]
    for unit in unexpected:
        with pytest.raises(UndefinedUnitError):
            parse_units(unit)