From b374c2c0cb92b12fabb2252eb6f4f34c53109f39 Mon Sep 17 00:00:00 2001 From: zehraturan <78978788+zehraturan@users.noreply.github.com> Date: Tue, 11 Oct 2022 16:51:39 +0200 Subject: [PATCH] The solution of the week3 assignment was added --- README.md | 22 +++++++++++++------- main.py | 5 +++++ requirements.txt | Bin 0 -> 190 bytes result_20221011-164918.json | 1 + utils.py | 40 ++++++++++++++++++++++++++++++++++++ 5 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 main.py create mode 100644 requirements.txt create mode 100644 result_20221011-164918.json create mode 100644 utils.py diff --git a/README.md b/README.md index 2129f26..a718a13 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,13 @@ # 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()) @@ -12,21 +15,26 @@ - 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 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. + +- 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`) +- Create a `requirements.txt` file at the end of your project to store all the dependency information. (`pip freeze > requirements.txt`) diff --git a/main.py b/main.py new file mode 100644 index 0000000..bd17d4a --- /dev/null +++ b/main.py @@ -0,0 +1,5 @@ +from utils import make_api_call, clean_up, save_file + +result_list = make_api_call() +clean_dict = clean_up(result_list) +save_file(clean_dict) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..94b56f0506a85ea448a60de6e3bc927fa60cbc09 GIT binary patch literal 190 zcmX|)OA3QP5Cv;3c$7$NLof>uqb4DOA9Qq+!^b>Zk>L$=Q?Gjc)x4OPX?QDzo^e%- zgok3>h>58AP>KxVXD;*%nmX-T^}cS|+X<<9ska)QB1`vX+U~AN^vI$=$Q6#O+pYX; SF0!}ouE?C&tj665bNe6q@f@!J literal 0 HcmV?d00001 diff --git a/result_20221011-164918.json b/result_20221011-164918.json new file mode 100644 index 0000000..46d4951 --- /dev/null +++ b/result_20221011-164918.json @@ -0,0 +1 @@ +{"1": {"name": "Leanne Graham", "username": "Bret", "email": "Sincere@april.biz"}, "2": {"name": "Ervin Howell", "username": "Antonette", "email": "Shanna@melissa.tv"}, "3": {"name": "Clementine Bauch", "username": "Samantha", "email": "Nathan@yesenia.net"}, "4": {"name": "Patricia Lebsack", "username": "Karianne", "email": "Julianne.OConner@kory.org"}, "5": {"name": "Chelsey Dietrich", "username": "Kamren", "email": "Lucio_Hettinger@annie.ca"}, "6": {"name": "Mrs. Dennis Schulist", "username": "Leopoldo_Corkery", "email": "Karley_Dach@jasper.info"}, "7": {"name": "Kurtis Weissnat", "username": "Elwyn.Skiles", "email": "Telly.Hoeger@billy.biz"}, "8": {"name": "Nicholas Runolfsdottir V", "username": "Maxime_Nienow", "email": "Sherwood@rosamond.me"}, "9": {"name": "Glenna Reichert", "username": "Delphine", "email": "Chaim_McDermott@dana.io"}, "10": {"name": "Clementina DuBuque", "username": "Moriah.Stanton", "email": "Rey.Padberg@karina.biz"}} \ No newline at end of file diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..2f8d203 --- /dev/null +++ b/utils.py @@ -0,0 +1,40 @@ +import json +import sys +import requests +from datetime import datetime + +def make_api_call(): + """make api call and get the information of the users + return : list - user info + """ + try: + res = requests.get('http://jsonplaceholder.typicode.com/users') + if res.status_code >= 400: + raise requests.exceptions.HTTPError("There is an API error") + + except requests.exceptions.HTTPError as exc: + print(exc) + sys.exit() + else: + result_obj = res.json() + return result_obj + + +def clean_up(lst): + """clean up the result obj + return: list - cleaned up version of the res obj + """ + new_dict = dict() + for item in lst: + new_dict[item["id"]]= {'name': item["name"], #"id" is the key of the new_dict + 'username' : item["username"], + 'email' : item["email"]} + + return new_dict + +def save_file(obj): + """creates a file and saves the users data + """ + filename = "result_" + datetime.now().strftime('%Y%m%d-%H%M%S')+'.json' + with open(filename, "w") as f: + json.dump(obj, f) \ No newline at end of file