Skip to content

Commit

Permalink
Make bool arguments in search_cities keyword only arguments.
Browse files Browse the repository at this point in the history
Rename input variable to value.
Fix more linting issues: magic number, list comprehension.
  • Loading branch information
yaph committed Jul 25, 2024
1 parent 0d08aef commit dcc2d12
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
4 changes: 3 additions & 1 deletion bin/continents.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import httpx

ACCOUNT_ERR_CODE = 10

# see http://download.geonames.org/export/dump/readme.txt
continent_ids = [6255146, 6255147, 6255148, 6255149, 6255151, 6255150, 6255152]
url = 'http://api.geonames.org/getJSON'
Expand All @@ -20,7 +22,7 @@


def account_ok(j):
if j.get('status', {}).get('value') == 10:
if j.get('status', {}).get('value') == ACCOUNT_ERR_CODE:
print(j['status']['message'])
sys.exit(1)

Expand Down
9 changes: 2 additions & 7 deletions bin/us_counties.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@
import json
from pathlib import Path

counties = []
p_data = Path('data')

reader = csv.reader(p_data.joinpath('us_counties.txt').open())
for line in reader:
counties.append({
'fips': line[1] + line[2],
'name': line[3],
'state': line[0]
})

counties = [{'fips': line[1] + line[2], 'name': line[3], 'state': line[0]} for line in reader]

p_data.joinpath('us_counties.json').write_text(json.dumps(counties))
3 changes: 2 additions & 1 deletion geonamescache/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ def search_cities(
self,
query: str,
attribute: CitySearchAttribute = 'alternatenames',
*,
case_sensitive: bool = False,
contains_search: bool = True,
contains_search: bool = True
) -> List[City]:
"""Search all city records and return list of records, that match query for given attribute."""
results = []
Expand Down
10 changes: 5 additions & 5 deletions geonamescache/mappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ def country(
gc = GeonamesCache()
dataset = gc.get_dataset_by_key(gc.get_countries(), from_key)

def mapper(input: str) -> Any:
# For country name inputs take the names mapping into account.
def mapper(value: str) -> Any:
# For country names take the mappings into account.
if from_key == "name":
input = mappings.country_names.get(input, input)
# If there is a record return the demanded attribute.
item = dataset.get(input)
value = mappings.country_names.get(value, value)
# If there is a record return the corresponding attribute value.
item = dataset.get(value)
if item:
return item[to_key]
return None
Expand Down

0 comments on commit dcc2d12

Please sign in to comment.