Skip to content

exercise#1

Open
samyou-softwire wants to merge 9 commits into
masterfrom
exercise
Open

exercise#1
samyou-softwire wants to merge 9 commits into
masterfrom
exercise

Conversation

@samyou-softwire
Copy link
Copy Markdown
Owner

No description provided.

@PhilipDW183
Copy link
Copy Markdown
Collaborator

Impressive work and cool functionality! The ability to refresh and select a specific character is pretty cool, along with the functionality of using the API to match by the first few characters!

@PhilipDW183
Copy link
Copy Markdown
Collaborator

Two things before diving into the code:

  1. A description of the merge request would be helpful for a user. Telling them what is the purpose of the merge request and what it does. This does not have to be how it works, but what it does would be helpful and why.

  2. The commit messages are not very clear. This would make it difficult to rollback to a specific commit unless you knew what they were. This source is a good source for covering best practice for commit messages https://cbea.ms/git-commit/. Generally you want an action word to explain what that commit is doing to the code base, to capitalise the start of the commit message and to say whether the change is fixing a bug or something else. In some cases it seems like you are fixing a bug, but I couldn't be sure.

Copy link
Copy Markdown
Collaborator

@PhilipDW183 PhilipDW183 left a comment

Choose a reason for hiding this comment

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

Very clear variable naming throughout! It was pretty easy to read and understand what each variable was doing.

However, a lot of the code could have been put into functions. This would have made it easier to understand what the flow is meant to be and made it easier to read and understand. It would also have ensured that if your code was imported somewhere else, it wouldn't just run.

Comment thread main.py
Comment on lines +11 to +12
PUBLIC_API_KEY = getenv("MARVEL_PUBLIC_API_KEY")
PRIVATE_API_KEY = getenv("MARVEL_PRIVATE_API_KEY")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice use of keeping your environment variables secure! Good DevOps practice!

Comment thread main.py
Comment on lines +14 to +15
SEARCH_URL = "https://gateway.marvel.com:443/v1/public/characters"
DETAILS_URL = "https://gateway.marvel.com:443/v1/public/characters/{id}"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is there a way you could have just added the {id} onto the first url rather than having to create two urls?

Comment thread main.py
Comment on lines +4 to +6
from time import time_ns

from requests import get
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why the gap here? It would be good to keep all imports together unless there is some specific reason not to

Comment thread main.py
Comment on lines +19 to +34
while True:
timestamp = str(time_ns())
key_hash = md5(f"{timestamp}{PRIVATE_API_KEY}{PUBLIC_API_KEY}".encode("utf-8")).hexdigest()
default_params = {
'apikey': PUBLIC_API_KEY,
'ts': timestamp,
'hash': key_hash,
}

name = input("What Marvel character? ")

keep_searching = True
ID = None
offset = 0

while keep_searching:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

A nested while statement could be suggested to not be best practice as it can get messy (see comment below on nested if/else statements)

Comment thread main.py
Comment on lines +49 to +53
data = res['data']
total = data['total']
offset = data['offset']

characters = data['results']
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Have you thought about using .get() instead of []. This would just ensure that an error wouldn't be thrown if that specific section wasn't there.

Comment thread main.py
print(f"- {i}: {character['name']}")
IDs.append(character['id'])

choice = input(f"Which ID? (1-{len(characters)}), or 'n' for next page ")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is pretty cool functionality! Awesome job!

Comment thread main.py
Comment on lines +55 to +82
if total == 0:
print("Couldn't find any characters by this name...")
keep_searching = False
elif total == 1:
ID = characters[0]['id']
keep_searching = False
else:
IDs = []
i = 0
print(f"Please select a character)")
for character in characters:
i += 1
print(f"- {i}: {character['name']}")
IDs.append(character['id'])

choice = input(f"Which ID? (1-{len(characters)}), or 'n' for next page ")

if choice == "n":
if offset + PAGE_SIZE >= total:
print("There are no more pages...")
keep_searching = False
else:
offset += PAGE_SIZE
else:
index = int(choice) - 1
ID = IDs[index]
keep_searching = False
found_a_character = True
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

At this point there is many nested if/else if statements, on top of the nested while statements, this makes following the logic difficult, especially to understand which branch you end up in. If there are nested if/else statements, then sometimes it is best to put everything into a function and use return statements.

Comment thread main.py
Comment on lines +91 to +92
print(f"Name: {character['name']}")
print(f"Bio: {character['description']}")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice functionality to print both the name and the description!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants