porimol / countryinfo

A python module for returning data about countries, ISO info and states/provinces within them.
MIT License
140 stars 44 forks source link

Cache outside of the CountryInfo object #40

Open voidus opened 2 years ago

voidus commented 2 years ago

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

atifiu commented 1 year ago

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?

PLPeeters commented 1 year ago

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?

atifiu commented 1 year ago

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

PLPeeters commented 1 year ago

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

atifiu commented 1 year ago

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