Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
# 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 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`)
5 changes: 5 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -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)
Binary file added requirements.txt
Binary file not shown.
1 change: 1 addition & 0 deletions result_20221011-164918.json
Original file line number Diff line number Diff line change
@@ -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"}}
40 changes: 40 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
@@ -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)