Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve address number output issue and refactor road_formats for clarity #2138

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))

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