Skip to content

Commit

Permalink
Merge c5a315c into f86f26a
Browse files Browse the repository at this point in the history
  • Loading branch information
Urban Ishimwe committed Nov 29, 2019
2 parents f86f26a + c5a315c commit df9a8f8
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 35 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ var/
*.egg
*.eggs
.mypy_cache/
.vscode
.vscode
env/
9 changes: 0 additions & 9 deletions rwalocation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +0,0 @@
from .main import Province

app = Province()

def district():
print(app.district())

def province():
print(app.province())
3 changes: 1 addition & 2 deletions rwalocation/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -222627,8 +222627,7 @@
"cell_name": "Rutare",
"village_code": 507150505,
"village_name": "Shyara"
}
]</pre><pre style="word-wrap: break-word; white-space: pre-wrap;">[
},
{
"id": "fdf89758-99f0-4b6d-a793-e72f274c0fa1",
"country_code": "RWA",
Expand Down
89 changes: 72 additions & 17 deletions rwalocation/main.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,94 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

__version__ = "0.1.1"
__version__ = "0.1.2"

import requests
import operator

class Province:
def __init__(self):
self.districts = dict()
self.districtsArray = []
self.data_url = 'https://raw.githubusercontent.com/DevRW/rwanda-location/develop/src/db/locations.json'
self.sorted_data = self.sort_by_district(self.data_url)
self.districts = self.district() # the list of all districts equivalent to self.district()
self.provinces = self.province() # the list of all provinces equivalent to self.province()

def main(self):
self.province()
self.district()

@staticmethod
def sort_by_district(data_url):
response = requests.get(data_url)
sorted_x = sorted(response.json(), key=operator.itemgetter("district_name"), reverse=False)
return sorted_x

def province(self):
return self.sorted_data
def province(self) -> list:
temp = []
provincesArray = []
for value in self.sorted_data:
if(value['province_name'] not in temp):
temp.append(value['province_name'])
province_name = value['province_name']
province_code = value['province_code']
provincesArray.append({'province_name': province_name,'province_code': province_code})
return provincesArray

def district(self):
def district(self) -> list:
temp = []
districtsArray = []
for value in self.sorted_data:
if(value['district_name'] not in self.districts.values()):
district_name = self.districts['district_name'] = value['district_name']
district_code = self.districts['district_code'] = value['district_code']
province_name = self.districts['province_name'] = value['province_name']
self.districtsArray.append({'district_name': district_name, 'district_code': district_code, 'province_name': province_name})
return self.districtsArray
if(value['district_name'] not in temp):
temp.append(value['district_name'])
district_name = value['district_name']
district_code = value['district_code']
province_name = value['province_name']
districtsArray.append({'district_name': district_name, 'district_code': district_code, 'province_name': province_name})
return districtsArray

def allLocations(self) -> list:
return self.sorted_data

@staticmethod
def is_province_found(data, province) -> bool:
for instance in data:
if instance['province_name'].lower() == province.lower():
return True
return False

@staticmethod
def is_district_found(data, district) -> bool:
for instance in data:
if instance['district_name'].lower() == district.lower():
return True
return False

@staticmethod
def filter_array(array, attrib, value) -> list:
RESULT = []
for instance in array:
if instance[attrib].lower() == value.lower():
RESULT.append(instance)
return RESULT

@staticmethod
def search_array(array, value) -> list:
RESULT = []
for instance in array:
all = list(instance.values())
for one in all:
if str(one).lower() == str(value).lower():
RESULT.append(instance)
return RESULT

def getLocationsByProvince(self, province) -> list:
if self.is_province_found(self.provinces, str(province)):
return self.filter_array(self.sorted_data, 'province_name', str(province))
return []

def getLocationsByDistrict(self, district) -> list:
if self.is_district_found(self.districts, str(district)):
return self.filter_array(self.sorted_data, 'district_name', str(district))
return []

def getLocationsByName(self, name) -> list:
return self.search_array(self.sorted_data, str(name))

if __name__ == "__main__":
Province().main()
print(Province().allLocations())
7 changes: 5 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@
import os
from setuptools import find_packages, setup

DEPENDENCIES = []
EXCLUDE_FROM_PACKAGES = ["contrib", "docs", "tests*"]
CURDIR = os.path.abspath(os.path.dirname(__file__))

with io.open(os.path.join(CURDIR, "README.md"), "r", encoding="utf-8") as f:
README = f.read()


def read_requirement():
all_dep = open(os.path.realpath(os.path.dirname(os.path.abspath(__file__)) + '/' + 'requirements.txt')).readlines()
return [ dep.replace('\n', '') for dep in all_dep]

def get_version():
main_file = os.path.join(CURDIR, "rwalocation", "main.py")
_version_re = re.compile(r"__version__\s+=\s+(?P<version>.*)")
Expand All @@ -47,7 +50,7 @@ def get_version():
scripts=[],
entry_points={"console_scripts": ["rwalocation=rwalocation:main"]},
zip_safe=False,
install_requires=DEPENDENCIES,
install_requires=read_requirement(),
test_suite="tests.test_project",
python_requires=">=3.6",
# license and classifier list:
Expand Down
21 changes: 17 additions & 4 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,26 @@ def test_import(self):
self.assertIsNotNone(self.location)

def test_list_districts(self):
self.assertIsInstance(self.location.district(), list)
self.assertIsInstance(self.location.districts, list)

def test_list_province_not_zero(self):
self.assertNotEqual(len(self.location.province()), 0)
self.assertNotEqual(len(self.location.provinces), 0)

def test_list_province_return_list(self):
self.assertIsInstance(self.location.province(), list)
self.assertIsInstance(self.location.provinces, list)

def test_sort_by_district_return_an_array(self):
self.assertIsInstance(self.location.sort_by_district(), list)
self.assertIsInstance(self.location.sort_by_district(self.location.sorted_data), list)

def test_get_all_locations(self):
self.assertNotEqual(len(self.location.allLocations()), 0)

def test_get_locations_by_province(self):
self.assertNotEqual(len(self.location.getLocationsByProvince('kigali')), 0)

def test_get_locations_by_district(self):
self.assertNotEqual(len(self.location.getLocationsByDistrict('kicukiro')), 0)

def test_search_locations_by_name(self):
self.assertEqual(len(self.location.getLocationsByName('23434')), 0)

0 comments on commit df9a8f8

Please sign in to comment.