0

I use the following code, which I got from this question, to get country codes from their russian names.

def get_country_code(russian_name):
    gettext.translation('iso3166', pycountry.LOCALES_DIR, languages=['RU']).install()
    country_code = ''.join([country.alpha_2 for country in pycountry.countries if _(country.name) == russian_name])
    return country_code

But as I try to use this function in my jupyter-notebook, it gives me the following error:

TypeError: 'str' object is not callable

Is seems like this is because in jupyter as well as in interactive mode _ means the result of the previous calculation, but gettext define it as its function.

How can I execute this code in Jupyter?

Dimitrius
  • 564
  • 6
  • 21

1 Answers1

0

I've found the solution to the problem. The install() function sets variable _ in builtins like this:

builtins.__dict__['_'] = self.gettext

So we can use this function instead.

def get_country_code(russian_name):
    rus = gettext.translation('iso3166', pycountry.LOCALES_DIR, languages=['RU'])
    rus.install()
    country_code = ''.join([country.alpha_2 for country in pycountry.countries if rus.gettext(country.name) == russian_name]) # u'FR'
    return country_code
Dimitrius
  • 564
  • 6
  • 21