Skip to content

Commit

Permalink
Feature/python (#33)
Browse files Browse the repository at this point in the history
* Create .gitignore

* Create CityProvinceQuery.py

* Update CityProvinceQuery.py

* Update README.md
  • Loading branch information
sajaddp authored Aug 15, 2023
1 parent 5a51fb4 commit ade7678
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
- لیست شهرستان های ایران با فرمت xlsx
- لیست شهرهای ایران با فرمت txt
- لیست شهرستان های ایران با فرمت txt
- لیست شهرهای ایران با فرمت Ts
- لیست شهرستان های ایران با فرمت Ts
- لیست شهرهای ایران با فرمت Py
- لیست شهرستان های ایران با فرمت Py

## نحوه استفاده

Expand Down Expand Up @@ -80,6 +84,10 @@ In this repository, you can access the list of cities and provinces of Iran in v
- List of provinces in Iran with xlsx format
- List of cities in Iran with txt format
- List of provinces in Iran with txt format
- List of cities in Iran with Ts format
- List of provinces in Iran with Ts format
- List of cities in Iran with Py format
- List of provinces in Iran with Py format

## Usage

Expand Down
51 changes: 51 additions & 0 deletions example/python/CityProvinceQuery.py
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]

0 comments on commit ade7678

Please sign in to comment.