Beispiel #1
0
def update_stats_count(today: str) -> None:
    """Counts the # of all house numbers as of today."""
    statedir = config.get_abspath("workdir/stats")
    csv_path = os.path.join(statedir, "%s.csv" % today)
    count_path = os.path.join(statedir, "%s.count" % today)
    city_count_path = os.path.join(statedir, "%s.citycount" % today)
    house_numbers = set()
    cities: Dict[str, int] = {}
    first = True
    with open(csv_path, "r") as stream:
        for line in stream.readlines():
            if first:
                # Ignore the oneliner header.
                first = False
                continue
            cells = line.split("\t")
            # Ignore last column, which is the user who touched the object last.
            house_numbers.add("\t".join(cells[:4]))
            city_key = util.get_city_key(cells[0], cells[1])
            if city_key in cities:
                cities[city_key] += 1
            else:
                cities[city_key] = 1

    with open(count_path, "w") as stream:
        house_numbers_len = str(len(house_numbers))
        stream.write(house_numbers_len + "\n")

    with open(city_count_path, "w") as stream:
        for key, value in cities.items():
            stream.write(key + "\t" + str(value) + "\n")
Beispiel #2
0
def update_stats_count(today: str) -> None:
    """Counts the # of all house numbers as of today."""
    statedir = config.get_abspath("workdir/stats")
    csv_path = os.path.join(statedir, "%s.csv" % today)
    count_path = os.path.join(statedir, "%s.count" % today)
    city_count_path = os.path.join(statedir, "%s.citycount" % today)
    house_numbers = set()
    cities: Dict[str, Set[str]] = {}
    first = True
    valid_settlements = util.get_valid_settlements()
    with open(csv_path, "r") as stream:
        for line in stream.readlines():
            if first:
                # Ignore the oneliner header.
                first = False
                continue
            # postcode, city name, street name, house number, user
            cells = line.split("\t")
            # Ignore last column, which is the user who touched the object last.
            house_numbers.add("\t".join(cells[:4]))
            city_key = util.get_city_key(cells[0], cells[1], valid_settlements)
            city_value = "\t".join(cells[2:4])
            if city_key in cities:
                cities[city_key].add(city_value)
            else:
                cities[city_key] = set([city_value])
    write_count_path(count_path, house_numbers)
    write_city_count_path(city_count_path, cities)
Beispiel #3
0
 def test_happy(self) -> None:
     """Tests the happy path."""
     valid_settlements = set(["lábatlan"])
     self.assertEqual(util.get_city_key("1234", "Budapest", valid_settlements), "budapest_23")
     self.assertEqual(util.get_city_key("1889", "Budapest", valid_settlements), "budapest")
     self.assertEqual(util.get_city_key("9999", "", valid_settlements), "_Empty")
     self.assertEqual(util.get_city_key("9999", "Lábatlan", valid_settlements), "lábatlan")
     self.assertEqual(util.get_city_key("9999", "junk", valid_settlements), "_Invalid")
     # Even if the postcode does not start with 1.
     self.assertEqual(util.get_city_key("9999", "Budapest", valid_settlements), "budapest")
Beispiel #4
0
 def test_happy(self) -> None:
     """Tests the happy path."""
     self.assertEqual(util.get_city_key("1234", "Budapest"), "budapest_23")
     self.assertEqual(util.get_city_key("9999", ""), "")
     self.assertEqual(util.get_city_key("9999", "Lábatlan"), "lábatlan")