Open
Conversation
|
|
||
| #Instantiate with id, allowing custom or using UUID Int auto generate | ||
| def __init__(self, id=None, condition=None): | ||
| self.id = id if id is not None else uuid.uuid1().int |
| self.condition = condition if condition is not None else 0 | ||
| #get the class name | ||
| def get_category(self): | ||
| return str(type(self).__name__) |
There was a problem hiding this comment.
nice use of python introspection!
| def __str__(self): | ||
| return (f"An object of type {self.get_category()} with id {self.id}") | ||
|
|
||
| #adding a condition description class to describe condition based on value of condition |
| return (f"An object of type {self.get_category()} with id {self.id}") | ||
|
|
||
| #adding a condition description class to describe condition based on value of condition | ||
| def condition_description(self): |
There was a problem hiding this comment.
clever, but unnecessarily so here. A dict would have sufficed, and been O(1) lookup, vs O(n) to fall through n conditions in the worse case :)
| #adding a condition description class to describe condition based on value of condition | ||
| def condition_description(self): | ||
| case = lambda x: self.condition < x | ||
| if case(1): item_condition_desc = "Very Poor Indeed" |
There was a problem hiding this comment.
love the descriptions, though! :D
| def remove(self, item_to_remove): | ||
| if item_to_remove not in self.inventory: | ||
| return None | ||
| self.inventory.remove(item_to_remove) |
There was a problem hiding this comment.
great use of inventory.remove instead of re-implementing that code.
| if item_to_give not in self.inventory or item_to_get not in vendor_friend.inventory: | ||
| return False | ||
|
|
||
| vendor_friend.add(self.remove(item_to_give)) |
There was a problem hiding this comment.
concise! for debugging and maintenance, this could be on different lines. That said, you did do error checking above, so that ameliorates some of the chances that you'll hit an error here.
| #get items from inventory by catagory | ||
| def get_by_category(self, category_string): | ||
|
|
||
| return [item for item in self.inventory if item.get_category() == category_string] |
There was a problem hiding this comment.
excellent use of list comprehension
|
|
||
| def get_best_by_category(self, category_string): | ||
| if self.get_by_category(category_string): | ||
| return max(self.get_by_category(category_string), key=lambda item: item.condition) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.