Skip to content

Commit c08ff10

Browse files
committed
Added Country and Region class.
1 parent 53afa96 commit c08ff10

File tree

13 files changed

+1211
-852
lines changed

13 files changed

+1211
-852
lines changed

ChangeLog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
8.9.0 2022-10-14
2+
* Added Country and Region class.
3+
14
8.8.1 2022-08-12
25
* Updated the homepage link.
36

IP2Location/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from IP2Location.database import IP2Location
2+
from IP2Location.webservice import IP2LocationWebService
3+
from IP2Location.iptools import IP2LocationIPTools
4+
from IP2Location.country import Country
5+
from IP2Location.region import Region

IP2Location/country.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import csv
2+
import os
3+
4+
"""Country class."""
5+
class Country(object):
6+
7+
def __init__(self, filename=None):
8+
self.fields = []
9+
self.records = {}
10+
11+
if filename is not None:
12+
if os.path.isfile(filename) == False:
13+
raise ValueError("The CSV file " + filename + " is not found.")
14+
15+
if filename:
16+
line = 1
17+
with open(filename, "r", encoding = "utf-8") as f:
18+
mycsv = csv.reader(f)
19+
for row in mycsv:
20+
if type(row) == list :
21+
if (line == 1) :
22+
if row[0] != "country_code":
23+
raise ValueError("Invalid country information CSV file.")
24+
self.fields = row
25+
else:
26+
self.records[row[0]] = row
27+
line = line + 1
28+
29+
def get_country_info(self, country_code=None):
30+
"""Get the country information."""
31+
results = []
32+
is_not_empty = (self.records and True) or False
33+
if (is_not_empty == False):
34+
raise ValueError("No record available.")
35+
if (country_code):
36+
results = {}
37+
if country_code in self.records:
38+
for i in range(0,len(self.fields)):
39+
results[self.fields[i]] = self.records[country_code][i]
40+
return results
41+
else:
42+
return {}
43+
44+
for key in self.records.keys():
45+
# print (key)
46+
data = {}
47+
for i in range(0,len(self.fields)):
48+
data[self.fields[i]] = self.records[key][i]
49+
results.append(data)
50+
return results

0 commit comments

Comments
 (0)