sasdata.quantities.unit_parser module

sasdata.quantities.unit_parser.find_named_unit(unit: Unit, rtol: float = 1e-14) NamedUnit | None

Find a named unit matching the one provided

sasdata.quantities.unit_parser.known_mistake(unit_str: str) Unit | None

Take known broken units from historical files and give them a reasonible parse

sasdata.quantities.unit_parser.parse(string: str, name_lookup: bool = True, longest_unit: bool = True, lookup_rtol: float = 1e-14)
sasdata.quantities.unit_parser.parse_named_unit(unit_string: str, rtol: float = 1e-14) NamedUnit

Parses unit into a named unit. Parses unit into a Unit if it is not already, and then finds an equivaelent named unit. Please note that this might not be the expected unit from the string itself. E.g. ‘kgm/2’ will become newtons.

Parameters:
  • unit_string – string describing the units, e.g. km/s

  • rtol – relative tolerance for matching scale factors

sasdata.quantities.unit_parser.parse_named_unit_from_group(unit_str: str, from_group: UnitGroup) NamedUnit

Parses unit_str into a named unit. The named unit found must be part of from_group. If two units are found, the unit that is present in from_group is returned. This is useful in cases of ambiguities.

sasdata.quantities.unit_parser.parse_single_unit(unit_str: str, unit_group: UnitGroup | None = None, longest_unit: bool = True) tuple[Unit | None, str]

Attempts to find a single unit for unit_str. Return this unit, and the remaining string in a tuple. If a unit cannot be parsed, the unit will be None, and the remaining string will be the entire unit_str.

The shortest_unit parameter specifies how to resolve ambiguities. If it is true, then it will parse the longest unit available. Otherwise, it will stop parsing as soon as it has found any unit.

If unit_group is set, it will only try to parse units within that group. This is useful for resolving ambiguities.

sasdata.quantities.unit_parser.parse_unit(unit_str: str, longest_unit: bool = True) Unit

Parse unit_str into a unit.

sasdata.quantities.unit_parser.parse_unit_from_group(unit_str: str, from_group: UnitGroup) Unit | None

Tries to use the given unit group to resolve ambiguities. Parse a unit twice with different options, and returns whatever conforms to the unit group.

sasdata.quantities.unit_parser.parse_unit_stack(unit_str: str, longest_unit: bool = True) list[Unit]

Split unit_str into a stack of parsed units.

sasdata.quantities.unit_parser.parse_unit_strs(unit_str: str, current_units: list[Unit] | None = None, longest_unit: bool = True) list[Unit]

Recursively parse units from unit_str until no more characters are present.

sasdata.quantities.unit_parser.split_unit_str(unit_str: str) list[str]

Separate the letters from the numbers in unit_str

sasdata.quantities.unit_parser.validate_unit_str(unit_str: str) bool

Validate whether unit_str is valid. This doesn’t mean that the unit specified in unit_str exists but rather it only consists of letters, and numbers as a unit string should.