changeset 22:d8c17766895b

Added functions for container components
author KarstenBock@gmx.net
date Thu, 01 Sep 2011 23:26:50 +0200
parents def430d59281
children 979fffc5af49
files components/container.py
diffstat 1 files changed, 79 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/components/container.py	Thu Sep 01 23:26:02 2011 +0200
+++ b/components/container.py	Thu Sep 01 23:26:50 2011 +0200
@@ -20,3 +20,82 @@
     
     def __init__(self):
         Component.__init__(self, children=list, max_bulk=int)
+
+
+class BulkLimitError(Exception):
+    """Error that gets raised when the item would exceed the 
+    bulk limit of the container."""
+    
+    def __init__(self, bulk, max_bulk):
+        self.bulk = bulk
+        self.max_bulk = max_bulk
+    
+    def __str__(self):
+        return "Item would exceed the bulk limit of the container."
+        
+def get_free_slot(container):
+    """Returns the first slot of the container that is not occupied."""
+    index = 0
+    for child in container.children:
+        if not child:
+            return index
+        index += 1
+    container.children.append(None)
+    return index
+
+def get_total_bulk(container):
+    """Returns the bulk of all items in the container."""
+    total_bulk = 0
+    for child in container.children:
+        if child:
+            total_bulk += child.bulk
+    return total_bulk
+
+def get_total_weight(container):
+    """Returns the weight of all items in the container."""
+    total_weight = 0
+    for child in container.children:
+        if child:
+            total_weight += child.weight
+    return total_weight
+
+def get_item(container, slot):
+    """Returns the item that is in the slot."""
+    if len(container.children) >= (slot + 1):
+        return container.children[slot]
+    return None
+
+def remove_item(container, slot):
+    """Removes the item at the given slot."""
+    item = get_item(container, slot)
+    if item:
+        container.children[slot] = None
+        item.container = None
+        item.slot = -1
+
+def take_item(container, slot):
+    """Moves the item at the given slot out of the container and returns it."""
+    item = get_item(container, slot)
+    if item:
+        remove_item(container, slot)
+    return item
+
+def put_item(container, item, slot=-1):
+    """Puts the item at the given slot in the container.
+    Returns the item previously at the slot."""
+    if slot == -1:
+        slot = get_free_slot(container)
+    total_bulk = get_total_bulk(container)
+    total_bulk += item.bulk
+    old_item = get_item(container, slot)
+    if old_item:
+        total_bulk -= old_item.bulk
+    if total_bulk > container.max_bulk:
+        raise BulkLimitError(total_bulk, container.max_bulk)
+    remove_item(container, slot)
+    container.children[slot] = item
+    if item.container:
+        remove_item(item.container, item.slot)
+    item.container = container
+    item.slot = slot
+    return old_item
\ No newline at end of file