Skip to content

Commit

Permalink
Resolve address number output issue in ko_KR address provider
Browse files Browse the repository at this point in the history
  • Loading branch information
semi-yu authored Jan 3, 2025
1 parent 233a9a7 commit d285d56
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
46 changes: 40 additions & 6 deletions faker/providers/address/ko_KR/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}}",
Expand Down Expand Up @@ -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))

Check failure on line 556 in faker/providers/address/ko_KR/__init__.py

View workflow job for this annotation

GitHub Actions / flake8

missing whitespace after ','

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: 서울특별시
Expand Down
20 changes: 20 additions & 0 deletions tests/providers/test_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit d285d56

Please sign in to comment.