Skip to content

ScientificFloatValidator

Qt Base Class: QValidator

Signature: QValidator(self, parent: Optional[PySide6.QtCore.QObject] = None) -> None

Base classes

Name Children Inherits
BaseScientificValidator
prettyqt.validators.scientificvalidators

⋔ Inheritance diagram

graph TD
  1473367033552["validators.ScientificFloatValidator"]
  1473367015984["validators.BaseScientificValidator"]
  1473245568976["gui.Validator"]
  1473245536768["gui.ValidatorMixin"]
  1473299815024["core.ObjectMixin"]
  140713234304496["builtins.object"]
  1473289208592["QtGui.QValidator"]
  1473288842240["QtCore.QObject"]
  1473291690208["Shiboken.Object"]
  1473367015984 --> 1473367033552
  1473245568976 --> 1473367015984
  1473245536768 --> 1473245568976
  1473299815024 --> 1473245536768
  140713234304496 --> 1473299815024
  1473289208592 --> 1473245568976
  1473288842240 --> 1473289208592
  1473291690208 --> 1473288842240
  140713234304496 --> 1473291690208

🛈 DocStrings

Bases: BaseScientificValidator

This is a validator for float values represented as strings in scientific notation.

(i.e. "1.35e-9", ".24E+8", "14e3" etc.) Also supports SI unit prefix like 'M', 'n' etc.

Source code in prettyqt\validators\scientificvalidators.py
class ScientificFloatValidator(BaseScientificValidator):
    """This is a validator for float values represented as strings in scientific notation.

    (i.e. "1.35e-9", ".24E+8", "14e3" etc.)
    Also supports SI unit prefix like 'M', 'n' etc.
    """

    ID = "scientific_float"
    re_pattern = re.compile(
        r"(\s*([+-]?)(\d+\.\d+|\.\d+|\d+\.?)([eE][+-]?\d+)?\s?([YZEPTGMkmµunpfazy]?)\s*)",
        flags=re.UNICODE,
    )
    group_map = {"match": 0, "sign": 1, "mantissa": 2, "exponent": 3, "si": 4}

    def validate(self, string, position):
        if (
            string.strip() in "+.-."
            or string.strip() in list("YZEPTGMkmµunpfazy")
            or re.match(r"[+-]?(in$|i$)", string, re.IGNORECASE)
        ):
            return self.State.Intermediate, string, position

        # Accept input of [+-]inf. Not case sensitive.
        if re.match(r"[+-]?\binf$", string, re.IGNORECASE):
            return self.State.Acceptable, string.lower(), position

        if not (group_dict := self.get_group_dict(string)):
            return (
                (self.State.Intermediate, string, position)
                if string[position - 1] in "eE-+." and "i" not in string.lower()
                else (self.State.Invalid, "", position)
            )
        if group_dict["match"] == string:
            return self.State.Acceptable, string, position
        if string.count(".") > 1:
            return self.State.Invalid, group_dict["match"], position
        position = min(position, len(string))
        if string[position - 1] in "eE-+" and "i" not in string.lower():
            return self.State.Intermediate, string, position
        return self.State.Invalid, group_dict["match"], position

⌗ Property table

Qt Property Type Doc
objectName QString

Validator ID: scientific_float