Skip to content

Commit

Permalink
Bump version
Browse files Browse the repository at this point in the history
Remove datadir attribute from GeonamesCache
Add constructor and set min_city_population, which is used to access different sized city datasets
Update supported Python versions
  • Loading branch information
yaph committed Jun 30, 2022
1 parent 5782397 commit 9ec3311
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
max-parallel: 4
matrix:
python-version: [3.7, 3.8, 3.9]
python-version: [3.9, 3.10, 3.11]

steps:
- uses: actions/checkout@v1
Expand Down
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
include README.rst LICENSE
recursive-include geonamescache *.json
recursive-include geonamescache data/*.json
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ install: clean
python setup.py install


# Call example: make release version=1.3.0
# Call example: make release version=1.4.0
release: test dist
git tag -a $(version) -m 'Create version $(version)'
git push --tags
Expand Down
Empty file added data/__init__.py
Empty file.
18 changes: 10 additions & 8 deletions geonamescache/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# -*- coding: utf-8 -*-
__title__ = 'geonamescache'
__version__ = '1.3.0'
__version__ = '1.4.0'
__author__ = 'Ramiro Gómez'
__license__ = 'MIT'


import os
import json

from importlib import resources

from . import geonamesdata


Expand All @@ -19,7 +21,9 @@ class GeonamesCache:
cities_items = None
cities_by_names = {}
us_counties = None
datadir = os.path.dirname(os.path.abspath(__file__))

def __init__(self, min_city_population=15000):
self.min_city_population = min_city_population

def get_dataset_by_key(self, dataset, key):
return dict((d[key], d) for c, d in list(dataset.items()))
Expand Down Expand Up @@ -48,7 +52,7 @@ def get_cities(self):
"""Get a dictionary of cities keyed by geonameid."""

if self.cities is None:
self.cities = self._load_data(self.cities, 'cities.json')
self.cities = self._load_data(self.cities, f'cities{self.min_city_population}.json')
return self.cities

def get_cities_by_name(self, name):
Expand All @@ -66,8 +70,7 @@ def get_cities_by_name(self, name):

def get_us_counties(self):
if self.us_counties is None:
self.us_counties = self._load_data(
self.us_counties, 'us_counties.json')
self.us_counties = self._load_data(self.us_counties, 'us_counties.json')
return self.us_counties

def search_cities(self, query, attribute='alternatenames', case_sensitive=True):
Expand Down Expand Up @@ -95,6 +98,5 @@ def search_cities(self, query, attribute='alternatenames', case_sensitive=True):

def _load_data(self, datadict, datafile):
if datadict is None:
with open(os.path.join(self.datadir, datafile), 'r') as f:
datadict = json.load(f)
datadict = json.loads(resources.files('data').joinpath(datafile).read_text())
return datadict
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
author_email='[email protected]',
url='https://github.com/yaph/geonamescache',
license='MIT',
packages=find_packages(exclude=('tests', 'data', 'scripts')),
package_data={'': ['LICENSE']},
packages=find_packages(exclude=('tests', 'scripts')),
package_data={'': ['data/*.json', 'LICENSE']},
include_package_data=True,
classifiers=[
'Development Status :: 5 - Production/Stable',
Expand All @@ -28,9 +28,9 @@
'License :: OSI Approved :: MIT License',
'Programming Language :: Python',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Topic :: Software Development :: Libraries :: Python Modules'
],
test_suite='tests',
Expand Down
4 changes: 2 additions & 2 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ def test_cities_len():


def test_us_counties_len():
# Make sure there are 3235 counties, which includes Puerto Rico etc.
# Make sure there are more than 3000 counties
us_counties = geonamescache.get_us_counties()
assert 3234 == len(us_counties)
assert 3000 < len(us_counties)
2 changes: 1 addition & 1 deletion tests/test_geonamescache.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_get_cities_by_name(self):
self.assertEqual(name, cities[gid]['name'])

def test_get_cities_by_name_madrid(self):
self.assertGreater(len(self.geonamescache.get_cities_by_name('Madrid')), 2)
self.assertGreater(len(self.geonamescache.get_cities_by_name('Madrid')), 1)

def test_cities_in_us_states(self):
cities = self.geonamescache.get_cities()
Expand Down

0 comments on commit 9ec3311

Please sign in to comment.