-
Notifications
You must be signed in to change notification settings - Fork 0
Factory exercise #1
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
base: master
Are you sure you want to change the base?
Conversation
factory.py
Outdated
| # self.__creature_name = creature_name | ||
| # self.__creature_type = creature_type | ||
|
|
||
| def print_creature(self): |
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.
raise NotImplementedError
yochaibl
left a comment
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.
As for the idea files, see this:
http://stackoverflow.com/questions/11124053/accidentally-committed-idea-directory-files-into-git
| print "hello" | ||
| # not implemented | ||
|
|
||
| def get_creature_name(self): |
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.
raise NotImplementedError
factory.py
Outdated
| self._temp_of_fire = temp_of_fire | ||
|
|
||
| def print_creature(self): | ||
| print "This is a Dragon, is it %s and its' fire tempature is %d. Its' name is %s"%\ |
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.
is it -> it is
Its' -> its :)
add a space before and after the percent (in the end of the line)
| (self._num_of_eyes, self._num_of_teeth, self._num_of_legs, self.get_creature_name()) | ||
|
|
||
|
|
||
| def creature_factory(creature_name, features): |
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.
Good job!
There's a more pythonic way to do it, I'll show you.
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.
| class Monster(Creature): | ||
|
|
||
| def __init__(self, creature_name, num_of_eyes, num_of_teeth, num_of_legs): | ||
| Creature.__init__(self, creature_name) |
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.
Say super(Monster, self).__init__(self, creature_name)
| (self._num_of_eyes, self._num_of_teeth, self._num_of_legs, self.get_creature_name()) | ||
|
|
||
|
|
||
| def creature_factory(creature_name, features): |
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.
Good job!
And here's how you can improve it: http://stackoverflow.com/a/60215/1206176
| return Monster(creature_name, features["num_of_eyes"], features["num_of_teeth"], features["num_of_legs"]) | ||
|
|
||
|
|
||
| def name_exists(name, list): |
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.
Simply say return name in list
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.
Another thing - don't use the word list as a variable. It's a reserved word.
| elif name_exists(sys.argv[1], creatures): | ||
| new_creature = creature_factory(sys.argv[1], creatures[sys.argv[1]]) | ||
| new_creature.print_creature() | ||
| else: |
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.
Could be nice if you moved this check to the beginning, so you get rid of the problem as soon as you can (and quit with error) and then, if everything is fine, continue as usual.
| for creature_name, creature_data in creatures.iteritems(): | ||
| new_creature = creature_factory(creature_name, creature_data) | ||
| new_creature.print_creature() | ||
| elif name_exists(sys.argv[1], creatures): |
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.
Accessing an element in an array always gives me the feeling that something bad is going to happen.
Once in the beginning, determine how many arguments you got and assign each to a variable with a meaningful name, and don't mess with that array again. The code will become much more readable.
No description provided.