-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved performance and added logic to query by Country
- Loading branch information
1 parent
888fd2c
commit e14ebbb
Showing
8 changed files
with
242 additions
and
140 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 |
---|---|---|
@@ -1,19 +1,38 @@ | ||
""" | ||
This is the main file to run the WorldBankDataDownloader. | ||
""" | ||
from src.world_bank_data_downloader import WorldBankDataDownloader | ||
from src.utilts.singleton_logger import SingletonLogger | ||
from src.utilts.world_bank_data_downloader import WorldBankDataDownloader | ||
|
||
|
||
def main(): | ||
""" | ||
Main function to download all data from the World Bank API. | ||
Main function to download data from the World Bank API. | ||
:return: None | ||
:rtype: None | ||
""" | ||
# Set up logging | ||
logger = SingletonLogger().get_logger() | ||
|
||
# Initialize the downloader | ||
downloader = WorldBankDataDownloader() | ||
all_data = downloader.download_all_data() | ||
|
||
# Get all country codes and indicator codes | ||
country_codes = downloader.country_codes | ||
indicator_codes = downloader.indicator_codes | ||
|
||
# Download data for all countries and indicators | ||
all_data = {} | ||
for country_code in country_codes: | ||
logger.info(f"Downloading data for country: {country_code}") | ||
country_data = downloader.fetch_data_concurrently(country_code, indicator_codes) | ||
all_data[country_code] = country_data | ||
|
||
# Save the data to a file | ||
downloader.save_data_to_file(all_data) | ||
|
||
logger.info("Data download and save completed.") | ||
|
||
|
||
if __name__ == "__main__": | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ pytest==8.2.2 | |
coverage==7.6.0 | ||
pytest-cov==5.0.0 | ||
tenacity==8.5.0 | ||
aiohttp |
Empty file.
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,42 @@ | ||
""" | ||
This script downloads data from the World Bank API for a specific country (Australia in this case) and saves it to a file. | ||
""" | ||
from src.utilts.singleton_logger import SingletonLogger | ||
from src.utilts.world_bank_data_downloader import WorldBankDataDownloader | ||
|
||
|
||
def main(): | ||
""" | ||
Main function to download data from the World Bank API. | ||
:return: None | ||
:rtype: None | ||
""" | ||
logger = SingletonLogger().get_logger() | ||
country_code = 'AUS' | ||
|
||
# Create an instance of the WorldBankDataDownloader | ||
downloader = WorldBankDataDownloader() | ||
|
||
# Get the list of all indicators | ||
indicators = downloader.get_indicators() | ||
|
||
# Fetch data for Australia (AUS) for all indicators concurrently | ||
australia_data = downloader.fetch_data_concurrently(country_code, indicators) | ||
|
||
# Save the data to a file | ||
filename = f'../data/raw/{country_code}_world_bank_data.json' | ||
downloader.save_data_to_file(australia_data, filename=filename) | ||
|
||
# Load the data from the file (for verification) | ||
loaded_data = downloader.load_data_from_file(filename=filename) | ||
|
||
# Print the loaded data (or a subset of it) | ||
for indicator, data in loaded_data.items(): | ||
logger.info(f"Indicator: {indicator}") | ||
for entry in data: | ||
logger.info(entry) | ||
logger.info("\n") | ||
|
||
|
||
if __name__ == '__main__': | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
""" | ||
SingletonLogger class is a singleton class that provides a single instance of logger object. | ||
""" | ||
|
||
import logging | ||
import threading | ||
|
||
|
||
class SingletonLogger: | ||
_instance = None | ||
_lock = threading.RLock() | ||
|
||
def __new__(cls, logger_name=None, log_level=logging.DEBUG, log_format=None): | ||
if not cls._instance: | ||
with cls._lock: | ||
if not cls._instance: | ||
cls._instance = super().__new__(cls) | ||
cls._instance._initialize_logger(logger_name, log_level, log_format) | ||
return cls._instance | ||
|
||
def _initialize_logger(self, logger_name, log_level, log_format): | ||
self._logger = logging.getLogger(logger_name or __name__) | ||
self._logger.setLevel(log_level) | ||
|
||
console_handler = logging.StreamHandler() | ||
console_handler.setLevel(log_level) | ||
|
||
if log_format: | ||
formatter = logging.Formatter(log_format) | ||
else: | ||
formatter = logging.Formatter("%(asctime)s - %(threadName)s - %(levelname)s - %(message)s") | ||
console_handler.setFormatter(formatter) | ||
|
||
self._logger.addHandler(console_handler) | ||
|
||
def get_logger(self): | ||
return self._logger | ||
|
||
|
||
# logger = SingletonLogger().get_logger() |
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
Oops, something went wrong.