<commit_before>import random
from osgeo import ogr, osr

class LocGenerator:
    def initialise(self):
        return

    def cleanup(self):
        return

class BasicRandomLocGenerator(LocGenerator):
    def __init__(self, seed, bbox):
        self._random_seed = seed
        self._randomiser = random.Random(seed)
        self.bbox = bbox
    
    def update_zone(self, zone_name):
        # Nothing to do.
        return

    def gen_loc_within_zone(self, zone_name):
        return self.gen_loc_within_curr_zone()

    def gen_loc_within_curr_zone(self):
        loc_x = self._randomiser.uniform(self.bbox[0][0], self.bbox[1][0])
        loc_y = self._randomiser.uniform(self.bbox[0][1], self.bbox[1][1])
        return (loc_x, loc_y)

class WithinZoneLocGenerator:
    def __init__(self, seed, zone_polys_dict, constraint_checkers=None):
        self._random_seed = seed
        self._zone_polys_dict = zone_polys_dict
        self.constraint_checkers = constraint_checkers

    def initialise(self):
        self._randomiser = random.Random(self._random_seed)
        if self.constraint_checkers:
            for cc in self.constraint_checkers:
                cc.initialise()
    
    def update_zone(self, zone_name):
        self._curr_zone = zone_name
        zone_shp = self._zone_polys_dict[zone_name]
        self._curr_zone_poly = zone_shp.GetGeometryRef()
        self._curr_zone_env = self._curr_zone_poly.GetEnvelope()
        if self.constraint_checkers:
            for cc in self.constraint_checkers:
                cc.update_region(zone_name, self._curr_zone_poly)
        
    def gen_loc_within_zone(self, zone_name):
        self.update_zone(zone_name)
        return self.gen_loc_within_curr_zone()

    def gen_loc_within_curr_zone(self):
        valid_loc_found = False
        loc_pt = None
        while True:
            # First generate a random point within the extents
            loc_x = self._randomiser.uniform(self._curr_zone_env[0],
                self._curr_zone_env[1])
            loc_y = self._randomiser.uniform(self._curr_zone_env[2],
                self._curr_zone_env[3])
            loc_pt = ogr.Geometry(ogr.wkbPoint)
            loc_pt.AddPoint(loc_x, loc_y)
            if self._curr_zone_poly.Contains(loc_pt):
                if not self.constraint_checkers:
                    valid_loc_found = True
                else:
                    # Also check the location passes any constraints.
                    all_cc_pass = True
                    for cc in self.constraint_checkers:
                        if not cc.is_valid(loc_pt):
                            all_cc_pass = False
                            break
                    if all_cc_pass:
                        valid_loc_found = True
            if valid_loc_found:        
                break
            else:
                # Keep searching for a valid location ...
                loc_pt.Destroy()
        return loc_pt

    def cleanup(self):
        if self.constraint_checkers:
            for cc in self.constraint_checkers:
                cc.cleanup()
        return        
<commit_msg>Improved the LocGenerator update_zone() method so it doesn't update if already in that zone.<commit_after>import random
from osgeo import ogr, osr

class LocGenerator:
    def initialise(self):
        return

    def cleanup(self):
        return

class BasicRandomLocGenerator(LocGenerator):
    def __init__(self, seed, bbox):
        self._random_seed = seed
        self._randomiser = random.Random(seed)
        self.bbox = bbox
    
    def update_zone(self, zone_name):
        # Nothing to do.
        return

    def gen_loc_within_zone(self, zone_name):
        return self.gen_loc_within_curr_zone()

    def gen_loc_within_curr_zone(self):
        loc_x = self._randomiser.uniform(self.bbox[0][0], self.bbox[1][0])
        loc_y = self._randomiser.uniform(self.bbox[0][1], self.bbox[1][1])
        return (loc_x, loc_y)

class WithinZoneLocGenerator:
    def __init__(self, seed, zone_polys_dict, constraint_checkers=None):
        self._random_seed = seed
        self._zone_polys_dict = zone_polys_dict
        self.constraint_checkers = constraint_checkers
        self._curr_zone = None

    def initialise(self):
        self._curr_zone = None
        self._randomiser = random.Random(self._random_seed)
        if self.constraint_checkers:
            for cc in self.constraint_checkers:
                cc.initialise()
    
    def update_zone(self, zone_name):
        if self._curr_zone == zone_name:
            # Nothing to do.
            return
        self._curr_zone = zone_name
        try:
            zone_shp = self._zone_polys_dict[zone_name]
        except KeyError:
            msg = "Error: couldn't find zone %s in list of %d known zones."\
                % (zone_name, len(self._zone_polys_dict))
            raise ValueError(msg)
        self._curr_zone_poly = zone_shp.GetGeometryRef()
        self._curr_zone_env = self._curr_zone_poly.GetEnvelope()
        if self.constraint_checkers:
            for cc in self.constraint_checkers:
                cc.update_region(zone_name, self._curr_zone_poly)
        return
        
    def gen_loc_within_zone(self, zone_name):
        self.update_zone(zone_name)
        return self.gen_loc_within_curr_zone()

    def gen_loc_within_curr_zone(self):
        valid_loc_found = False
        loc_pt = None
        while True:
            # First generate a random point within the extents
            loc_x = self._randomiser.uniform(self._curr_zone_env[0],
                self._curr_zone_env[1])
            loc_y = self._randomiser.uniform(self._curr_zone_env[2],
                self._curr_zone_env[3])
            loc_pt = ogr.Geometry(ogr.wkbPoint)
            loc_pt.AddPoint(loc_x, loc_y)
            if self._curr_zone_poly.Contains(loc_pt):
                if not self.constraint_checkers:
                    valid_loc_found = True
                else:
                    # Also check the location passes any constraints.
                    all_cc_pass = True
                    for cc in self.constraint_checkers:
                        if not cc.is_valid(loc_pt):
                            all_cc_pass = False
                            break
                    if all_cc_pass:
                        valid_loc_found = True
            if valid_loc_found:        
                break
            else:
                # Keep searching for a valid location ...
                loc_pt.Destroy()
        return loc_pt

    def cleanup(self):
        if self.constraint_checkers:
            for cc in self.constraint_checkers:
                cc.cleanup()
        return        

{'fraction_non_alphanumeric': 0.07325789160214413, 'fraction_numerical': 0.00357355568790947, 'mean_word_length': 1.861951427354069, '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': '497259', 'n_tokens_mistral': 1986, 'n_tokens_neox': 1922, 'n_words': 402}