From d285d56c85201e8915515e76530cb59924d0650c Mon Sep 17 00:00:00 2001 From: semi-yu <97109907+semi-yu@users.noreply.github.com> Date: Sat, 4 Jan 2025 02:05:04 +0900 Subject: [PATCH] Resolve address number output issue in `ko_KR` address provider --- faker/providers/address/ko_KR/__init__.py | 46 ++++++++++++++++++++--- tests/providers/test_address.py | 20 ++++++++++ 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/faker/providers/address/ko_KR/__init__.py b/faker/providers/address/ko_KR/__init__.py index 437a2c00de..5d67409d5a 100644 --- a/faker/providers/address/ko_KR/__init__.py +++ b/faker/providers/address/ko_KR/__init__.py @@ -454,12 +454,8 @@ class Provider(AddressProvider): "{{building_name}} {{building_dong}}동 ###호", ) road_formats = ( - "{{road_name}}{{road_suffix}} ###", - "{{road_name}}{{road_suffix}} 지하###", - "{{road_name}}{{road_suffix}} ###-##", - "{{road_name}}{{road_number}}{{road_suffix}} ###", - "{{road_name}}{{road_number}}{{road_suffix}} 지하###", - "{{road_name}}{{road_number}}{{road_suffix}} ###-##", + "{{road_name}}{{road_suffix}} {{building_number}}", + "{{road_name}}{{road_number}}{{road_suffix}} {{building_number}}", ) road_address_formats = ( "{{metropolitan_city}} {{borough}} {{road}}", @@ -531,6 +527,44 @@ def road_suffix(self) -> str: """ return self.random_element(self.road_suffixes) + def building_number(self) -> str: + """ + :returns: A random building number + + Generates building number(건물 번호). There are 3 types of building number with current ROK addressing system. + (1) 19: A typical format. Only marks one building. + (2) 지하11: The building entrance is underground. + (3) 132-1: Several buildings are distinguished with sub-building-number(가지 번호). + + Generating probability is arbitrarily. + + :example: 19, 지하11, 143-1 + """ + if self.random_int() % 9 < 1: + return self.building_number_underground() + elif self.random_int() % 9 < 4: + return self.building_number_segregated() + else: + return "%d" % self.generator.random.randint(1, 999) + + def building_number_underground(self) -> str: + """ + :returns: A random building number with undergrond entrances + + :example: 지하11 + """ + return "지하%d" % (self.generator.random.randint(1,999)) + + def building_number_segregated(self) -> str: + """ + :returns: A random building number distinguished with sub-building-number(가지 번호) + + :example: 143-1 + """ + main_building_number = self.generator.random.randint(1, 999) + sub_building_number = self.generator.random.randint(1, 99) + return "%d-%d" % (main_building_number, sub_building_number) + def metropolitan_city(self) -> str: """ :example: 서울특별시 diff --git a/tests/providers/test_address.py b/tests/providers/test_address.py index a510132470..d677bffbd1 100644 --- a/tests/providers/test_address.py +++ b/tests/providers/test_address.py @@ -1305,6 +1305,26 @@ def test_building_name(self, faker, num_samples): building_name = faker.building_name() assert isinstance(building_name, str) + def test_building_number(self, faker, num_samples): + for _ in range(num_samples): + building_number = faker.building_number() + assert isinstance(building_number, str) + assert '#' not in building_number + + def test_building_number_underground(self, faker, num_samples): + for _ in range(num_samples): + building_number = faker.building_number_underground() + assert isinstance(building_number, str) + assert '#' not in building_number + assert building_number[:2] == '지하' + + def test_building_number_segregated(self, faker, num_samples): + for _ in range(num_samples): + building_number = faker.building_number_segregated() + assert isinstance(building_number, str) + assert '#' not in building_number + assert '-' in building_number + def test_building_suffix(self, faker, num_samples): for _ in range(num_samples): building_suffix = faker.building_suffix()