Initialize a FloatWithUnit from a string. Example Memory.from_string("1. Mb")

def from_string(cls, s):
        """
        Initialize a FloatWithUnit from a string. Example Memory.from_string("1. Mb")
        """
        # Extract num and unit string.
        s = s.strip()
        for i, char in enumerate(s):
            if char.isalpha() or char.isspace():
                break
        else:
            raise Exception("Unit is missing in string %s" % s)
        num, unit = float(s[:i]), s[i:]

        # Find unit type (set it to None if it cannot be detected)
        for unit_type, d in BASE_UNITS.items():
            if unit in d:
                break
        else:
            unit_type = None

        return cls(num, unit, unit_type=unit_type)