From 71592aa262a94ac9dda6b45fe296423e41e277ce Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Fri, 23 Jun 2023 11:23:32 +0000 Subject: [PATCH] 'Refactored by Sourcery' --- 0x15-api/0-gather_data_from_an_API.py | 12 +++++---- 0x15-api/1-export_to_CSV.py | 7 ++--- 0x15-api/2-export_to_JSON.py | 7 ++--- .../3-dictionary_of_list_of_dictionaries.py | 27 ++++++++++++------- 0x16-api_advanced/0-subs.py | 2 +- 0x16-api_advanced/1-top_ten.py | 2 +- 0x16-api_advanced/100-count.py | 4 +-- 0x16-api_advanced/2-recurse.py | 2 +- 0x16-api_advanced/main.py | 8 +++--- 9 files changed, 42 insertions(+), 29 deletions(-) diff --git a/0x15-api/0-gather_data_from_an_API.py b/0x15-api/0-gather_data_from_an_API.py index 47b584f..58b2a02 100644 --- a/0x15-api/0-gather_data_from_an_API.py +++ b/0x15-api/0-gather_data_from_an_API.py @@ -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] diff --git a/0x15-api/1-export_to_CSV.py b/0x15-api/1-export_to_CSV.py index 4be0658..af5a993 100644 --- a/0x15-api/1-export_to_CSV.py +++ b/0x15-api/1-export_to_CSV.py @@ -1,5 +1,6 @@ #!/usr/bin/python3 """Exports to-do list information for a given employee ID to CSV format.""" + import csv import requests import sys @@ -7,11 +8,11 @@ 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")] diff --git a/0x15-api/2-export_to_JSON.py b/0x15-api/2-export_to_JSON.py index c453c86..e15c172 100644 --- a/0x15-api/2-export_to_JSON.py +++ b/0x15-api/2-export_to_JSON.py @@ -1,5 +1,6 @@ #!/usr/bin/python3 """Exports to-do list information for a given employee ID to JSON format.""" + import json import requests import sys @@ -7,11 +8,11 @@ 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"), diff --git a/0x15-api/3-dictionary_of_list_of_dictionaries.py b/0x15-api/3-dictionary_of_list_of_dictionaries.py index 87fe205..6be0608 100644 --- a/0x15-api/3-dictionary_of_list_of_dictionaries.py +++ b/0x15-api/3-dictionary_of_list_of_dictionaries.py @@ -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, + ) diff --git a/0x16-api_advanced/0-subs.py b/0x16-api_advanced/0-subs.py index b26ca19..e1e0536 100755 --- a/0x16-api_advanced/0-subs.py +++ b/0x16-api_advanced/0-subs.py @@ -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" headers = { "User-Agent": "linux:0x16.api.advanced:v1.0.0 (by /u/bdov_)" } diff --git a/0x16-api_advanced/1-top_ten.py b/0x16-api_advanced/1-top_ten.py index 45a68bc..f204d60 100755 --- a/0x16-api_advanced/1-top_ten.py +++ b/0x16-api_advanced/1-top_ten.py @@ -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" headers = { "User-Agent": "linux:0x16.api.advanced:v1.0.0 (by /u/bdov_)" } diff --git a/0x16-api_advanced/100-count.py b/0x16-api_advanced/100-count.py index 5a89e60..18c598b 100755 --- a/0x16-api_advanced/100-count.py +++ b/0x16-api_advanced/100-count.py @@ -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" headers = { "User-Agent": "linux:0x16.api.advanced:v1.0.0 (by /u/bdov_)" } @@ -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) diff --git a/0x16-api_advanced/2-recurse.py b/0x16-api_advanced/2-recurse.py index 7e2f6f0..80b7dd9 100755 --- a/0x16-api_advanced/2-recurse.py +++ b/0x16-api_advanced/2-recurse.py @@ -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" headers = { "User-Agent": "linux:0x16.api.advanced:v1.0.0 (by /u/bdov_)" } diff --git a/0x16-api_advanced/main.py b/0x16-api_advanced/main.py index f9bf1b8..29cec9e 100755 --- a/0x16-api_advanced/main.py +++ b/0x16-api_advanced/main.py @@ -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: {} ".format(sys.argv[0])) - print("Ex: {} programming 'python java javascript'".format( - sys.argv[0])) + print(f"Usage: {sys.argv[0]} ") + 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()))