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
65 changes: 33 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
17 changes: 17 additions & 0 deletions api.py
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions cleanup.py
Original file line number Diff line number Diff line change
@@ -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
129 changes: 0 additions & 129 deletions in-class-project/.gitignore

This file was deleted.

1 change: 0 additions & 1 deletion in-class-project/README.md

This file was deleted.

23 changes: 0 additions & 23 deletions in-class-project/api.py

This file was deleted.

17 changes: 0 additions & 17 deletions in-class-project/cleanup.py

This file was deleted.

This file was deleted.

12 changes: 0 additions & 12 deletions in-class-project/get_currency.py

This file was deleted.

12 changes: 0 additions & 12 deletions in-class-project/main.py

This file was deleted.

5 changes: 0 additions & 5 deletions in-class-project/requirements.txt

This file was deleted.

7 changes: 0 additions & 7 deletions in-class-project/save_to_file.py

This file was deleted.

Empty file removed in-class-project/utils.py
Empty file.
7 changes: 7 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -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')
62 changes: 62 additions & 0 deletions myfirstresult.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
Binary file added requirements.txt
Binary file not shown.
Loading