Beispiel #1
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)
Beispiel #2
0
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