Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sourcery refactored master branch #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
12 changes: 7 additions & 5 deletions 0x15-api/0-gather_data_from_an_API.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#!/usr/bin/python3
"""Returns to-do list information for a given employee ID."""

import requests
import sys

if __name__ == "__main__":
url = "https://jsonplaceholder.typicode.com/"
user = requests.get(url + "users/{}".format(sys.argv[1])).json()
todos = requests.get(url + "todos", params={"userId": sys.argv[1]}).json()
user = requests.get(f"{url}users/{sys.argv[1]}").json()
todos = requests.get(f"{url}todos", params={"userId": sys.argv[1]}).json()

completed = [t.get("title") for t in todos if t.get("completed") is True]
print("Employee {} is done with tasks({}/{}):".format(
user.get("name"), len(completed), len(todos)))
[print("\t {}".format(c)) for c in completed]
print(
f'Employee {user.get("name")} is done with tasks({len(completed)}/{len(todos)}):'
)
[print(f"\t {c}") for c in completed]
Comment on lines +3 to +16
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 8-14 refactored with the following changes:

7 changes: 4 additions & 3 deletions 0x15-api/1-export_to_CSV.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#!/usr/bin/python3
"""Exports to-do list information for a given employee ID to CSV format."""

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 10-14 refactored with the following changes:

import csv
import requests
import sys

if __name__ == "__main__":
user_id = sys.argv[1]
url = "https://jsonplaceholder.typicode.com/"
user = requests.get(url + "users/{}".format(user_id)).json()
user = requests.get(f"{url}users/{user_id}").json()
username = user.get("username")
todos = requests.get(url + "todos", params={"userId": user_id}).json()
todos = requests.get(f"{url}todos", params={"userId": user_id}).json()

with open("{}.csv".format(user_id), "w", newline="") as csvfile:
with open(f"{user_id}.csv", "w", newline="") as csvfile:
writer = csv.writer(csvfile, quoting=csv.QUOTE_ALL)
[writer.writerow(
[user_id, username, t.get("completed"), t.get("title")]
Expand Down
7 changes: 4 additions & 3 deletions 0x15-api/2-export_to_JSON.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#!/usr/bin/python3
"""Exports to-do list information for a given employee ID to JSON format."""

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 10-14 refactored with the following changes:

import json
import requests
import sys

if __name__ == "__main__":
user_id = sys.argv[1]
url = "https://jsonplaceholder.typicode.com/"
user = requests.get(url + "users/{}".format(user_id)).json()
user = requests.get(f"{url}users/{user_id}").json()
username = user.get("username")
todos = requests.get(url + "todos", params={"userId": user_id}).json()
todos = requests.get(f"{url}todos", params={"userId": user_id}).json()

with open("{}.json".format(user_id), "w") as jsonfile:
with open(f"{user_id}.json", "w") as jsonfile:
json.dump({user_id: [{
"task": t.get("title"),
"completed": t.get("completed"),
Expand Down
27 changes: 18 additions & 9 deletions 0x15-api/3-dictionary_of_list_of_dictionaries.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
#!/usr/bin/python3
"""Exports to-do list information of all employees to JSON format."""

import json
import requests

if __name__ == "__main__":
url = "https://jsonplaceholder.typicode.com/"
users = requests.get(url + "users").json()
users = requests.get(f"{url}users").json()

with open("todo_all_employees.json", "w") as jsonfile:
json.dump({
u.get("id"): [{
"task": t.get("title"),
"completed": t.get("completed"),
"username": u.get("username")
} for t in requests.get(url + "todos",
params={"userId": u.get("id")}).json()]
for u in users}, jsonfile)
json.dump(
{
u.get("id"): [
{
"task": t.get("title"),
"completed": t.get("completed"),
"username": u.get("username"),
}
for t in requests.get(
f"{url}todos", params={"userId": u.get("id")}
).json()
]
for u in users
},
jsonfile,
)
Comment on lines +3 to +27
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 8-18 refactored with the following changes:

2 changes: 1 addition & 1 deletion 0x16-api_advanced/0-subs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

def number_of_subscribers(subreddit):
"""Return the total number of subscribers on a given subreddit."""
url = "https://www.reddit.com/r/{}/about.json".format(subreddit)
url = f"https://www.reddit.com/r/{subreddit}/about.json"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function number_of_subscribers refactored with the following changes:

headers = {
"User-Agent": "linux:0x16.api.advanced:v1.0.0 (by /u/bdov_)"
}
Expand Down
2 changes: 1 addition & 1 deletion 0x16-api_advanced/1-top_ten.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

def top_ten(subreddit):
"""Print the titles of the 10 hottest posts on a given subreddit."""
url = "https://www.reddit.com/r/{}/hot/.json".format(subreddit)
url = f"https://www.reddit.com/r/{subreddit}/hot/.json"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function top_ten refactored with the following changes:

headers = {
"User-Agent": "linux:0x16.api.advanced:v1.0.0 (by /u/bdov_)"
}
Expand Down
4 changes: 2 additions & 2 deletions 0x16-api_advanced/100-count.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def count_words(subreddit, word_list, instances={}, after="", count=0):
after (str): The parameter for the next page of the API results.
count (int): The parameter of results matched thus far.
"""
url = "https://www.reddit.com/r/{}/hot/.json".format(subreddit)
url = f"https://www.reddit.com/r/{subreddit}/hot/.json"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function count_words refactored with the following changes:

headers = {
"User-Agent": "linux:0x16.api.advanced:v1.0.0 (by /u/bdov_)"
}
Expand Down Expand Up @@ -49,6 +49,6 @@ def count_words(subreddit, word_list, instances={}, after="", count=0):
print("")
return
instances = sorted(instances.items(), key=lambda kv: (-kv[1], kv[0]))
[print("{}: {}".format(k, v)) for k, v in instances]
[print(f"{k}: {v}") for k, v in instances]
else:
count_words(subreddit, word_list, instances, after, count)
2 changes: 1 addition & 1 deletion 0x16-api_advanced/2-recurse.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

def recurse(subreddit, hot_list=[], after="", count=0):
"""Returns a list of titles of all hot posts on a given subreddit."""
url = "https://www.reddit.com/r/{}/hot/.json".format(subreddit)
url = f"https://www.reddit.com/r/{subreddit}/hot/.json"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function recurse refactored with the following changes:

headers = {
"User-Agent": "linux:0x16.api.advanced:v1.0.0 (by /u/bdov_)"
}
Expand Down
8 changes: 4 additions & 4 deletions 0x16-api_advanced/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
"""
100-main
"""

import sys

if __name__ == '__main__':
count_words = __import__('100-count').count_words
if len(sys.argv) < 3:
print("Usage: {} <subreddit> <list of keywords>".format(sys.argv[0]))
print("Ex: {} programming 'python java javascript'".format(
sys.argv[0]))
print(f"Usage: {sys.argv[0]} <subreddit> <list of keywords>")
print(f"Ex: {sys.argv[0]} programming 'python java javascript'")
else:
result = count_words(sys.argv[1], [x for x in sys.argv[2].split()])
result = count_words(sys.argv[1], list(sys.argv[2].split()))
Comment on lines +5 to +14
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 10-14 refactored with the following changes: