-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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] | ||
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.""" | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
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")] | ||
|
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.""" | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
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"), | ||
|
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
headers = { | ||
"User-Agent": "linux:0x16.api.advanced:v1.0.0 (by /u/bdov_)" | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
headers = { | ||
"User-Agent": "linux:0x16.api.advanced:v1.0.0 (by /u/bdov_)" | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
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) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
headers = { | ||
"User-Agent": "linux:0x16.api.advanced:v1.0.0 (by /u/bdov_)" | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
There was a problem hiding this comment.
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:use-fstring-for-concatenation
)use-fstring-for-formatting
)