From cf299dbec1985852b6eb7fbca7a4971519797071 Mon Sep 17 00:00:00 2001 From: Husnain_Khurshid Date: Wed, 4 Jan 2023 14:39:29 +0500 Subject: [PATCH 1/9] fix: (functions updated) --- hawqal/cities.py | 71 +++++++++++++++++++++++++++++++---------------- hawqal/country.py | 37 ++++++++---------------- hawqal/states.py | 37 ++++++++---------------- test_hawqal.py | 8 +++--- 4 files changed, 74 insertions(+), 79 deletions(-) diff --git a/hawqal/cities.py b/hawqal/cities.py index 41c885a..8e88ac2 100644 --- a/hawqal/cities.py +++ b/hawqal/cities.py @@ -32,35 +32,58 @@ def getCities(country="", state="", meta={}): f"SELECT city_name FROM cities ORDER BY city_name") cities = [city[0] for city in data] return cities - elif country != "" and type(country) != type({}) and state == "" and len(meta) == 0: + elif country != "" and state == "" and len(meta) == 0: data = cursor.execute( - f"SELECT city_name FROM cities WHERE (country_name='{string.capwords(country)}' OR state_name='{string.capwords(country)}') ORDER BY city_name") - return [city[0] for city in data] - - elif (type(country) == type({}) and state == "" and len(meta) == 0): - selectedFields = Filter.CityFilters(country) - if len(selectedFields) != 0: + f"SELECT city_name FROM cities WHERE country_name = '{string.capwords(country)}' ORDER BY city_name") + cities = [city[0] for city in data] + return cities + elif country == "" and state != "" and len(meta) == 0: + data = cursor.execute( + f"SELECT city_name FROM cities WHERE state_name = '{string.capwords(state)}' ORDER BY city_name") + cities = [city[0] for city in data] + return cities + elif country != "" and state != "" and len(meta) == 0: + data = cursor.execute( + f"SELECT city_name FROM cities WHERE country_name = '{string.capwords(country)}' AND state_name = '{string.capwords(state)}' ORDER BY city_name") + cities = [city[0] for city in data] + return cities + elif country != "" and state != "" and len(meta) != 0: + selectedFields = Filter.StateFilter(meta) + if len(selectedFields) > 0: data = cursor.execute( - f"SELECT city_name,{selectedFields} FROM cities ORDER BY city_name") - return [list(city) for city in list(data)] + f"SELECT city_name,{selectedFields} FROM cities WHERE country_name = '{string.capwords(country)}' AND state_name = '{string.capwords(state)}' ORDER BY city_name") + return [list(city) for city in data] else: data = cursor.execute( - f"SELECT city_name FROM cities ORDER BY city_name") - return [city[0] for city in list(data)] - - elif country != "" and type(state) == type({}) and len(meta) == 0: - selectedFields = Filter.CityFilters(state) - if len(selectedFields) != 0: + f"SELECT city_name FROM cities WHERE country_name = '{string.capwords(country)}' AND state_name = '{string.capwords(state)}' ORDER BY city_name") + return [city[0] for city in data] + elif country == "" and state != "" and len(meta) != 0: + selectedFields = Filter.StateFilter(meta) + if len(selectedFields) > 0: data = cursor.execute( - f"SELECT city_name,{selectedFields} FROM cities WHERE (country_name='{string.capwords(country)}' OR state_name='{string.capwords(country)}') ORDER BY city_name") - return [list(city) for city in list(data)] + f"SELECT city_name,{selectedFields} FROM cities WHERE state_name = '{string.capwords(state)}' ORDER BY city_name") + return [list(city) for city in data] else: data = cursor.execute( - f"SELECT city_name FROM cities WHERE (country_name='{string.capwords(country)}' OR state_name='{string.capwords(country)}') ORDER BY city_name") + f"SELECT city_name FROM cities WHERE state_name = '{string.capwords(state)}' ORDER BY city_name") + return [city[0] for city in data] + elif country != "" and state == "" and len(meta) != 0: + selectedFields = Filter.StateFilter(meta) + if len(selectedFields) > 0: + data = cursor.execute( + f"SELECT city_name,{selectedFields} FROM cities WHERE country_name = '{string.capwords(country)}' ORDER BY city_name") + return [list(city) for city in data] + else: + data = cursor.execute( + f"SELECT city_name FROM cities WHERE country_name = '{string.capwords(country)}' ORDER BY city_name") + return [city[0] for city in data] + elif country == "" and state == "" and len(meta) != 0: + selectedFields = Filter.StateFilter(meta) + if len(selectedFields) > 0: + data = cursor.execute( + f"SELECT city_name,{selectedFields} FROM cities ORDER BY city_name") + return[list(city) for city in data] + else: + data = cursor.execute( + f"SELECT city_name FROM cities ORDER BY city_name") return [city[0] for city in data] - - elif country != "" and state != "" and len(meta) > 0: - selectedFields = Filter.CityFilters(meta) - data = cursor.execute( - f"SELECT city_name,{selectedFields} FROM cities WHERE country_name='{string.capwords(string.capwords(country))}' AND state_name='{string.capwords(string.capwords(state))}' ORDER BY city_name") - return [list(city) for city in list(data)] diff --git a/hawqal/country.py b/hawqal/country.py index 5601c79..983f21b 100644 --- a/hawqal/country.py +++ b/hawqal/country.py @@ -9,10 +9,10 @@ class Country: @staticmethod def getCountries(country="", meta={}): """ - 1. Countries function takes two parameters as input country name and filters.\n + 1. Countries function takes two parameters as input country name and meta.\n 2. By default, function will return countries name.\n 3. Additional fields are included in filter.\n - 4. From filter of boolean TRUE fields will be included in output + 4. From meta TRUE fields will be included in output e.g { "coordinates": True, @@ -35,31 +35,16 @@ def getCountries(country="", meta={}): data = cursor.execute( f"SELECT country_name FROM countries ORDER BY country_name ASC") return [country[0] for country in list(data)] - - elif type(country) == type({}): - if type(meta) == type(""): - if meta != "": - selectedFields = Filter.CountryFilter(country) - data = cursor.execute( - f'SELECT country_name,{selectedFields} FROM countries WHERE country_name = "{string.capwords(meta)}"') - return [list(country) for country in data][0] - else: - selectedFields = Filter.CountryFilter(country) - data = cursor.execute( - f'SELECT country_name,{selectedFields} FROM countries') - return [list(country) for country in data] + elif country == "" and len(meta) != 0: + selectedFields = Filter.CountryFilter(meta) + if len(selectedFields) != 0: + data = cursor.execute( + f"SELECT country_name,{selectedFields} FROM countries ORDER BY country_name ASC") + return [list(country) for country in data] else: - meta, country = country, "" - selectedFields = Filter.CountryFilter(meta) - if len(selectedFields) != 0: - data = cursor.execute( - f'SELECT country_name,{selectedFields} FROM countries') - return [list(country) for country in data] - else: - data = cursor.execute( - f'SELECT country_name FROM countries') - return [country[0] for country in data] - + data = cursor.execute( + f"SELECT country_name FROM countries ORDER BY country_name ASC") + return [country[0] for country in list(data)] elif (country != "" and len(meta) > 0): selectedFields = Filter.CountryFilter(meta) if len(selectedFields) != 0: diff --git a/hawqal/states.py b/hawqal/states.py index 0e78990..31c7c27 100644 --- a/hawqal/states.py +++ b/hawqal/states.py @@ -31,43 +31,30 @@ def getStates(country="", meta={}): f'SELECT state_name FROM states') return [state[0] for state in data] - elif type(country) == type({}) and len(meta) == 0: - if type(meta) == type(""): - if meta != "": - selectedFields = Filter.CountryFilter(country) - data = cursor.execute( - f'SELECT state_name,{selectedFields} FROM states WHERE country_name = "{string.capwords(meta)}"') - return [list(country) for country in data] - else: - meta, country = country, "" - selectedFields = Filter.StateFilter(meta) - data = cursor.execute( - f'SELECT state_name,{selectedFields} FROM states') - return [list(country) for country in data] + elif country == "" and len(meta) != 0: + selectedFields = Filter.StateFilter(meta) + if len(selectedFields) != 0: + data = cursor.execute( + f'SELECT state_name,{selectedFields} FROM states') + return [list(state) for state in data] else: - selectedFields = Filter.CountryFilter(country) - if len(selectedFields) != 0: - data = cursor.execute( - f'SELECT state_name,{selectedFields} FROM states ORDER BY state_name') - return [list(country) for country in data] - else: - data = cursor.execute( - f'SELECT state_name FROM states ORDER BY state_name') - return [country[0] for country in data] + data = cursor.execute( + f'SELECT state_name FROM states') + return [state for state in data] elif country != "" and len(meta) > 0: selectedFields = Filter.StateFilter(meta) if len(selectedFields) != 0: data = cursor.execute( f'SELECT state_name,{selectedFields} FROM states WHERE country_name = "{string.capwords(country)}"') - return [list(country) for country in data] + return [list(state) for state in data] else: data = cursor.execute( f'SELECT state_name FROM states WHERE country_name = "{string.capwords(country)}"') - return [country[0] for country in data] + return [state[0] for state in data] elif country != "" and len(meta) == 0: selectedFields = Filter.StateFilter(meta) data = cursor.execute( f'SELECT state_name FROM states WHERE country_name = "{string.capwords(country)}"') - return [country[0] for country in list(data)] + return [state[0] for state in list(data)] diff --git a/test_hawqal.py b/test_hawqal.py index 145b0dc..00629e1 100644 --- a/test_hawqal.py +++ b/test_hawqal.py @@ -9,16 +9,16 @@ class TestFunc(unittest.TestCase): def test_getCountries(self): self.assertEqual(len(Country.getCountries()), 250) self.assertEqual(len(Country.getCountries( - "pakistan", {"coordinates": True})), 3) + country="pakistan", meta={"coordinates": True})), 3) def test_getStates(self): self.assertEqual(len(States.getStates()), 4989) - self.assertEqual(len(States.getStates("pakistan")), 8) + self.assertEqual(len(States.getStates(country="pakistan")), 8) def test_getCities(self): self.assertEqual(len(City.getCities()), 150710) - self.assertEqual(len(City.getCities("pakistan")), 458) - self.assertEqual(len(City.getCities("punjab")), 329) + self.assertEqual(len(City.getCities(country="pakistan")), 458) + self.assertEqual(len(City.getCities(state="punjab")), 329) if __name__ == '__main__': From 37cfda4ecdb66f6398ab68484e2afac593f7e68d Mon Sep 17 00:00:00 2001 From: hamzaanis Date: Wed, 4 Jan 2023 14:49:34 +0500 Subject: [PATCH 2/9] Simplifying the getting of data --- hawqal/country.py | 42 ++++++++++++------------------------------ 1 file changed, 12 insertions(+), 30 deletions(-) diff --git a/hawqal/country.py b/hawqal/country.py index 983f21b..c1475f1 100644 --- a/hawqal/country.py +++ b/hawqal/country.py @@ -7,7 +7,7 @@ class Country: @staticmethod - def getCountries(country="", meta={}): + def getCountries(meta={}): """ 1. Countries function takes two parameters as input country name and meta.\n 2. By default, function will return countries name.\n @@ -31,32 +31,14 @@ def getCountries(country="", meta={}): database = Database(file_name).makeConnection() cursor = database.cursor() - if country == "" and len(meta) == 0: - data = cursor.execute( - f"SELECT country_name FROM countries ORDER BY country_name ASC") - return [country[0] for country in list(data)] - elif country == "" and len(meta) != 0: - selectedFields = Filter.CountryFilter(meta) - if len(selectedFields) != 0: - data = cursor.execute( - f"SELECT country_name,{selectedFields} FROM countries ORDER BY country_name ASC") - return [list(country) for country in data] - else: - data = cursor.execute( - f"SELECT country_name FROM countries ORDER BY country_name ASC") - return [country[0] for country in list(data)] - elif (country != "" and len(meta) > 0): - selectedFields = Filter.CountryFilter(meta) - if len(selectedFields) != 0: - data = cursor.execute( - f'SELECT country_name,{selectedFields} FROM countries WHERE country_name = "{string.capwords(country)}"') - return [list(country) for country in data][0] - else: - data = cursor.execute( - f'SELECT country_name FROM countries WHERE country_name = "{string.capwords(country)}"') - return [list(country) for country in data][0] - - elif (country != "" and len(meta) == 0): - data = cursor.execute( - f'SELECT * FROM countries WHERE country_name = "{string.capwords(country)}"') - return [list(country) for country in data][0] + query = "SELECT" + + if len(meta) > 0: + query = query + f' {selectedFields}' + elif len(meta)==0: + query=query+" * " + + query = query + " FROM countries ORDER BY country_name ASC" + + data = cursor.execute(query) + return [country[0] for country in list(data)] From b22a22f8e7a2ac69e67e89e68b239cd20dfd65de Mon Sep 17 00:00:00 2001 From: hamzaanis Date: Wed, 4 Jan 2023 14:52:34 +0500 Subject: [PATCH 3/9] Added filter --- hawqal/country.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/hawqal/country.py b/hawqal/country.py index c1475f1..e795811 100644 --- a/hawqal/country.py +++ b/hawqal/country.py @@ -33,7 +33,9 @@ def getCountries(meta={}): query = "SELECT" + if len(meta) > 0: + selectedFields = Filter.CountryFilter(meta) query = query + f' {selectedFields}' elif len(meta)==0: query=query+" * " From 97af1289d5548edfbcc16089fe7da67fd199bec8 Mon Sep 17 00:00:00 2001 From: hamzaanis Date: Wed, 4 Jan 2023 15:50:09 +0500 Subject: [PATCH 4/9] Get state and get states --- hawqal/states.py | 86 +++++++++++++++++++++++++++++++----------------- 1 file changed, 56 insertions(+), 30 deletions(-) diff --git a/hawqal/states.py b/hawqal/states.py index 31c7c27..e72a679 100644 --- a/hawqal/states.py +++ b/hawqal/states.py @@ -7,7 +7,7 @@ class States: @staticmethod - def getStates(country="", meta={}): + def getStates(country_name="", meta={}): """ 1. States function takes two parameters as input state name and filters.\n 2. By default, function will return states name.\n @@ -26,35 +26,61 @@ def getStates(country="", meta={}): database = Database(file_name).makeConnection() cursor = database.cursor() - if country == "" and len(meta) == 0: - data = cursor.execute( - f'SELECT state_name FROM states') - return [state[0] for state in data] + query = "SELECT" - elif country == "" and len(meta) != 0: + if len(meta)>0: selectedFields = Filter.StateFilter(meta) - if len(selectedFields) != 0: - data = cursor.execute( - f'SELECT state_name,{selectedFields} FROM states') - return [list(state) for state in data] - else: - data = cursor.execute( - f'SELECT state_name FROM states') - return [state for state in data] - - elif country != "" and len(meta) > 0: - selectedFields = Filter.StateFilter(meta) - if len(selectedFields) != 0: - data = cursor.execute( - f'SELECT state_name,{selectedFields} FROM states WHERE country_name = "{string.capwords(country)}"') - return [list(state) for state in data] - else: - data = cursor.execute( - f'SELECT state_name FROM states WHERE country_name = "{string.capwords(country)}"') - return [state[0] for state in data] - - elif country != "" and len(meta) == 0: + query = query + f' {selectedFields}' + elif len(meta)==0: + query = query + " *" + + query = query + " FROM states" + + if len(country_name)>0: + query=query+ f' where country_name= "{string.capwords(country_name)}"' + + cursor.execute(query) + + data_json = [] + header = [i[0] for i in curr.description] + data = cursor.fetchall() + for i in data: + data_json.append(dict(zip(header, i))) + return data_json + + + @staticmethod + def getState(country_name="",state_name="", meta={}): + + file_name = os.path.join( + os.path.dirname(__file__), '..', 'database', 'hawqalDB.sqlite') + + with open(file_name, 'r', encoding="utf8") as db: + database = Database(file_name).makeConnection() + cursor = database.cursor() + + query = "SELECT" + + if len(meta)>0: selectedFields = Filter.StateFilter(meta) - data = cursor.execute( - f'SELECT state_name FROM states WHERE country_name = "{string.capwords(country)}"') - return [state[0] for state in list(data)] + query = query + f' {selectedFields}' + elif len(meta)==0: + query = query + " *" + + query = query + " FROM states where" + + if len(country_name)>0 and len(state_name)>0: + query = f' country_name= "{string.capwords(country_name)}" and state_name= "{string.capwords(state_name)}"' + elif len(country_name)>0: + query=query+ f' country_name= "{string.capwords(country_name)}"' + elif len(state_name)>0: + query=query+ f' state_name= "{string.capwords(state_name)}"' + + query = query + " ORDER BY country_name" + + data_json = [] + header = [i[0] for i in curr.description] + data = cursor.fetchall() + for i in data: + data_json.append(dict(zip(header, i))) + return data_json \ No newline at end of file From a15392e3da5d68c8bc515793cb4db9f36da007cd Mon Sep 17 00:00:00 2001 From: hamzaanis Date: Wed, 4 Jan 2023 15:53:43 +0500 Subject: [PATCH 5/9] Cursor execute not done for state --- hawqal/states.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/hawqal/states.py b/hawqal/states.py index e72a679..0e6a6fd 100644 --- a/hawqal/states.py +++ b/hawqal/states.py @@ -78,8 +78,10 @@ def getState(country_name="",state_name="", meta={}): query = query + " ORDER BY country_name" + cursor.execute(query) + data_json = [] - header = [i[0] for i in curr.description] + header = [i[0] for i in cursor.description] data = cursor.fetchall() for i in data: data_json.append(dict(zip(header, i))) From 8cc2ee469b525e50c60f48e48c7227679cd74764 Mon Sep 17 00:00:00 2001 From: Husnain_Khurshid Date: Wed, 4 Jan 2023 16:28:35 +0500 Subject: [PATCH 6/9] fix: (fixed state function) --- hawqal/__init__.py | 3 ++- hawqal/states.py | 32 ++++++++++++++++---------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/hawqal/__init__.py b/hawqal/__init__.py index 7fb398c..94b4e2b 100644 --- a/hawqal/__init__.py +++ b/hawqal/__init__.py @@ -5,5 +5,6 @@ getCities = City.getCities getCountries = Country.getCountries getStates = States.getStates +getState = States.getState -__all__ = [getCities, getCountries, getStates] +__all__ = [getCities, getCountries, getStates, getState] diff --git a/hawqal/states.py b/hawqal/states.py index 0e6a6fd..354bc31 100644 --- a/hawqal/states.py +++ b/hawqal/states.py @@ -28,30 +28,30 @@ def getStates(country_name="", meta={}): query = "SELECT" - if len(meta)>0: + if len(meta) > 0: selectedFields = Filter.StateFilter(meta) query = query + f' {selectedFields}' - elif len(meta)==0: + elif len(meta) == 0: query = query + " *" query = query + " FROM states" - if len(country_name)>0: - query=query+ f' where country_name= "{string.capwords(country_name)}"' + if len(country_name) > 0: + query = query + \ + f' where country_name= "{string.capwords(country_name)}"' cursor.execute(query) data_json = [] - header = [i[0] for i in curr.description] + header = [i[0] for i in cursor.description] data = cursor.fetchall() for i in data: data_json.append(dict(zip(header, i))) return data_json - @staticmethod - def getState(country_name="",state_name="", meta={}): - + def getState(country_name="", state_name="", meta={}): + file_name = os.path.join( os.path.dirname(__file__), '..', 'database', 'hawqalDB.sqlite') @@ -61,20 +61,20 @@ def getState(country_name="",state_name="", meta={}): query = "SELECT" - if len(meta)>0: + if len(meta) > 0: selectedFields = Filter.StateFilter(meta) query = query + f' {selectedFields}' - elif len(meta)==0: + elif len(meta) == 0: query = query + " *" query = query + " FROM states where" - if len(country_name)>0 and len(state_name)>0: + if len(country_name) > 0 and len(state_name) > 0: query = f' country_name= "{string.capwords(country_name)}" and state_name= "{string.capwords(state_name)}"' - elif len(country_name)>0: - query=query+ f' country_name= "{string.capwords(country_name)}"' - elif len(state_name)>0: - query=query+ f' state_name= "{string.capwords(state_name)}"' + elif len(country_name) > 0: + query = query + f' country_name= "{string.capwords(country_name)}"' + elif len(state_name) > 0: + query = query + f' state_name= "{string.capwords(state_name)}"' query = query + " ORDER BY country_name" @@ -85,4 +85,4 @@ def getState(country_name="",state_name="", meta={}): data = cursor.fetchall() for i in data: data_json.append(dict(zip(header, i))) - return data_json \ No newline at end of file + return data_json From d9e9086a9227bbef6f1e79585a7388e74fdfc433 Mon Sep 17 00:00:00 2001 From: hamzaanis Date: Wed, 4 Jan 2023 17:00:33 +0500 Subject: [PATCH 7/9] State filter added --- hawqal/__init__.py | 6 +++- hawqal/cities.py | 1 - hawqal/country.py | 4 +-- {filter => hawqal/filters}/filter.py | 2 +- hawqal/filters/state_filter.py | 24 +++++++++++++ hawqal/states.py | 54 +++++++++++----------------- 6 files changed, 52 insertions(+), 39 deletions(-) rename {filter => hawqal/filters}/filter.py (98%) create mode 100644 hawqal/filters/state_filter.py diff --git a/hawqal/__init__.py b/hawqal/__init__.py index 7fb398c..7ba367e 100644 --- a/hawqal/__init__.py +++ b/hawqal/__init__.py @@ -1,9 +1,13 @@ from hawqal.cities import City from hawqal.country import Country from hawqal.states import States +from .filters.state_filter import StateFilter getCities = City.getCities getCountries = Country.getCountries getStates = States.getStates +getState = States.getState -__all__ = [getCities, getCountries, getStates] +# filters = [StateFilter] +# functions = [getCities, getCountries, getStates, getState] +__all__ = [getCities, getCountries, getStates, getState] diff --git a/hawqal/cities.py b/hawqal/cities.py index 8e88ac2..4b19672 100644 --- a/hawqal/cities.py +++ b/hawqal/cities.py @@ -1,5 +1,4 @@ from dal.dao import Database -from filter.filter import Filter import string import os diff --git a/hawqal/country.py b/hawqal/country.py index e795811..669469c 100644 --- a/hawqal/country.py +++ b/hawqal/country.py @@ -1,8 +1,6 @@ from dal.dao import Database -from filter.filter import Filter +from .filters.filter import Filter import os -import string - class Country: diff --git a/filter/filter.py b/hawqal/filters/filter.py similarity index 98% rename from filter/filter.py rename to hawqal/filters/filter.py index d279480..def18d4 100644 --- a/filter/filter.py +++ b/hawqal/filters/filter.py @@ -50,4 +50,4 @@ def CityFilters(meta): for key, value in meta.items(): if value: fields = fields + keyArrtibutes[key] + ',' - return fields[:-1] + return fields[:-1] \ No newline at end of file diff --git a/hawqal/filters/state_filter.py b/hawqal/filters/state_filter.py new file mode 100644 index 0000000..01324d0 --- /dev/null +++ b/hawqal/filters/state_filter.py @@ -0,0 +1,24 @@ +class StateFilter: + def __init__(self, state_id=True, state_name=True, country_name=True, longitude=True, latitude=True): + print("Param") + print(state_id) + self.state_id = state_id + self.state_name = state_name + self.country_name = country_name + self.longitude = longitude + self.latitude = latitude + + def __str__(self): + fields = "" + if self.state_id: + fields = fields+'state_id,' + if self.state_name: + fields = fields+' state_name,' + if self.country_name: + fields = fields + 'country_name,' + if self.longitude: + fields = fields + 'longitude,' + if self.latitude: + fields = fields + 'latitude,' + + return fields[:-1]+' ' diff --git a/hawqal/states.py b/hawqal/states.py index 0e6a6fd..e6a24dc 100644 --- a/hawqal/states.py +++ b/hawqal/states.py @@ -1,5 +1,5 @@ from dal.dao import Database -from filter.filter import Filter +from .filters.state_filter import StateFilter import string import os @@ -7,7 +7,7 @@ class States: @staticmethod - def getStates(country_name="", meta={}): + def getStates(country_name="", filter=StateFilter()): """ 1. States function takes two parameters as input state name and filters.\n 2. By default, function will return states name.\n @@ -26,32 +26,28 @@ def getStates(country_name="", meta={}): database = Database(file_name).makeConnection() cursor = database.cursor() - query = "SELECT" + query = "SELECT " + str(filter) + " FROM states" - if len(meta)>0: - selectedFields = Filter.StateFilter(meta) - query = query + f' {selectedFields}' - elif len(meta)==0: - query = query + " *" - - query = query + " FROM states" - - if len(country_name)>0: - query=query+ f' where country_name= "{string.capwords(country_name)}"' + if len(country_name) > 0: + query = query + \ + f' where country_name= "{string.capwords(country_name)}"' + print(query) + print(filter) cursor.execute(query) data_json = [] - header = [i[0] for i in curr.description] + header = [i[0] for i in cursor.description] data = cursor.fetchall() for i in data: data_json.append(dict(zip(header, i))) return data_json - @staticmethod - def getState(country_name="",state_name="", meta={}): - + def getState(country_name="", state_name="", filter=StateFilter()): + if state_name == "": + raise ValueError("state_name must be set") + file_name = os.path.join( os.path.dirname(__file__), '..', 'database', 'hawqalDB.sqlite') @@ -59,22 +55,14 @@ def getState(country_name="",state_name="", meta={}): database = Database(file_name).makeConnection() cursor = database.cursor() - query = "SELECT" - - if len(meta)>0: - selectedFields = Filter.StateFilter(meta) - query = query + f' {selectedFields}' - elif len(meta)==0: - query = query + " *" + query = "SELECT " + filter + " FROM states" - query = query + " FROM states where" - - if len(country_name)>0 and len(state_name)>0: - query = f' country_name= "{string.capwords(country_name)}" and state_name= "{string.capwords(state_name)}"' - elif len(country_name)>0: - query=query+ f' country_name= "{string.capwords(country_name)}"' - elif len(state_name)>0: - query=query+ f' state_name= "{string.capwords(state_name)}"' + if len(country_name) > 0 and len(state_name) > 0: + query = f'where country_name= "{string.capwords(country_name)}" and state_name= "{string.capwords(state_name)}"' + elif len(country_name) > 0: + query = query + f' country_name= "{string.capwords(country_name)}"' + elif len(state_name) > 0: + query = query + f' state_name= "{string.capwords(state_name)}"' query = query + " ORDER BY country_name" @@ -85,4 +73,4 @@ def getState(country_name="",state_name="", meta={}): data = cursor.fetchall() for i in data: data_json.append(dict(zip(header, i))) - return data_json \ No newline at end of file + return data_json From 9b67a75291ac1b811f89a2534a1e7a37c3d5f167 Mon Sep 17 00:00:00 2001 From: Husnain_Khurshid Date: Wed, 4 Jan 2023 18:57:39 +0500 Subject: [PATCH 8/9] fix: (fixed city,country and state functions along with their filters) --- Json/Query.py | 7 ++ hawqal/__init__.py | 10 ++- hawqal/cities.py | 109 ++++++++++++++----------------- hawqal/country.py | 61 +++++++++++++---- hawqal/filters/city_filter.py | 28 ++++++++ hawqal/filters/country_filter.py | 52 +++++++++++++++ hawqal/filters/state_filter.py | 2 - hawqal/states.py | 33 +++------- 8 files changed, 199 insertions(+), 103 deletions(-) create mode 100644 Json/Query.py create mode 100644 hawqal/filters/city_filter.py create mode 100644 hawqal/filters/country_filter.py diff --git a/Json/Query.py b/Json/Query.py new file mode 100644 index 0000000..6c07219 --- /dev/null +++ b/Json/Query.py @@ -0,0 +1,7 @@ +def convertJson(cursor): + data_json = [] + header = [i[0] for i in cursor.description] + data = cursor.fetchall() + for i in data: + data_json.append(dict(zip(header, i))) + return data_json diff --git a/hawqal/__init__.py b/hawqal/__init__.py index 7ba367e..7c4c6d1 100644 --- a/hawqal/__init__.py +++ b/hawqal/__init__.py @@ -2,12 +2,16 @@ from hawqal.country import Country from hawqal.states import States from .filters.state_filter import StateFilter +from .filters.city_filter import CityFilter +from .filters.country_filter import CountryFilter + getCities = City.getCities +getCity = City.getCity getCountries = Country.getCountries +getCountry = Country.getCountry getStates = States.getStates getState = States.getState -# filters = [StateFilter] -# functions = [getCities, getCountries, getStates, getState] -__all__ = [getCities, getCountries, getStates, getState] +__all__ = [getCountries, getStates, + getState, StateFilter, CountryFilter, getCountry, getCities, CityFilter, getCity] diff --git a/hawqal/cities.py b/hawqal/cities.py index 4b19672..c57080d 100644 --- a/hawqal/cities.py +++ b/hawqal/cities.py @@ -1,12 +1,14 @@ from dal.dao import Database import string import os +from .filters.city_filter import CityFilter +from Json.Query import convertJson class City: @staticmethod - def getCities(country="", state="", meta={}): + def getCities(country_name="", state_name="", filter=CityFilter()): """ 1. Countries function takes two parameters as input country name and filters.\n 2. By default, function will return countries name.\n @@ -19,6 +21,44 @@ def getCities(country="", state="", meta={}): "state":True } """ + + file_name = os.path.join( + os.path.dirname(__file__), '..', 'database', 'hawqalDB.sqlite') + + with open(file_name, 'r', encoding="utf8") as db: + database = Database(file_name).makeConnection() + cursor = database.cursor() + + query = "SELECT " + str(filter) + " FROM cities" + + if country_name != "": + query = query + \ + f" WHERE country_name = '{string.capwords(country_name)}'" + elif state_name != "": + query = query + \ + f" WHERE state_name = '{string.capwords(state_name)}'" + + cursor.execute(query) + + return convertJson(cursor) + + @staticmethod + def getCity(country_name="", state_name="", city_name="", filter=CityFilter()): + """ + 1. Countries function takes two parameters as input country name and filters.\n + 2. By default, function will return countries name.\n + 3. Additional fields are included in filter.\n + 4. From filter of boolean TRUE fields will be included in output + e.g + { + "coordinates": True, + "country": True, + "state":True + } + """ + if country_name == "" or state_name == "" or city_name == "": + raise ValueError("country,state and city name must be set") + file_name = os.path.join( os.path.dirname(__file__), '..', 'database', 'hawqalDB.sqlite') @@ -26,63 +66,10 @@ def getCities(country="", state="", meta={}): database = Database(file_name).makeConnection() cursor = database.cursor() - if state == "" and country == "" and len(meta) == 0: - data = cursor.execute( - f"SELECT city_name FROM cities ORDER BY city_name") - cities = [city[0] for city in data] - return cities - elif country != "" and state == "" and len(meta) == 0: - data = cursor.execute( - f"SELECT city_name FROM cities WHERE country_name = '{string.capwords(country)}' ORDER BY city_name") - cities = [city[0] for city in data] - return cities - elif country == "" and state != "" and len(meta) == 0: - data = cursor.execute( - f"SELECT city_name FROM cities WHERE state_name = '{string.capwords(state)}' ORDER BY city_name") - cities = [city[0] for city in data] - return cities - elif country != "" and state != "" and len(meta) == 0: - data = cursor.execute( - f"SELECT city_name FROM cities WHERE country_name = '{string.capwords(country)}' AND state_name = '{string.capwords(state)}' ORDER BY city_name") - cities = [city[0] for city in data] - return cities - elif country != "" and state != "" and len(meta) != 0: - selectedFields = Filter.StateFilter(meta) - if len(selectedFields) > 0: - data = cursor.execute( - f"SELECT city_name,{selectedFields} FROM cities WHERE country_name = '{string.capwords(country)}' AND state_name = '{string.capwords(state)}' ORDER BY city_name") - return [list(city) for city in data] - else: - data = cursor.execute( - f"SELECT city_name FROM cities WHERE country_name = '{string.capwords(country)}' AND state_name = '{string.capwords(state)}' ORDER BY city_name") - return [city[0] for city in data] - elif country == "" and state != "" and len(meta) != 0: - selectedFields = Filter.StateFilter(meta) - if len(selectedFields) > 0: - data = cursor.execute( - f"SELECT city_name,{selectedFields} FROM cities WHERE state_name = '{string.capwords(state)}' ORDER BY city_name") - return [list(city) for city in data] - else: - data = cursor.execute( - f"SELECT city_name FROM cities WHERE state_name = '{string.capwords(state)}' ORDER BY city_name") - return [city[0] for city in data] - elif country != "" and state == "" and len(meta) != 0: - selectedFields = Filter.StateFilter(meta) - if len(selectedFields) > 0: - data = cursor.execute( - f"SELECT city_name,{selectedFields} FROM cities WHERE country_name = '{string.capwords(country)}' ORDER BY city_name") - return [list(city) for city in data] - else: - data = cursor.execute( - f"SELECT city_name FROM cities WHERE country_name = '{string.capwords(country)}' ORDER BY city_name") - return [city[0] for city in data] - elif country == "" and state == "" and len(meta) != 0: - selectedFields = Filter.StateFilter(meta) - if len(selectedFields) > 0: - data = cursor.execute( - f"SELECT city_name,{selectedFields} FROM cities ORDER BY city_name") - return[list(city) for city in data] - else: - data = cursor.execute( - f"SELECT city_name FROM cities ORDER BY city_name") - return [city[0] for city in data] + query = "SELECT " + \ + str(filter) + \ + f" FROM cities WHERE country_name='{string.capwords(country_name)}' AND state_name='{string.capwords(state_name)}' AND city_name = '{string.capwords(city_name)}'" + + cursor.execute(query) + + return convertJson(cursor) diff --git a/hawqal/country.py b/hawqal/country.py index 669469c..9a66f1c 100644 --- a/hawqal/country.py +++ b/hawqal/country.py @@ -1,11 +1,15 @@ from dal.dao import Database from .filters.filter import Filter +from Json.Query import convertJson +from .filters.country_filter import CountryFilter +import string import os + class Country: @staticmethod - def getCountries(meta={}): + def getCountries(filter=CountryFilter()): """ 1. Countries function takes two parameters as input country name and meta.\n 2. By default, function will return countries name.\n @@ -21,24 +25,55 @@ def getCountries(meta={}): } """ + file_name = os.path.join( + os.path.dirname(__file__), '..', 'database', 'hawqalDB.sqlite') + + with open(file_name, 'r', encoding="utf8") as db: + database = Database(file_name).makeConnection() + cursor = database.cursor() + + query = "SELECT " + str(filter) + + query = query + " FROM countries ORDER BY country_name ASC" + print(query) + cursor.execute(query) + + return convertJson(cursor) + + @staticmethod + def getCountry(country_name="", filter=CountryFilter()): + """ + 1. Countries function takes two parameters as input country name and meta.\n + 2. By default, function will return countries name.\n + 3. Additional fields are included in filter.\n + 4. From meta TRUE fields will be included in output + e.g + { + "coordinates": True, + "region": True, + "currency": True, + "timezone": True, + "capital": True + } - file_name = os.path.join(os.path.dirname( - __file__), '..', 'database', 'hawqalDB.sqlite') + """ + file_name = os.path.join( + os.path.dirname(__file__), '..', 'database', 'hawqalDB.sqlite') with open(file_name, 'r', encoding="utf8") as db: database = Database(file_name).makeConnection() cursor = database.cursor() - query = "SELECT" + if country_name == "": + raise ValueError("country_name must be set") + query = "SELECT " + str(filter) + " FROM countries" - if len(meta) > 0: - selectedFields = Filter.CountryFilter(meta) - query = query + f' {selectedFields}' - elif len(meta)==0: - query=query+" * " - - query = query + " FROM countries ORDER BY country_name ASC" + if country_name != "": + + query = query + \ + f" WHERE country_name = '{string.capwords(country_name)}' ORDER BY country_name ASC" + + cursor.execute(query) - data = cursor.execute(query) - return [country[0] for country in list(data)] + return convertJson(cursor) diff --git a/hawqal/filters/city_filter.py b/hawqal/filters/city_filter.py new file mode 100644 index 0000000..2128c2d --- /dev/null +++ b/hawqal/filters/city_filter.py @@ -0,0 +1,28 @@ +class CityFilter: + def __init__(self, city_id=True, city_name=True, state_name=True, state_id=True, country_name=True, latitude=True, longitude=True): + self.country_name = country_name + self.longitude = longitude + self.latitude = latitude + self.city_id = city_id + self.city_name = city_name + self.state_name = state_name + self.state_id = state_id + + def __str__(self): + fields = "" + if self.country_name: + fields = fields+'country_name,' + if self.longitude: + fields = fields+' longitude,' + if self.latitude: + fields = fields + 'latitude,' + if self.city_id: + fields = fields+'city_id,' + if self.city_name: + fields = fields+' city_name,' + if self.state_name: + fields = fields + 'state_name,' + if self.state_id: + fields = fields + 'state_id,' + + return fields[:-1]+' ' diff --git a/hawqal/filters/country_filter.py b/hawqal/filters/country_filter.py new file mode 100644 index 0000000..ac90f22 --- /dev/null +++ b/hawqal/filters/country_filter.py @@ -0,0 +1,52 @@ +class CountryFilter: + def __init__(self, country_name=True, iso_code=True, phone_code=True, capital=True, currency=True, currency_name=True, currency_symbol=True, country_domain=True, region=True, subregion=-True, timezone=True, zone_city=True, UTC=True, latitude=True, longitude=True): + self.country_name = country_name + self.longitude = longitude + self.latitude = latitude + self.iso_code = iso_code + self.phone_code = phone_code + self.capital = capital + self.currency = currency + self.currency_name = currency_name + self.currency_symbol = currency_symbol + self.country_domain = country_domain + self.region = region + self.subregion = subregion + self.timezone = timezone + self.zone_city = zone_city + self.UTC = UTC + + def __str__(self): + fields = "" + if self.country_name: + fields = fields+'country_name,' + if self.longitude: + fields = fields+' longitude,' + if self.latitude: + fields = fields + 'latitude,' + if self.iso_code: + fields = fields + 'iso_code,' + if self.phone_code: + fields = fields + 'phone_code,' + if self.capital: + fields = fields+'capital,' + if self.currency: + fields = fields+' currency,' + if self.currency_name: + fields = fields + 'currency_name,' + if self.currency_symbol: + fields = fields + 'currency_symbol,' + if self.country_domain: + fields = fields + 'country_domain,' + if self.region: + fields = fields + 'region,' + if self.subregion: + fields = fields + 'subregion,' + if self.timezone: + fields = fields + 'timezone,' + if self.zone_city: + fields = fields + 'zone_city,' + if self.UTC: + fields = fields + 'UTC,' + + return fields[:-1]+' ' diff --git a/hawqal/filters/state_filter.py b/hawqal/filters/state_filter.py index 01324d0..dd1beb0 100644 --- a/hawqal/filters/state_filter.py +++ b/hawqal/filters/state_filter.py @@ -1,7 +1,5 @@ class StateFilter: def __init__(self, state_id=True, state_name=True, country_name=True, longitude=True, latitude=True): - print("Param") - print(state_id) self.state_id = state_id self.state_name = state_name self.country_name = country_name diff --git a/hawqal/states.py b/hawqal/states.py index 9660986..7ac1036 100644 --- a/hawqal/states.py +++ b/hawqal/states.py @@ -1,6 +1,7 @@ from dal.dao import Database from .filters.state_filter import StateFilter import string +from Json.Query import convertJson import os @@ -19,6 +20,7 @@ def getStates(country_name="", filter=StateFilter()): } """ + file_name = os.path.join( os.path.dirname(__file__), '..', 'database', 'hawqalDB.sqlite') @@ -32,45 +34,28 @@ def getStates(country_name="", filter=StateFilter()): query = query + \ f' where country_name= "{string.capwords(country_name)}"' - print(query) - print(filter) cursor.execute(query) - data_json = [] - header = [i[0] for i in cursor.description] - data = cursor.fetchall() - for i in data: - data_json.append(dict(zip(header, i))) - return data_json + return convertJson(cursor) @staticmethod def getState(country_name="", state_name="", filter=StateFilter()): if state_name == "": raise ValueError("state_name must be set") - file_name = os.path.join( - os.path.dirname(__file__), '..', 'database', 'hawqalDB.sqlite') - - with open(file_name, 'r', encoding="utf8") as db: - database = Database(file_name).makeConnection() - cursor = database.cursor() - query = "SELECT " + filter + " FROM states" - + cursor = Database.database_connection() if len(country_name) > 0 and len(state_name) > 0: query = f'where country_name= "{string.capwords(country_name)}" and state_name= "{string.capwords(state_name)}"' elif len(country_name) > 0: - query = query + f'where country_name= "{string.capwords(country_name)}"' + query = query + \ + f'where country_name= "{string.capwords(country_name)}"' elif len(state_name) > 0: - query = query + f'where state_name= "{string.capwords(state_name)}"' + query = query + \ + f'where state_name= "{string.capwords(state_name)}"' query = query + " ORDER BY country_name" cursor.execute(query) - data_json = [] - header = [i[0] for i in cursor.description] - data = cursor.fetchall() - for i in data: - data_json.append(dict(zip(header, i))) - return data_json + return convertJson(cursor) From b74adb8a2d3ab3dc444e46471714fcd899602920 Mon Sep 17 00:00:00 2001 From: Hussnain12 <“hussnain.ml.fiver@gmail.com”> Date: Wed, 4 Jan 2023 21:26:32 +0500 Subject: [PATCH 9/9] updated cities file and fixed __str__ method in filters files --- dal/dao.py | 21 +++--- hawqal/cities.py | 120 +++++++++++++++++++------------ hawqal/country.py | 33 +++------ hawqal/filters/city_filter.py | 23 ++---- hawqal/filters/country_filter.py | 38 ++-------- hawqal/filters/state_filter.py | 16 ++--- hawqal/states.py | 22 +++--- 7 files changed, 117 insertions(+), 156 deletions(-) diff --git a/dal/dao.py b/dal/dao.py index 55b125b..149ec52 100644 --- a/dal/dao.py +++ b/dal/dao.py @@ -1,13 +1,10 @@ import sqlite3 - -''' -Connecting with database -''' - - -class Database: - def __init__(self, path): - self.databasePath = path - - def makeConnection(self): - return sqlite3.connect(f"{self.databasePath}") +import os + +def create_cursor(): + dirname = os.path.dirname(__file__) + file_name = os.path.join(dirname, '..', 'database', 'hawqalDB.sqlite') + with open(file_name, 'r', encoding="utf8"): + database = sqlite3.connect(file_name) + cursor = database.cursor() + return cursor \ No newline at end of file diff --git a/hawqal/cities.py b/hawqal/cities.py index c57080d..fed0da2 100644 --- a/hawqal/cities.py +++ b/hawqal/cities.py @@ -1,4 +1,4 @@ -from dal.dao import Database +from dal.dao import create_cursor import string import os from .filters.city_filter import CityFilter @@ -8,67 +8,93 @@ class City: @staticmethod - def getCities(country_name="", state_name="", filter=CityFilter()): + def getCities(country_name="", state_name="", filters=CityFilter()): """ - 1. Countries function takes two parameters as input country name and filters.\n - 2. By default, function will return countries name.\n - 3. Additional fields are included in filter.\n - 4. From filter of boolean TRUE fields will be included in output - e.g - { - "coordinates": True, - "country": True, - "state":True - } + it takes 3 optional parameters and return JSON object. + + Default: + By default it will return all the cities with all the fields including + + - city_id + + Args: + country_name (str): Optional - To get the cities of some specific country. + state_name (str): Optional - To get the cities of some specific state. + filters (CityFilter): Optional - A class object specifying which fields to include in the results. + + Returns: + list: A json object containing the requested information about the cities. + + Example: + - To get all cities + + ``` + hawqal.getCities() + ``` + + - To get all cities of some country + + ``` + hawqal.getCities(country_name="Pakistan") + ``` + + - To get all cities of some state in a country + + ``` + hawqal.getCities(country_name="Pakistan",state_name="Punjab") + ``` + + - To apply filters and exclude state_name + ``` + hawqal.getCities(filter=hawqal.CityFilter(state_name=False)) + ``` """ - - file_name = os.path.join( - os.path.dirname(__file__), '..', 'database', 'hawqalDB.sqlite') - - with open(file_name, 'r', encoding="utf8") as db: - database = Database(file_name).makeConnection() - cursor = database.cursor() - - query = "SELECT " + str(filter) + " FROM cities" + cursor = create_cursor() + query = "SELECT " + str(filters) + " FROM cities" if country_name != "": query = query + \ - f" WHERE country_name = '{string.capwords(country_name)}'" + f" WHERE country_name = '{string.capwords(country_name)}'" elif state_name != "": query = query + \ - f" WHERE state_name = '{string.capwords(state_name)}'" + f" WHERE state_name = '{string.capwords(state_name)}'" cursor.execute(query) return convertJson(cursor) @staticmethod - def getCity(country_name="", state_name="", city_name="", filter=CityFilter()): + def getCity(country_name="", state_name="", city_name="", filters=CityFilter()): """ - 1. Countries function takes two parameters as input country name and filters.\n - 2. By default, function will return countries name.\n - 3. Additional fields are included in filter.\n - 4. From filter of boolean TRUE fields will be included in output - e.g - { - "coordinates": True, - "country": True, - "state":True - } + Retrieves information about a city from the database. + + Parameters: + country_name (str): The name of the country where the city is located. Optional. + state_name (str): The name of the state or province where the city is located. Optional. + city_name (str): The name of the city. Required. + filters (CityFilter): An object specifying which fields to include in the results. Optional. + + Returns: + A Json object containing the requested information about the city. + + Raises: + ValueError: If `city_name` is not specified. """ - if country_name == "" or state_name == "" or city_name == "": - raise ValueError("country,state and city name must be set") - - file_name = os.path.join( - os.path.dirname(__file__), '..', 'database', 'hawqalDB.sqlite') - - with open(file_name, 'r', encoding="utf8") as db: - database = Database(file_name).makeConnection() - cursor = database.cursor() - - query = "SELECT " + \ - str(filter) + \ - f" FROM cities WHERE country_name='{string.capwords(country_name)}' AND state_name='{string.capwords(state_name)}' AND city_name = '{string.capwords(city_name)}'" + cursor = create_cursor() + country_name = string.capwords(country_name) + state_name = string.capwords(state_name) + city_name = string.capwords(city_name) + if city_name == "": + raise ValueError("City name must be set") + query = "SELECT " + str(filters) + " FROM CITIES " + if country_name != "": + query = query + f"WHERE country_name='{country_name}' AND city_name='{city_name}' " + elif state_name != "": + query = query + f"Where state_name='{state_name}' and city_name='{city_name}'" + elif country_name != "" and state_name != "": + query = query + f"Where state_name='{state_name}' and country_name='{country_name} and city_name='{city_name}' '" + else: + query = query + f"where city_name='{city_name}'" cursor.execute(query) diff --git a/hawqal/country.py b/hawqal/country.py index 9a66f1c..25e39b7 100644 --- a/hawqal/country.py +++ b/hawqal/country.py @@ -1,5 +1,5 @@ -from dal.dao import Database -from .filters.filter import Filter +from dal.dao import create_cursor +# from .filters.filter import Filter from Json.Query import convertJson from .filters.country_filter import CountryFilter import string @@ -9,13 +9,14 @@ class Country: @staticmethod - def getCountries(filter=CountryFilter()): + def getCountries(filters=CountryFilter()): """ - 1. Countries function takes two parameters as input country name and meta.\n - 2. By default, function will return countries name.\n + 1. getCountries function takes an opti\n + 2. By default, function will return countries name with all the filters.\n 3. Additional fields are included in filter.\n 4. From meta TRUE fields will be included in output e.g + { "coordinates": True, "region": True, @@ -25,14 +26,8 @@ def getCountries(filter=CountryFilter()): } """ - file_name = os.path.join( - os.path.dirname(__file__), '..', 'database', 'hawqalDB.sqlite') - - with open(file_name, 'r', encoding="utf8") as db: - database = Database(file_name).makeConnection() - cursor = database.cursor() - - query = "SELECT " + str(filter) + cursor=create_cursor() + query = "SELECT " + str(filters) query = query + " FROM countries ORDER BY country_name ASC" print(query) @@ -41,7 +36,7 @@ def getCountries(filter=CountryFilter()): return convertJson(cursor) @staticmethod - def getCountry(country_name="", filter=CountryFilter()): + def getCountry(country_name="", filters=CountryFilter()): """ 1. Countries function takes two parameters as input country name and meta.\n 2. By default, function will return countries name.\n @@ -57,17 +52,11 @@ def getCountry(country_name="", filter=CountryFilter()): } """ - file_name = os.path.join( - os.path.dirname(__file__), '..', 'database', 'hawqalDB.sqlite') - - with open(file_name, 'r', encoding="utf8") as db: - database = Database(file_name).makeConnection() - cursor = database.cursor() - + cursor = create_cursor() if country_name == "": raise ValueError("country_name must be set") - query = "SELECT " + str(filter) + " FROM countries" + query = "SELECT " + str(filters) + " FROM countries" if country_name != "": diff --git a/hawqal/filters/city_filter.py b/hawqal/filters/city_filter.py index 2128c2d..614b636 100644 --- a/hawqal/filters/city_filter.py +++ b/hawqal/filters/city_filter.py @@ -1,5 +1,6 @@ class CityFilter: - def __init__(self, city_id=True, city_name=True, state_name=True, state_id=True, country_name=True, latitude=True, longitude=True): + def __init__(self, city_id=True, city_name=True, state_name=True, state_id=True, country_name=True, latitude=True, + longitude=True): self.country_name = country_name self.longitude = longitude self.latitude = latitude @@ -10,19 +11,7 @@ def __init__(self, city_id=True, city_name=True, state_name=True, state_id=True, def __str__(self): fields = "" - if self.country_name: - fields = fields+'country_name,' - if self.longitude: - fields = fields+' longitude,' - if self.latitude: - fields = fields + 'latitude,' - if self.city_id: - fields = fields+'city_id,' - if self.city_name: - fields = fields+' city_name,' - if self.state_name: - fields = fields + 'state_name,' - if self.state_id: - fields = fields + 'state_id,' - - return fields[:-1]+' ' + for field in ['country_name', 'city_id', 'city_name', 'state_id', 'state_name', 'latitude', 'longitude']: + if getattr(self, field): + fields = fields + field + ', ' + return fields[:-2] diff --git a/hawqal/filters/country_filter.py b/hawqal/filters/country_filter.py index ac90f22..06e3ef2 100644 --- a/hawqal/filters/country_filter.py +++ b/hawqal/filters/country_filter.py @@ -16,37 +16,11 @@ def __init__(self, country_name=True, iso_code=True, phone_code=True, capital=Tr self.zone_city = zone_city self.UTC = UTC + + def __str__(self): fields = "" - if self.country_name: - fields = fields+'country_name,' - if self.longitude: - fields = fields+' longitude,' - if self.latitude: - fields = fields + 'latitude,' - if self.iso_code: - fields = fields + 'iso_code,' - if self.phone_code: - fields = fields + 'phone_code,' - if self.capital: - fields = fields+'capital,' - if self.currency: - fields = fields+' currency,' - if self.currency_name: - fields = fields + 'currency_name,' - if self.currency_symbol: - fields = fields + 'currency_symbol,' - if self.country_domain: - fields = fields + 'country_domain,' - if self.region: - fields = fields + 'region,' - if self.subregion: - fields = fields + 'subregion,' - if self.timezone: - fields = fields + 'timezone,' - if self.zone_city: - fields = fields + 'zone_city,' - if self.UTC: - fields = fields + 'UTC,' - - return fields[:-1]+' ' + for field in ['country_name', 'iso_code', 'phone_code', 'capital', 'currency', 'currency_name', 'currency_symbol', 'country_domain', 'region', 'subregion', 'timezone', 'zone_city', 'UTC', 'longitude', 'latitude']: + if getattr(self, field): + fields = fields + field + ', ' + return fields[:-2] \ No newline at end of file diff --git a/hawqal/filters/state_filter.py b/hawqal/filters/state_filter.py index dd1beb0..1296431 100644 --- a/hawqal/filters/state_filter.py +++ b/hawqal/filters/state_filter.py @@ -8,15 +8,7 @@ def __init__(self, state_id=True, state_name=True, country_name=True, longitude= def __str__(self): fields = "" - if self.state_id: - fields = fields+'state_id,' - if self.state_name: - fields = fields+' state_name,' - if self.country_name: - fields = fields + 'country_name,' - if self.longitude: - fields = fields + 'longitude,' - if self.latitude: - fields = fields + 'latitude,' - - return fields[:-1]+' ' + for field in ['state_id', 'state_name', 'country_name', 'longitude', 'latitude']: + if getattr(self, field): + fields = fields + field + ', ' + return fields[:-2] diff --git a/hawqal/states.py b/hawqal/states.py index 7ac1036..e92e316 100644 --- a/hawqal/states.py +++ b/hawqal/states.py @@ -1,4 +1,4 @@ -from dal.dao import Database +from dal.dao import create_cursor from .filters.state_filter import StateFilter import string from Json.Query import convertJson @@ -8,11 +8,11 @@ class States: @staticmethod - def getStates(country_name="", filter=StateFilter()): + def getStates(country_name="", filters=StateFilter()): """ 1. States function takes two parameters as input state name and filters.\n 2. By default, function will return states name.\n - 3. Additional fields are included in filter.\n + 3. Additional fields are included in filters.\n 4. From filter of boolean TRUE fields will be included in output e.g { @@ -20,15 +20,9 @@ def getStates(country_name="", filter=StateFilter()): } """ + cursor = create_cursor() - file_name = os.path.join( - os.path.dirname(__file__), '..', 'database', 'hawqalDB.sqlite') - - with open(file_name, 'r', encoding="utf8") as db: - database = Database(file_name).makeConnection() - cursor = database.cursor() - - query = "SELECT " + str(filter) + " FROM states" + query = "SELECT " + str(filters) + " FROM states" if len(country_name) > 0: query = query + \ @@ -39,12 +33,12 @@ def getStates(country_name="", filter=StateFilter()): return convertJson(cursor) @staticmethod - def getState(country_name="", state_name="", filter=StateFilter()): + def getState(country_name="", state_name="", filters=StateFilter()): if state_name == "": raise ValueError("state_name must be set") - query = "SELECT " + filter + " FROM states" - cursor = Database.database_connection() + query = "SELECT " + filters + " FROM states" + cursor=create_cursor() if len(country_name) > 0 and len(state_name) > 0: query = f'where country_name= "{string.capwords(country_name)}" and state_name= "{string.capwords(state_name)}"' elif len(country_name) > 0: