Skip to content

Commit

Permalink
Implemented safer loading of json files
Browse files Browse the repository at this point in the history
  • Loading branch information
kaspersorensen committed Oct 16, 2019
1 parent 22b11b9 commit 5b33a51
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
9 changes: 5 additions & 4 deletions countryinfo/countryinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ def __init__(self, country_name=None):
self.__countries = {}
for file_path in __files_path:
if isfile(file_path):
country_info = json.load(open(file_path))
# pprint(country_info)
if country_info.get('name', None):
self.__countries[country_info['name'].lower()] = country_info
with open(file_path, encoding='utf-8') as file:
country_info = json.load(file)
# pprint(country_info)
if country_info.get('name', None):
self.__countries[country_info['name'].lower()] = country_info


def info(self):
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
exclude='tests'
),
include_package_data=True,
test_suite="tests.Tests",
data_files=[("data", data_files)], # package data files
url='https://github.com/porimol/countryinfo',
license='MIT License',
Expand Down
10 changes: 10 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import unittest
from countryinfo import CountryInfo

class Tests(unittest.TestCase):

def test_all_countries_have_names(self):
allCountries = CountryInfo().all()

if __name__ == '__main__':
unittest.main()

0 comments on commit 5b33a51

Please sign in to comment.