-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create .gitignore * Create CityProvinceQuery.py * Update CityProvinceQuery.py * Update README.md
- Loading branch information
Showing
2 changed files
with
59 additions
and
0 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
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
|
||
""" | ||
Author: Sajad Dehshiri <[email protected]> | ||
GitHub: https://github.com/sajaddp/list-of-cities-in-Iran | ||
If you find this useful, please consider starring the repository on GitHub. | ||
Python Version: 3.9 (Note: 3.11 features are not included as it hasn't been released as of September 2021) | ||
""" | ||
|
||
import json | ||
import os | ||
|
||
|
||
class CityProvinceQuery: | ||
|
||
def __init__(self): | ||
base_path = os.path.join(os.path.dirname( | ||
os.path.abspath(__file__)), '../../json') | ||
cities_file_path = os.path.join(base_path, 'cities.json') | ||
provinces_file_path = os.path.join(base_path, 'provinces.json') | ||
|
||
with open(cities_file_path, 'r', encoding='utf-8') as cities_file: | ||
self.cities = json.load(cities_file) | ||
|
||
with open(provinces_file_path, 'r', encoding='utf-8') as provinces_file: | ||
self.provinces = json.load(provinces_file) | ||
|
||
def get_all_cities(self): | ||
return self.cities | ||
|
||
def get_all_provinces(self): | ||
return self.provinces | ||
|
||
def get_cities_by_province_name(self, name): | ||
province = next((p for p in self.provinces if p['name'] == name), None) | ||
return [city for city in self.cities if city['province_id'] == province['id']] if province else [] | ||
|
||
def get_cities_by_province_id(self, province_id): | ||
return [city for city in self.cities if city['province_id'] == province_id] | ||
|
||
def get_cities_by_province_slug(self, slug): | ||
province = next((p for p in self.provinces if p['slug'] == slug), None) | ||
return [city for city in self.cities if city['province_id'] == province['id']] if province else [] | ||
|
||
def get_city_by_name(self, name): | ||
return [city for city in self.cities if city['name'] == name] | ||
|
||
def get_city_by_id(self, city_id): | ||
return [city for city in self.cities if city['id'] == city_id] | ||
|
||
def get_city_by_slug(self, slug): | ||
return [city for city in self.cities if city['slug'] == slug] |