comparison src/parpg/components/container.py @ 46:3bacef35b252

get_free_slot no raises an exception instead of appending a slot to the container.
author KarstenBock@gmx.net
date Fri, 02 Sep 2011 12:54:43 +0200
parents 9f631144124f
children 96af64cf3b81
comparison
equal deleted inserted replaced
45:0443ab9533ce 46:3bacef35b252
30 self.bulk = bulk 30 self.bulk = bulk
31 self.max_bulk = max_bulk 31 self.max_bulk = max_bulk
32 32
33 def __str__(self): 33 def __str__(self):
34 return "Item would exceed the bulk limit of the container." 34 return "Item would exceed the bulk limit of the container."
35 35
36 class NoFreeSlotError(Exception):
37 """Error that gets raised when the container has no free slots."""
38
39 def __str__(self):
40 return "Container can't hold any more items."
41
36 def get_free_slot(container): 42 def get_free_slot(container):
37 """Returns the first slot of the container that is not occupied.""" 43 """Returns the first slot of the container that is not occupied."""
38 index = 0 44 index = 0
39 for child in container.children: 45 for child in container.children:
40 if not child: 46 if not child:
41 return index 47 return index
42 index += 1 48 index += 1
43 container.children.append(None) 49 raise NoFreeSlotError
44 return index
45 50
46 def get_total_bulk(container): 51 def get_total_bulk(container):
47 """Returns the bulk of all items in the container.""" 52 """Returns the bulk of all items in the container."""
48 total_bulk = 0 53 total_bulk = 0
49 for child in container.children: 54 for child in container.children: