ts_lib_parsy_components.value_unit module#
- class ValueUnitParser(unit_parser: ~ts_lib_units.UnitParser, value_parser: ~ts_lib_parsy.Parser[str] = <ts_lib_parsy.Parser object>, raw_unit_parser: ~ts_lib_parsy.Parser[str] = <ts_lib_parsy.Parser object>, float_converter: ~typing.Callable[[str], float | None] = <function to_float>, default_unit: str = Unit.ARBITRARY_UNIT)[source]#
Bases:
objectParsers for raw value unit strings
- Parameters:
unit_parser – A UnitParser object.
value_fn – A parser for the raw value string.
unit_fn – A parser for the raw unit string.
float_converter – A function to convert the raw value string to a float.
default_unit – The default unit to use if no unit is found.
- unit_parser: UnitParser#
- value_parser: Parser[str] = <ts_lib_parsy.Parser object>#
- raw_unit_parser: Parser[str] = <ts_lib_parsy.Parser object>#
- float_converter() float | None#
Converts given value to its equivalent float.
- Parameters:
value – A string to convert
- Returns:
The float value of the input string. None if the string could not be parsed to float
- default_unit: str = 'ArbitraryUnit'#
- value_optional_unit() Parser[RawValueUnit][source]#
Create a parser for RawValueUnit objects where the unit is optional with a default.
- Returns:
A parser for RawValueUnit objects.
- value_unit(raw_unit: str | None = None) Parser[RawValueUnit][source]#
Create a parser for RawValueUnit objects, with a required unit.
By default this function assumes that the unit appears after the value in the stream.
If the unit appears elsewhere in the file, the raw_unit kwarg can be provided. The returned parser will parse the value string from the stream, but the input raw_unit will be used as the raw unit string, instead of parsing it from the stream.
- Keyword Arguments:
(default (raw_unit -- A raw unit string to accompany with value) – {None})
- Returns:
A parser for RawValueUnit objects.
- value(unit: Unit | None = None) Parser[RawValueUnit][source]#
Create a parser for RawValueUnit objects, with a default unit.
By default the unit is taken from the default_unit attribute.
If you wish to override this default, you can provide a unit kwarg.
- Keyword Arguments:
(default (unit -- The unit to store alongside the value) – {None})
- Returns:
A parser for RawValueUnit objects.