-
Notifications
You must be signed in to change notification settings - Fork 1
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
97 additions
and
35 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
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()) | ||
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,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()) |
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