Skip to content

Commit

Permalink
v1.5.0; fixing /name endpoint, updating api documentation and homepage
Browse files Browse the repository at this point in the history
  • Loading branch information
amckenna41 committed Feb 29, 2024
1 parent 381271d commit 0663eef
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
1 change: 1 addition & 0 deletions ToDo.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
- [X] Error when passing names with accents into /name endpoint e.g. https://iso3166-2-api.vercel.app/api/name/Goiás,Paraíba,São Paulo & https://iso3166-2-api.vercel.app/api/name/Goi%C3%A1s
- [X] In /name endpoint, remove repeated references to .lower().replace() etc.
- [X] Change thefuzz scorer ratio.
- [X] In output error message, ensure name is same as input param
<!-- >>> for d in abc.all:
... for e in abc.all[d]:
... if (unidecode(abc.all[d][e]["name"]).lower().replace(' ' ,'') not in new_list):
Expand Down
22 changes: 11 additions & 11 deletions index.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ def api_country_name(country_name=""):
break
else:
#return error if country name not found
error_message["message"] = "Invalid country name input: {}.".format(name_)
error_message["message"] = "Invalid country name input: {}.".format(name)
return jsonify(error_message), 400

#use iso3166 package to find corresponding alpha-2 code from its name
Expand All @@ -379,10 +379,10 @@ def api_subdivision_name(subdivision_name=""):
"""
Flask route for '/api/name' path/endpoint. Return all ISO 3166-2 subdivision data attributes and
values for inputted subdivision name/names. When searching for the sought subdivision name, a
closeness function is used to find the exact match within the dataset, if this returns nothing
then the dataset is searched for subdivision names that match 90% or more, the first match will
then be returned. If no matching subdivision name found or input is empty then return an error.
Route can accept path with or without trailing slash.
fuzzy search algorithm is used via "thefuzz" package that finds the exact match within the
dataset, if this returns nothing then the dataset is searched for subdivision names that match
90% or more, the first match will then be returned. If no matching subdivision name found or
input is empty then return an error. Route can accept path with or without trailing slash.
Parameters
==========
Expand Down Expand Up @@ -428,7 +428,7 @@ def is_float(string):
search_likeness = float(search_likeness)

#decode any unicode or accent characters using utf-8 encoding, lower case and remove additional whitespace
subdivision_name = unidecode(unquote_plus(subdivision_name).lower().replace(' ', ''))
subdivision_name_ = unidecode(unquote_plus(subdivision_name).lower().replace(' ', ''))

#object to store the subdivision name and its corresponding alpha-2 code and subdivision code (name: alpha_2, code: subd_code)
all_subdivision_names = {}
Expand Down Expand Up @@ -459,27 +459,27 @@ def is_float(string):
all_subdivision_names_list.append(unidecode(unquote_plus(all_iso3166_2[alpha_2][subd]["name"]).lower().replace(' ', '')))

#if comma in official subdivision name, append to the exception list, which is needed if a comma seperated list of names are input
if (',' in subdivision_name):
if (',' in subdivision_name_):
if (',' in unidecode(all_iso3166_2[alpha_2][subd]["name"].lower().replace(' ', ''))):
subdivision_name_expections.append(unidecode(all_iso3166_2[alpha_2][subd]["name"].lower().replace(' ', '')))

#only execute subdivision name exception code if comma is in input param
if (',' in subdivision_name):
if (',' in subdivision_name_):
#sort exceptions list alphabetically
subdivision_name_expections.sort()

#temp var to track input subdivision name
temp_subdivision_name = subdivision_name
temp_subdivision_name = subdivision_name_

#iterate over all subdivision names exceptions (those with a comma in them), append to seperate list if input param is one
for sub_name in subdivision_name_expections:
if (sub_name in temp_subdivision_name):
subdivision_name_expections_input.append(sub_name)
#remove current subdivision name from temp var, strip of commas
subdivision_name = temp_subdivision_name.replace(sub_name, '').strip(',')
subdivision_name_ = temp_subdivision_name.replace(sub_name, '').strip(',')

#sort all subdivision names codes
subdivision_names = sorted([subdivision_name])
subdivision_names = sorted([subdivision_name_])

#split multiple subdivision names into list
subdivision_names = subdivision_names[0].split(',')
Expand Down
2 changes: 1 addition & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<div class="content-infos">
<!-- version and last updated info -->
<div class="info" id="version"><b>Version:</b> 1.5.0</div>
<div class="info" id="last-updated"><b>Last Updated:</b> February 2024</div>
<div class="info" id="last-updated"><b>Last Updated:</b> March 2024</div>
<div class="info" id="author"><b>Developer/Maintainer:</b> <a href="https://github.com/amckenna41/" target="_blank">AJ</a></div>
</div>
<!-- list of available sections in API documentation -->
Expand Down
2 changes: 1 addition & 1 deletion tests/test_iso3166_2_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_homepage_endpoint(self):
author = soup.find(id='author').text.split(': ')[1]

self.assertEqual(version, "1.5.0", "Expected API version to be 1.5.0, got {}.".format(version))
self.assertEqual(last_updated, "February 2024", "Expected last updated data to be February 2024, got {}.".format(last_updated))
self.assertEqual(last_updated, "March 2024", "Expected last updated data to be March 2024, got {}.".format(last_updated))
self.assertEqual(author, "AJ", "Expected author to be AJ, got {}.".format(author))
#2.)
section_list_menu = soup.find(id='section-list-menu').find_all('li')
Expand Down

0 comments on commit 0663eef

Please sign in to comment.