コード例 #1
0
ファイル: range_test.py プロジェクト: simsong/ctools
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)]
コード例 #2
0
 def add_valid_data_description(self, desc):
     """Parse the variable descriptions typical of US Census Bureau data
     files that are used to describe valid values."""
     if "\n" in desc:
         for line in desc.split("\n"):
             self.add_valid_data_description(line)
         return
     if "," in desc:
         for line in desc.split(","):
             self.add_valid_data_description(line)
         return
     r = Range.extract_range_and_desc(desc, width=self.width)
     self.ranges.add(r)
コード例 #3
0
ファイル: variable.py プロジェクト: simsong/ctools
    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"
コード例 #4
0
ファイル: range_test.py プロジェクト: simsong/ctools
def test_parse():
    res = Range.extract_range_and_desc("1 hello", hardfail=True)
    assert type(res) == Range
    assert res.a == '1'
    assert res.b == '1'
    assert res.desc == "hello"

    assert Range.extract_range_and_desc("1 hello",
                                        python_type=int,
                                        hardfail=True) == Range(1, 1, "hello")
    assert Range.extract_range_and_desc("1-2 hello",
                                        python_type=int,
                                        hardfail=True) == Range(1, 2, "hello")
    assert Range.extract_range_and_desc("1-2 = hello",
                                        python_type=int,
                                        hardfail=True) == Range(1, 2, "hello")
    assert Range.extract_range_and_desc("1-2 = (hello)",
                                        python_type=int,
                                        hardfail=True) == Range(1, 2, "hello")

    assert Range.extract_range_and_desc("1 hello 3-4",
                                        python_type=int,
                                        hardfail=True) == Range(
                                            1, 1, "hello 3-4")
    assert Range.extract_range_and_desc("1 (hello 3-4)",
                                        python_type=int,
                                        hardfail=True) == Range(
                                            1, 1, "hello 3-4")
    assert Range.extract_range_and_desc("1 = hello 3-4",
                                        python_type=int,
                                        hardfail=True) == Range(
                                            1, 1, "hello 3-4")
    assert Range.extract_range_and_desc("1 = (hello 3-4)",
                                        python_type=int,
                                        hardfail=True) == Range(
                                            1, 1, "hello 3-4")

    assert Range.extract_range_and_desc("1-2 hello 3-4",
                                        python_type=int,
                                        hardfail=True) == Range(
                                            1, 2, "hello 3-4")
    assert Range.extract_range_and_desc("1-2 (hello 3-4)",
                                        python_type=int,
                                        hardfail=True) == Range(
                                            1, 2, "hello 3-4")
    assert Range.extract_range_and_desc("1-2 = hello 3-4",
                                        python_type=int,
                                        hardfail=True) == Range(
                                            1, 2, "hello 3-4")
    assert Range.extract_range_and_desc("1-2 = (hello 3-4)",
                                        python_type=int,
                                        hardfail=True) == Range(
                                            1, 2, "hello 3-4")

    assert Range.extract_range_and_desc("      1 = hello 3-4",
                                        python_type=int,
                                        hardfail=True) == Range(
                                            1, 1, "hello 3-4")

    # A few descriptions that we don't want to parse as Ranges
    assert Range.extract_range_and_desc("Up to 22 values",
                                        python_type=int,
                                        hardfail=False) == None
    assert Range.extract_range_and_desc(
        "Fips State Code (The legal values for this file are 01-02, 04-06)",
        python_type=int,
        hardfail=False) == None
コード例 #5
0
ファイル: range_test.py プロジェクト: simsong/ctools
def test_range_funcs():
    r1 = Range(1, 1)
    r2 = Range(1, 1)
    assert r1 == r2
    r3 = Range(2, 2)
    assert r1 < r3
コード例 #6
0
ファイル: variable.py プロジェクト: simsong/ctools
 def python_validation_text(self):
     ranges = Range.combine_ranges(self.ranges)
     return ", ".join(['{}-{}'.format(r.a, r.b) for r in ranges])