<commit_before>import os

from django.contrib.gis.geos import MultiPolygon, Polygon, GEOSGeometry

from osmaxx.countries._settings import POLYFILE_LOCATION

POLYFILE_ENDING = '.poly'


def get_polyfile_name_to_file_mapping():
    polyfile_mapping = {}
    for possible_polyfile in os.listdir(POLYFILE_LOCATION):
        if possible_polyfile.endswith(POLYFILE_ENDING):
            name, _ = possible_polyfile.split(POLYFILE_ENDING)
            polyfile_mapping[name] = possible_polyfile
    return polyfile_mapping


def polyfile_to_geos_geometry(relative_polygon_file, simplify_tolerance=None):
    with open(os.path.join(POLYFILE_LOCATION, relative_polygon_file)) as poly_file:
        poly = parse_poly(poly_file.readlines())
        if simplify_tolerance:
            poly = poly.simplify(tolerance=simplify_tolerance, preserve_topology=True)
            # simplifying can lead to a polygon. Assure, it stays a multipolygon.
            if poly and isinstance(poly, Polygon):
                poly = MultiPolygon(poly)
    return GEOSGeometry(poly)


def parse_poly_string(poly_string):
    return parse_poly(poly_string.splitlines())
<commit_msg>use dictionary comprehension instead of explicitly building dict iteratively<commit_after>import os

from django.contrib.gis.geos import MultiPolygon, Polygon, GEOSGeometry

from osmaxx.countries._settings import POLYFILE_LOCATION

POLYFILE_ENDING = '.poly'


def get_polyfile_name_to_file_mapping():
    filenames = os.listdir(POLYFILE_LOCATION)
    return {
        _extract_country_name_from_polyfile_name(filename): filename
        for filename in filenames if _is_polyfile(filename)
    }


def _is_polyfile(filename):
    return filename.endswith(POLYFILE_ENDING)


def _extract_country_name_from_polyfile_name(filename):
    name, _ = filename.split(POLYFILE_ENDING)
    return name


def polyfile_to_geos_geometry(relative_polygon_file, simplify_tolerance=None):
    with open(os.path.join(POLYFILE_LOCATION, relative_polygon_file)) as poly_file:
        poly = parse_poly(poly_file.readlines())
        if simplify_tolerance:
            poly = poly.simplify(tolerance=simplify_tolerance, preserve_topology=True)
            # simplifying can lead to a polygon. Assure, it stays a multipolygon.
            if poly and isinstance(poly, Polygon):
                poly = MultiPolygon(poly)
    return GEOSGeometry(poly)


def parse_poly_string(poly_string):
    return parse_poly(poly_string.splitlines())

{'fraction_non_alphanumeric': 0.07064555420219244, 'fraction_numerical': 0.0, 'mean_word_length': 4.231422505307855, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3001819', 'n_tokens_mistral': 789, 'n_tokens_neox': 751, 'n_words': 152}