diff --git a/README.md b/README.md index 2129f26..e1753b2 100644 --- a/README.md +++ b/README.md @@ -1,32 +1,33 @@ -# Class7-Python-Module-Week3 - -## Random User Creator App -#### We are going to create an app which will make an API call to `http://jsonplaceholder.typicode.com/users` and get a list of random users. Then our app will clean up the returned list of users and store them in a `.json` file. - -## Steps to follow: -#### Create an API function: -- Import necessary library for making an API call. -- Make the API call to this URL -> `http://jsonplaceholder.typicode.com/users` -- Check the status code, return object (res.json()) -- Wrap the API function in try/except block. -- Return the return object. - -#### Create a cleanup function: -- Take the return object as parameter and transform the return object. (Before starting with transforming this return object, check its data type as it may not be a dictionary. Plan your code according to its data type) -- Create a new Python dictionary containing all 10 users and their details from the return object. (Items to be included: id, name, username, email) -- Return the Python dictionary -#### Creata a function to write the users dictionary to a .json file: -- Take a dictionary as the parameter -- Open up a new file (or create if it isn't existing) -- Dump the Python dictionary into this file -#### Create a `main.py` file: -- Import all your modules -- Create the main app logic here - - -## Things to remember: -- Use a virtual environment. -- Create separate functions for all the steps. -- Modularize your code (either create a separate file for each function or create a file called `utils.py` and store all the functions in this file) -- Wrap the necessary parts of your functions in try/except blocks. -- Create a `requirements.txt` file at the end of your project to store all the dependancy information. (`pip freeze > requirements.txt`) +# Week3-homework +Create an API function: + + Import necessary library for making an API call. + Make the API call to this URL -> http://jsonplaceholder.typicode.com/users + Check the status code, return object (res.json()) + Wrap the API function in try/except block. + Return the return object. + +Create a cleanup function: + + Take the return object as parameter and transform the return object. (Before starting with transforming this return object, check its data type as it may not be a dictionary. Plan your code according to its data type) + Create a new Python dictionary containing all 10 users and their details from the return object. (Items to be included: id, name, username, email) + Return the Python dictionary + +Creata a function to write the users dictionary to a .json file: + + Take a dictionary as the parameter + Open up a new file (or create if it isn't existing) + Dump the Python dictionary into this file + +Create a main.py file: + + Import all your modules + Create the main app logic here + +Things to remember: + + Use a virtual environment. + Create separate functions for all the steps. + Modularize your code (either create a separate file for each function or create a file called utils.py and store all the functions in this file) + Wrap the necessary parts of your functions in try/except blocks. + Create a requirements.txt file at the end of your project to store all the dependancy information. (pip freeze > requirements.txt) diff --git a/api.py b/api.py new file mode 100644 index 0000000..4a0d1f1 --- /dev/null +++ b/api.py @@ -0,0 +1,17 @@ +import sys +import requests + +def make_api_call(): + """make api call and get 10 user`s information + return dictionary of list contain user`s information + """ + try: + r = requests.get('http://jsonplaceholder.typicode.com/users') + + except Exception as e: + print('Opps!', e, 'occured') + sys.exit() + + else: + result_obj = r.json() + return result_obj \ No newline at end of file diff --git a/cleanup.py b/cleanup.py new file mode 100644 index 0000000..4e6c6e4 --- /dev/null +++ b/cleanup.py @@ -0,0 +1,11 @@ +def clean_result(list_of_dict): + """clean up result obj + return dict of celan up version of result obj + """ + new_list = [] + for i in list_of_dict: + l = {'id':i['id'], 'name': i['name'], 'username': i['username'], 'email': i['email']} + new_list.append(l) + + new_dict = {item['id']:item for item in new_list} + return new_dict \ No newline at end of file diff --git a/in-class-project/.gitignore b/in-class-project/.gitignore deleted file mode 100644 index b6e4761..0000000 --- a/in-class-project/.gitignore +++ /dev/null @@ -1,129 +0,0 @@ -# Byte-compiled / optimized / DLL files -__pycache__/ -*.py[cod] -*$py.class - -# C extensions -*.so - -# Distribution / packaging -.Python -build/ -develop-eggs/ -dist/ -downloads/ -eggs/ -.eggs/ -lib/ -lib64/ -parts/ -sdist/ -var/ -wheels/ -pip-wheel-metadata/ -share/python-wheels/ -*.egg-info/ -.installed.cfg -*.egg -MANIFEST - -# PyInstaller -# Usually these files are written by a python script from a template -# before PyInstaller builds the exe, so as to inject date/other infos into it. -*.manifest -*.spec - -# Installer logs -pip-log.txt -pip-delete-this-directory.txt - -# Unit test / coverage reports -htmlcov/ -.tox/ -.nox/ -.coverage -.coverage.* -.cache -nosetests.xml -coverage.xml -*.cover -*.py,cover -.hypothesis/ -.pytest_cache/ - -# Translations -*.mo -*.pot - -# Django stuff: -*.log -local_settings.py -db.sqlite3 -db.sqlite3-journal - -# Flask stuff: -instance/ -.webassets-cache - -# Scrapy stuff: -.scrapy - -# Sphinx documentation -docs/_build/ - -# PyBuilder -target/ - -# Jupyter Notebook -.ipynb_checkpoints - -# IPython -profile_default/ -ipython_config.py - -# pyenv -.python-version - -# pipenv -# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. -# However, in case of collaboration, if having platform-specific dependencies or dependencies -# having no cross-platform support, pipenv may install dependencies that don't work, or not -# install all needed dependencies. -#Pipfile.lock - -# PEP 582; used by e.g. github.com/David-OConnor/pyflow -__pypackages__/ - -# Celery stuff -celerybeat-schedule -celerybeat.pid - -# SageMath parsed files -*.sage.py - -# Environments -.env -.venv -env/ -venv/ -ENV/ -env.bak/ -venv.bak/ - -# Spyder project settings -.spyderproject -.spyproject - -# Rope project settings -.ropeproject - -# mkdocs documentation -/site - -# mypy -.mypy_cache/ -.dmypy.json -dmypy.json - -# Pyre type checker -.pyre/ diff --git a/in-class-project/README.md b/in-class-project/README.md deleted file mode 100644 index b656cc2..0000000 --- a/in-class-project/README.md +++ /dev/null @@ -1 +0,0 @@ -# currencyexchangerapp \ No newline at end of file diff --git a/in-class-project/api.py b/in-class-project/api.py deleted file mode 100644 index ae668f8..0000000 --- a/in-class-project/api.py +++ /dev/null @@ -1,23 +0,0 @@ -import sys -import requests - - -def make_api_call(currency): - - """make api call and get the latest conversion rate - return: dict - currency info - """ - - try: - res = requests.get(f'https://v6.exchangerate-api.com/v6/daa01c089871655d0660d4c8/latest/{currency}') - if res.status_code >= 400: - raise requests.exceptions.HTTPError("Unsupported currency, please make sure to type your currency correctly.") - - except requests.exceptions.HTTPError as exc: - print(exc) - sys.exit() - - else: - result_obj = res.json() - return result_obj - diff --git a/in-class-project/cleanup.py b/in-class-project/cleanup.py deleted file mode 100644 index cf7a1ac..0000000 --- a/in-class-project/cleanup.py +++ /dev/null @@ -1,17 +0,0 @@ - -def clean_result(dct): - """clean up a result obj - return: dict - cleaned up version of the res obj. - """ - - new_dct = { - "currency_of_choice": dct['base_code'], - "date": dct['time_last_update_utc'], - "conversion_rates": { - "USD" : dct['conversion_rates']['USD'], - "EUR" : dct['conversion_rates']['EUR'], - "GBP" : dct['conversion_rates']['GBP'], - } - } - - return new_dct diff --git a/in-class-project/exchange_rates_2022-10-08 19:54:46.475170.json b/in-class-project/exchange_rates_2022-10-08 19:54:46.475170.json deleted file mode 100644 index 860bca6..0000000 --- a/in-class-project/exchange_rates_2022-10-08 19:54:46.475170.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "currency_of_choice": "GBP", - "date": "Sat, 08 Oct 2022 00:00:01 +0000", - "conversion_rates": { - "USD": 1.1116, - "EUR": 1.1382, - "GBP": 1 - } -} \ No newline at end of file diff --git a/in-class-project/get_currency.py b/in-class-project/get_currency.py deleted file mode 100644 index 59cada3..0000000 --- a/in-class-project/get_currency.py +++ /dev/null @@ -1,12 +0,0 @@ - -def get_currency(): - """a function to greet the user and ask for their currncy of choice - - Returns: - str: user's choice of currency - """ - - print("Welcome to CurrencyExchanger App!") - currency = input("Please provide the currency you want the latest rate of: ") - return currency - diff --git a/in-class-project/main.py b/in-class-project/main.py deleted file mode 100644 index d0c2bae..0000000 --- a/in-class-project/main.py +++ /dev/null @@ -1,12 +0,0 @@ -from get_currency import get_currency -from api import make_api_call -from cleanup import clean_result -from save_to_file import save_file - - - - -curr = get_currency() -result_dct = make_api_call(currency=curr) -clean_dct = clean_result(dct=result_dct) -save_file(obj=clean_dct) diff --git a/in-class-project/requirements.txt b/in-class-project/requirements.txt deleted file mode 100644 index b76ce1e..0000000 --- a/in-class-project/requirements.txt +++ /dev/null @@ -1,5 +0,0 @@ -certifi==2022.9.24 -charset-normalizer==2.1.1 -idna==3.4 -requests==2.28.1 -urllib3==1.26.12 diff --git a/in-class-project/save_to_file.py b/in-class-project/save_to_file.py deleted file mode 100644 index 35d6b67..0000000 --- a/in-class-project/save_to_file.py +++ /dev/null @@ -1,7 +0,0 @@ -import json -from datetime import datetime - -def save_file(obj): - - with open(f"{'exchange_rates_' + str(datetime.now())}.json", "w") as f: - json.dump(obj, f) diff --git a/in-class-project/utils.py b/in-class-project/utils.py deleted file mode 100644 index e69de29..0000000 diff --git a/main.py b/main.py new file mode 100644 index 0000000..6dc771a --- /dev/null +++ b/main.py @@ -0,0 +1,7 @@ +from api import make_api_call +from cleanup import clean_result +from save_to_file import save_file + +list_of_dict = make_api_call() +clean_dict = clean_result(list_of_dict) +save_file (clean_dict, 'myfirstresult') diff --git a/myfirstresult.json b/myfirstresult.json new file mode 100644 index 0000000..d030276 --- /dev/null +++ b/myfirstresult.json @@ -0,0 +1,62 @@ +{ + "1": { + "id": 1, + "name": "Leanne Graham", + "username": "Bret", + "email": "Sincere@april.biz" + }, + "2": { + "id": 2, + "name": "Ervin Howell", + "username": "Antonette", + "email": "Shanna@melissa.tv" + }, + "3": { + "id": 3, + "name": "Clementine Bauch", + "username": "Samantha", + "email": "Nathan@yesenia.net" + }, + "4": { + "id": 4, + "name": "Patricia Lebsack", + "username": "Karianne", + "email": "Julianne.OConner@kory.org" + }, + "5": { + "id": 5, + "name": "Chelsey Dietrich", + "username": "Kamren", + "email": "Lucio_Hettinger@annie.ca" + }, + "6": { + "id": 6, + "name": "Mrs. Dennis Schulist", + "username": "Leopoldo_Corkery", + "email": "Karley_Dach@jasper.info" + }, + "7": { + "id": 7, + "name": "Kurtis Weissnat", + "username": "Elwyn.Skiles", + "email": "Telly.Hoeger@billy.biz" + }, + "8": { + "id": 8, + "name": "Nicholas Runolfsdottir V", + "username": "Maxime_Nienow", + "email": "Sherwood@rosamond.me" + }, + "9": { + "id": 9, + "name": "Glenna Reichert", + "username": "Delphine", + "email": "Chaim_McDermott@dana.io" + }, + "10": { + "id": 10, + "name": "Clementina DuBuque", + "username": "Moriah.Stanton", + "email": "Rey.Padberg@karina.biz" + } +} \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..94b56f0 Binary files /dev/null and b/requirements.txt differ diff --git a/save_to_file.py b/save_to_file.py new file mode 100644 index 0000000..ed7001b --- /dev/null +++ b/save_to_file.py @@ -0,0 +1,6 @@ +import json + +def save_file(obj, filename): + + with open (f"{filename}.json", "w") as f: + json.dump(obj, f) \ No newline at end of file