-
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?
Changes from all commits
79ac055
515aa4f
0b13e0a
15e4864
0f623c7
7293907
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 +1,3 @@ | ||
| creatures.json | ||
| creatures.json | ||
| .idea | ||
| *.pyc |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "fluffy": {"type":"dragon", "color":"red", "temp_of_fire":96}, | ||
| "Giffa": {"type":"giraffe", "height":3.9, "num_of_dots":153}, | ||
| "Chatushi": {"type":"monster", "num_of_eyes":5, "num_of_teeth":33, "num_of_legs":4}, | ||
| "Arie": {"type":"giraffe", "height":4.4, "num_of_dots":134} | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import sys | ||
| import json | ||
|
|
||
| CREATURES_LIST_FILE = "creatures.json" | ||
|
|
||
|
|
||
| class Creature: | ||
|
|
||
| def __init__(self, creature_name): | ||
| self._creature_name = creature_name | ||
|
|
||
| def get_creature_name(self): | ||
| return self._creature_name | ||
|
|
||
|
|
||
| class Dragon(Creature): | ||
|
|
||
| def __init__(self, creature_name, color, temp_of_fire): | ||
| Creature.__init__(self, creature_name) | ||
| self._color = color | ||
| 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."%\ | ||
| (self._color, self._temp_of_fire, self.get_creature_name()) | ||
|
|
||
|
|
||
| class Giraffe(Creature): | ||
|
|
||
| def __init__(self, creature_name, height, num_of_dots): | ||
| Creature.__init__(self, creature_name) | ||
| self._height = height | ||
| self._num_of_dots = num_of_dots | ||
|
|
||
| def print_creature(self): | ||
| print "This is a Giraffe, is it %d meters tall and has %d dots. Its' name is %s."%\ | ||
| (self._height, self._num_of_dots, self.get_creature_name()) | ||
|
|
||
|
|
||
| class Monster(Creature): | ||
|
|
||
| def __init__(self, creature_name, num_of_eyes, num_of_teeth, num_of_legs): | ||
| Creature.__init__(self, creature_name) | ||
| self._num_of_eyes = num_of_eyes | ||
| self._num_of_teeth = num_of_teeth | ||
| self._num_of_legs = num_of_legs | ||
|
|
||
| def print_creature(self): | ||
| print "This is a Monster, is has %d eyes, %d teeth and %d legs. Its' name is %s."%\ | ||
| (self._num_of_eyes, self._num_of_teeth, self._num_of_legs, self.get_creature_name()) | ||
|
|
||
|
|
||
| def creature_factory(creature_name, features): | ||
|
Collaborator
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. Good job! And here's how you can improve it: http://stackoverflow.com/a/60215/1206176 |
||
| if features["type"] == "dragon": | ||
| return Dragon(creature_name, features["color"], features["temp_of_fire"]) | ||
| if features["type"] == "giraffe": | ||
| return Giraffe(creature_name, features["height"], features["num_of_dots"]) | ||
| if features["type"] == "monster": | ||
| return Monster(creature_name, features["num_of_eyes"], features["num_of_teeth"], features["num_of_legs"]) | ||
|
|
||
|
|
||
| def name_exists(name, list): | ||
|
Collaborator
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. Simply say
Collaborator
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. Another thing - don't use the word |
||
| if name in list: | ||
| return True | ||
| else: | ||
| return False | ||
|
|
||
|
|
||
| def parser(file_name): | ||
| json_data = open(file_name).read() | ||
| data = json.loads(json_data) | ||
| return data | ||
|
|
||
|
|
||
| def main(): | ||
| creatures = parser(CREATURES_LIST_FILE) | ||
| if len(sys.argv) == 1: | ||
| 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): | ||
|
Collaborator
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. 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. |
||
| new_creature = creature_factory(sys.argv[1], creatures[sys.argv[1]]) | ||
| new_creature.print_creature() | ||
| else: | ||
|
Collaborator
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. 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. |
||
| print "Creature not found" | ||
| sys.exit(1) | ||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| def squares(x): | ||
| return [x**2 for x in range(1, x+1)] | ||
|
|
||
|
|
||
| def dict_squares(x): | ||
| return {x: x**2 for x in range(1, x+1)} | ||
|
|
||
|
|
||
| def main(): | ||
| print squares(3) | ||
| print dict_squares(3) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() |
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)