Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
creatures.json
creatures.json
.idea
*.pyc
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/small exercises.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

208 changes: 178 additions & 30 deletions .idea/workspace.xml

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions creatures.example.json
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}
}
89 changes: 89 additions & 0 deletions factory.py
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)
Copy link
Collaborator

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 = 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):
Copy link
Collaborator

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

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):
Copy link
Collaborator

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

Copy link
Collaborator

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.

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):
Copy link
Collaborator

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.

new_creature = creature_factory(sys.argv[1], creatures[sys.argv[1]])
new_creature.print_creature()
else:
Copy link
Collaborator

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.

print "Creature not found"
sys.exit(1)

if __name__ == '__main__':
main()
15 changes: 15 additions & 0 deletions squares.py
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()