Changeset 435 for elixir

Show
Ignore:
Timestamp:
12/18/08 14:06:55 (3 years ago)
Author:
ged
Message:

simplified EntityCollection code

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • elixir/trunk/elixir/collection.py

    r399 r435  
    88# default entity collection 
    99class EntityCollection(list): 
    10     def __init__(self): 
     10    def __init__(self, *args): 
    1111        # _entities is a dict of entities keyed on their name. 
    1212        self._entities = {} 
    13         list.__init__(self) 
     13        list.__init__(self, *args) 
    1414 
    1515    def append(self, entity): 
     
    1919        super(EntityCollection, self).append(entity) 
    2020 
    21         key = entity.__name__ 
    22         mapped_entity = self._entities.get(key) 
    23         if mapped_entity: 
    24             if isinstance(mapped_entity, list): 
    25                 mapped_entity.append(entity) 
    26             else: 
    27                 self._entities[key] = [mapped_entity, entity] 
    28         else: 
    29             self._entities[key] = entity 
     21        existing_entities = self._entities.setdefault(entity.__name__, []) 
     22        existing_entities.append(entity) 
    3023 
    3124    def resolve(self, key, entity=None): 
     
    5144                                    "entity corresponding to the key '%s'!" 
    5245                                    % key) 
    53             elif isinstance(res, list): 
    54                 raise Exception("'%s' resolves to several entities, you should " 
    55                                 "use the full path (including the full module " 
    56                                 "name) to that entity." % key) 
     46            elif len(res) > 1: 
     47                raise Exception("'%s' resolves to several entities, you should" 
     48                                " use the full path (including the full module" 
     49                                " name) to that entity." % key) 
    5750            else: 
    58                 return res 
     51                return res[0] 
    5952 
    6053    def clear(self):