# Buggy code
def load(self, name, id):
    """Loads and sets this items's attributes from the item.db database. The item is looked up
    in the database by name or id.

    :param name: The name of the item to look up in the monster.item database.
    :param id: The id of the item to look up in the item.db database.

    :type name: String
    :type id: Integer

    :rtype: None
    :returns: None

    **Examples:**

    >>> potion_item = Item()
    >>> potion_item.load("Potion", None)    # Load an item by name.
    >>> potion_item.load(None, 1)           # Load an item by id.
    >>> pprint.pprint(potion_item.__dict__)
    {'description': u'Heals a monster by 50 HP.',
     'effect': [u'heal'],
     'id': 1,
     'name': u'Potion',
     'power': 50,
     'type': u'Consumable'}

    """

    if name:
        results = items.lookup(name, table="item")
    elif id:
        results = items.lookup_by_id(id, table="item")

    self.name = results["name"]
    self.name_trans = trans(results["name_trans"])
    self.description = results["description"]
    self.description_trans = trans(results["description_trans"])

    self.id = results["id"]
    self.type = results["type"]
    self.power = results["power"]
    self.sprite = results["sprite"]
    self.usable_in = results["usable_in"]
    self.surface = tools.load_and_scale(self.sprite)
    self.surface_size_original = self.surface.get_size()

    # TODO: maybe break out into own function
    from operator import itemgetter

    self.target = map(
        itemgetter(0),
        filter(
            itemgetter(1),
            sorted(results["target"].items(), key=itemgetter(1), reverse=True),
        ),
    )

    self.effect = results["effects"]


# Traceback (KeyError)
Traceback (most recent call last):
File "./tuxemon.py", line 60, in <module>
main()
File "/tmp/Tuxemon/tuxemon/core/main.py", line 109, in main
control.main()
File "/tmp/Tuxemon/tuxemon/core/control.py", line 512, in main
self.main_loop()
File "/tmp/Tuxemon/tuxemon/core/control.py", line 573, in main_loop
self.update(time_delta)
File "/tmp/Tuxemon/tuxemon/core/control.py", line 203, in update
state.update(dt)
File "/tmp/Tuxemon/tuxemon/core/states/combat/combat.py", line 156, in update
self.update_phase()
File "/tmp/Tuxemon/tuxemon/core/states/combat/combat.py", line 328, in update_phase
self.handle_action_queue()
File "/tmp/Tuxemon/tuxemon/core/states/combat/combat.py", line 338, in handle_action_queue
self.check_party_hp()
File "/tmp/Tuxemon/tuxemon/core/states/combat/combat.py", line 614, in check_party_hp
self.faint_monster(monster)
File "/tmp/Tuxemon/tuxemon/core/states/combat/combat.py", line 581, in faint_monster
awarded_exp = monster.total_experience/monster.level/len(self._damage_map[monster])
KeyError: <core.components.monster.Monster object at 0x7fb24236ddd0>

# Fixed code
def load(self, slug, id):
    """Loads and sets this items's attributes from the item.db database. The item is looked up
    in the database by name or id.

    :param slug: The item slug to look up in the monster.item database.
    :param id: The id of the item to look up in the item.db database.

    :type slug: String
    :type id: Integer

    :rtype: None
    :returns: None

    **Examples:**

    >>> potion_item = Item()
    >>> potion_item.load("item_potion", None)    # Load an item by name.
    >>> potion_item.load(None, 1)           # Load an item by id.
    >>> pprint.pprint(potion_item.__dict__)
    {'description': u'Heals a monster by 50 HP.',
     'effect': [u'heal'],
     'id': 1,
     'name': u'Potion',
     'power': 50,
     'type': u'Consumable'}

    """

    if slug:
        results = items.lookup(slug, table="item")
    elif id:
        results = items.lookup_by_id(id, table="item")
    else:
        # TODO: some kind of useful message here
        raise RuntimeError

    self.slug = results["slug"]  # short English identifier
    self.name = trans(results["name_trans"])  # will be locale string
    self.description = trans(results["description_trans"])  # will be locale string

    self.id = results["id"]
    self.type = results["type"]
    self.power = results["power"]
    self.sprite = results["sprite"]
    self.usable_in = results["usable_in"]
    self.surface = tools.load_and_scale(self.sprite)
    self.surface_size_original = self.surface.get_size()

    # TODO: maybe break out into own function
    from operator import itemgetter

    self.target = map(
        itemgetter(0),
        filter(
            itemgetter(1),
            sorted(results["target"].items(), key=itemgetter(1), reverse=True),
        ),
    )

    self.effect = results["effects"]