Skip to content

Commit

Permalink
Merge e75ac0f into f86f26a
Browse files Browse the repository at this point in the history
  • Loading branch information
Urban Ishimwe committed Dec 9, 2019
2 parents f86f26a + e75ac0f commit db56482
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 27 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -15,4 +15,5 @@ var/
*.egg
*.eggs
.mypy_cache/
.vscode
.vscode
env/
16 changes: 14 additions & 2 deletions rwalocation/__init__.py
Expand Up @@ -3,7 +3,19 @@
app = Province()

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

def province():
print(app.province())
return app.province()

def all_locations():
return app.all_locations()

def get_locations_by_province(province_name):
return app.get_locations_by_province(province_name)

def get_locations_by_district(district_name):
return app.get_locations_by_district(district_name)

def get_locations_by_name(location_name):
return app.get_locations_by_name(location_name)
3 changes: 1 addition & 2 deletions rwalocation/data.json
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
97 changes: 81 additions & 16 deletions rwalocation/main.py
@@ -1,39 +1,104 @@
#!/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()

self.all_locations()
self.get_locations_by_district('kicukiro')
self.get_locations_by_name('kag')
self.get_locations_by_province('East')
self.get_locations_by_district('kicuk')
self.get_locations_by_name('kicukiro')
self.get_locations_by_province('eat')

def province(self):
temp = []
provinces_array = []
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']
provinces_array.append({'province_name': province_name,'province_code': province_code})
return provinces_array

def district(self):
temp = []
districts_array = []
for value in self.sorted_data:
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']
districts_array.append({'district_name': district_name, 'district_code': district_code, 'province_name': province_name})
return districts_array

def all_locations(self):
return self.sorted_data

def get_locations_by_province(self, province):
if self.is_province_found(self.provinces, str(province)):
return self.filter_array(self.sorted_data, 'province_name', str(province))
return []

def get_locations_by_district(self, district):
if self.is_district_found(self.districts, str(district)):
return self.filter_array(self.sorted_data, 'district_name', str(district))
return []

def get_locations_by_name(self, name):
return self.search_array(self.sorted_data, str(name))

@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
@staticmethod
def is_province_found(data, province):
for instance in data:
if instance['province_name'].lower() == province.lower():
return True
return False

def district(self):
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
@staticmethod
def is_district_found(data, district):
for instance in data:
if instance['district_name'].lower() == district.lower():
return True
return False

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

@staticmethod
def search_array(array, value):
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

if __name__ == "__main__":
Province().main()

7 changes: 5 additions & 2 deletions setup.py
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
20 changes: 16 additions & 4 deletions tests/test_project.py
Expand Up @@ -13,13 +13,25 @@ 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.all_locations()), 0)

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

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

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

0 comments on commit db56482

Please sign in to comment.