@@ -11,11 +11,10 @@ def open_data():
11
11
for row in reader :
12
12
yield row
13
13
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 )
19
18
result = {}
20
19
for row in countries :
21
20
if row [1 ].lower () == alpha2 .lower ():
@@ -26,8 +25,10 @@ def alpha2(alpha2) -> str:
26
25
break
27
26
return result
28
27
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 )
31
32
result = {}
32
33
for row in countries :
33
34
if row [2 ].lower () == alpha3 .lower ():
@@ -38,8 +39,10 @@ def alpha3(alpha3) -> str:
38
39
break
39
40
return result
40
41
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 )
43
46
result = {}
44
47
for row in countries :
45
48
if int (row [3 ]) == numeric :
@@ -50,8 +53,14 @@ def numeric(numeric)-> int:
50
53
break
51
54
return result
52
55
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 )
55
64
result = []
56
65
for row in countries :
57
66
ratio = levenshtein_ratio (row [0 ].lower (), english_name .lower ())
@@ -66,7 +75,7 @@ def from_english(english_name, lev_ratio=1) -> str:
66
75
result .append (inner_dict )
67
76
return result
68
77
69
- def levenshtein_ratio (s , t ):
78
+ def levenshtein_ratio (s , t ) -> float :
70
79
'''Calculates levenshtein distance and ratio for from_english().'''
71
80
rows = len (s )+ 1
72
81
cols = len (t )+ 1
@@ -87,9 +96,12 @@ def levenshtein_ratio(s, t):
87
96
ratio = ((len (s )+ len (t )) - distance [row ][col ]) / (len (s )+ len (t ))
88
97
return ratio
89
98
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 )
93
105
result = []
94
106
for row in countries :
95
107
inner_dict = {}
0 commit comments