Skip to content

Commit

Permalink
feat: add country demonym converter
Browse files Browse the repository at this point in the history
  • Loading branch information
ratoaq2 committed Jan 3, 2023
1 parent 942c881 commit c49503e
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
29 changes: 29 additions & 0 deletions babelfish/converters/countrydemonym.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2023 the BabelFish authors. All rights reserved.
# Use of this source code is governed by the 3-clause BSD license
# that can be found in the LICENSE file.
#
from __future__ import unicode_literals
from . import CountryReverseConverter, CaseInsensitiveDict
from ..country import DEMONYMS
from ..exceptions import CountryConvertError, CountryReverseError


class CountryDemonymConverter(CountryReverseConverter):
def __init__(self):
self.to_demonym = {}
self.from_demonym = CaseInsensitiveDict()
for alpha2, demonym in DEMONYMS.items():
self.to_demonym[alpha2] = demonym
self.from_demonym[demonym] = alpha2

def convert(self, alpha2):
if alpha2 not in self.to_demonym:
raise CountryConvertError(alpha2)
return self.to_demonym[alpha2]

def reverse(self, demonym):
if demonym not in self.from_demonym:
raise CountryReverseError(demonym)
return self.from_demonym[demonym]
11 changes: 10 additions & 1 deletion babelfish/country.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

COUNTRIES = {}
COUNTRY_MATRIX = []
DEMONYMS = {}

#: The namedtuple used in the :data:`COUNTRY_MATRIX`
IsoCountry = namedtuple('IsoCountry', ['name', 'alpha2'])
Expand All @@ -26,11 +27,19 @@
COUNTRY_MATRIX.append(iso_country)
f.close()

with resource_stream('babelfish', 'data/country-demonyms.txt') as f:
for l in f:
alpha2, demonym = l.decode('utf-8').strip().split('=')
DEMONYMS[alpha2] = demonym


class CountryConverterManager(ConverterManager):
""":class:`~babelfish.converters.ConverterManager` for country converters"""
entry_point = 'babelfish.country_converters'
internal_converters = ['name = babelfish.converters.countryname:CountryNameConverter']
internal_converters = [
'name = babelfish.converters.countryname:CountryNameConverter',
'demonym = babelfish.converters.countrydemonym:CountryDemonymConverter',
]

country_converters = CountryConverterManager()

Expand Down
5 changes: 5 additions & 0 deletions babelfish/data/country-demonyms.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
BR=Brazilian
CH=Swiss
FR=French
US=American
GB=British
25 changes: 23 additions & 2 deletions tests/test_converters.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
from babelfish.converters import LanguageReverseConverter
import pytest
from pkg_resources import resource_stream
from babelfish import language_converters
from babelfish.exceptions import LanguageConvertError, LanguageReverseError
from babelfish import language_converters, Country
from babelfish.exceptions import CountryReverseError, LanguageConvertError, LanguageReverseError
from babelfish.language import Language
from babelfish.country import DEMONYMS


@pytest.mark.parametrize('demonym, expected', [
('Brazilian', 'BR'),
('American', 'US'),
('french', 'FR'),
('british', 'GB'),
('Swiss', 'CH'),
])
def test_converter_country_demonym(demonym, expected):
assert Country.fromdemonym(demonym) == expected

with pytest.raises(CountryReverseError):
Country.fromdemonym('aaa')


def test_converter_country_demonym_setup():
with pytest.raises(CountryReverseError):
Country.fromdemonym('aaa')
assert len(DEMONYMS) == 5


def test_converter_alpha2():
Expand Down

0 comments on commit c49503e

Please sign in to comment.