porimol / countryinfo

A python module for returning data about countries, ISO info and states/provinces within them.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cache outside of the CountryInfo object

voidus opened this issue · comments

Currently, all files are loaded every time a CountryInfo object is created. That's inefficient and should be changed.

Easiest would probably be to move the data loading part to a cached global function

commented

Just wanted to understand what you are suggesting. Move data loading part to global cached function called from init and loading all the files regardless of the country being searched or load only the file for which search is being performed?

I resorted to the following to only load the data once:

class PatchedCountryInfo(CountryInfo):
    _CountryInfo__countries = None

    def __init__(self, country_name=None):
        if self._CountryInfo__countries is None:
            super().__init__(country_name)

            type(self)._CountryInfo__countries = self._CountryInfo__countries

        self._CountryInfo__country_name = country_name.lower() if country_name else ''

        for country_info in self._CountryInfo__countries.values():
            if self._CountryInfo__country_name in map(lambda an: an.lower(), country_info.get('altSpellings', [])):
                self._CountryInfo__country_name = country_info['name'].lower()
                break

I'd do a PR instead of monkey-patching, but this repo seems pretty dead.

@porimol Are you still maintaining this?

commented

Let me know if I can be of any help here.

@atifiu Your status here is a bit unclear: do you have write access to this repo or are you offering to do the PR?

commented

I am offering PR. If we want to activated this repo.