Beispiel #1
0
def test_combine_ranges():
    r1 = Range(1, 2)
    r2 = Range(2, 4)
    r3 = Range(10, 12)
    l1 = [r1, r2, r3]
    assert Range.combine_ranges([r1, r2]) == [Range(1, 4)]
    assert Range.combine_ranges([r1, r2, r3]) == [Range(1, 4), Range(10, 12)]
Beispiel #2
0
    def python_validator(self):
        ret = []
        ret.append("    @classmethod")
        ret.append("    def {}(self,x):".format(self.python_validator_name()))
        ret.append('        """{}"""'.format(self.desc))
        if self.allow_any:
            ret.append("        return True")
            return "\n".join(ret) + "\n"
        if self.allow_null:
            ret.append("        if x is None or x == \"None\" or len(x) == 0:")
            ret.append("            return True")
        else:
            ret.append("        if x is None or x == \"None\":")
            ret.append("            return False")
        if self.allow_whitespace:
            size = "".rjust(self.width, " ")
            ret.append("        if x == '{}':".format(size))
            ret.append("            return True")

        if self.python_type == int or self.python_type == float:
            ret.append('        x = str(x).strip()')
            ret.append('        try:')
            if self.python_type == int:
                ret.append('            x = int(x)')
            if self.python_type == float:
                ret.append('            x = float(x)')
            ret.append('        except ValueError:')
            ret.append('            return False')
        ranges = Range.combine_ranges(self.ranges)
        try:
            expr = " or ".join([
                "({})".format(r.python_expr(self.python_type, self.width))
                for r in ranges
            ])
        except ValueError as e:
            logging.error(
                "Cannot create python range expression for variable " +
                str(self))
            raise RuntimeError(
                "Cannot create python range expression for variable " +
                str(self))

        if expr == "":
            expr = "True"
        ret.append("        return " + expr)
        return "\n".join(ret) + "\n"
Beispiel #3
0
 def python_validation_text(self):
     ranges = Range.combine_ranges(self.ranges)
     return ", ".join(['{}-{}'.format(r.a, r.b) for r in ranges])