Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
119 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,5 @@ var/ | |
*.egg | ||
*.eggs | ||
.mypy_cache/ | ||
.vscode | ||
.vscode | ||
env/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters