Skip to content

Commit cb727be

Browse files
committed
Changed functions, docstrings, exceptions
1 parent 9780392 commit cb727be

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

pylond/pylond.py

+27-15
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ def open_data():
1111
for row in reader:
1212
yield row
1313

14-
countries = open_data()
15-
headers = next(countries)
16-
17-
def alpha2(alpha2) -> str:
18-
'''Return a country based on the ISO-3166-1 alpha2 code.'''
14+
def alpha2(alpha2) -> dict:
15+
'''Returns a dict based on the ISO-3166-1 alpha2 code.'''
16+
countries = open_data()
17+
headers = next(countries)
1918
result = {}
2019
for row in countries:
2120
if row[1].lower() == alpha2.lower():
@@ -26,8 +25,10 @@ def alpha2(alpha2) -> str:
2625
break
2726
return result
2827

29-
def alpha3(alpha3) -> str:
30-
'''Return a country based on the ISO-3166-1 alpha3 code.'''
28+
def alpha3(alpha3) -> dict:
29+
'''Returns a dict based on the ISO-3166-1 alpha3 code.'''
30+
countries = open_data()
31+
headers = next(countries)
3132
result = {}
3233
for row in countries:
3334
if row[2].lower() == alpha3.lower():
@@ -38,8 +39,10 @@ def alpha3(alpha3) -> str:
3839
break
3940
return result
4041

41-
def numeric(numeric)-> int:
42-
'''Return a country based on the ISO-3166-1 numeric code.'''
42+
def numeric(numeric)-> dict:
43+
'''Returns a dict based on the ISO-3166-1 numeric code.'''
44+
countries = open_data()
45+
headers = next(countries)
4346
result = {}
4447
for row in countries:
4548
if int(row[3]) == numeric:
@@ -50,8 +53,14 @@ def numeric(numeric)-> int:
5053
break
5154
return result
5255

53-
def from_english(english_name, lev_ratio=1) -> str:
54-
'''Search for a country based on the English short name. Optional lev_ratio parameter should be in range of 0 to 1 (default).'''
56+
def from_english(english_name, lev_ratio=1) -> list:
57+
'''Returns a list based on the English short name. Optional lev_ratio parameter should be in range of 0 to 1 (default).'''
58+
if not isinstance(lev_ratio, (int, float)):
59+
raise TypeError(f'lev_ratio should be int or float, got: {type(lev_ratio).__name__}')
60+
if not 0 <= lev_ratio <= 1:
61+
raise ValueError(f'lev_ratio should be in range of 0 and 1, got: {lev_ratio}')
62+
countries = open_data()
63+
headers = next(countries)
5564
result = []
5665
for row in countries:
5766
ratio = levenshtein_ratio(row[0].lower(), english_name.lower())
@@ -66,7 +75,7 @@ def from_english(english_name, lev_ratio=1) -> str:
6675
result.append(inner_dict)
6776
return result
6877

69-
def levenshtein_ratio(s, t):
78+
def levenshtein_ratio(s, t) -> float:
7079
'''Calculates levenshtein distance and ratio for from_english().'''
7180
rows = len(s)+1
7281
cols = len(t)+1
@@ -87,9 +96,12 @@ def levenshtein_ratio(s, t):
8796
ratio = ((len(s)+len(t)) - distance[row][col]) / (len(s)+len(t))
8897
return ratio
8998

90-
def iter_countries(sort='en') -> str:
91-
'''Returns a list of countries. The sort parameter can take "eng" (default) or "isl"'''
92-
assert sort in ['en', 'is'], 'Sort parameter can only take "en" or "is"'
99+
def iter_countries(sort='en') -> list:
100+
'''Returns a list of countries. The sort parameter can take "en" (default) or "is"'''
101+
if sort not in ['en', 'is']:
102+
raise ValueError(f'Sort parameter can only take en or is, got: {sort}')
103+
countries = open_data()
104+
headers = next(countries)
93105
result = []
94106
for row in countries:
95107
inner_dict = {}

0 commit comments

Comments
 (0)