Changeset 332

Show
Ignore:
Timestamp:
05/14/08 18:55:49 (7 years ago)
Author:
cleverdevil
Message:

Made the elixir.entities list into a useful way to lookup entities by name.
You can do "from elixir import entities" and then get any registered entity
through "entities.MyEntity".

Files:
1 modified

Legend:

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

    r323 r332  
    3939__version__ = '0.6.0' 
    4040 
    41 __all__ = ['Entity', 'EntityMeta', 
     41__all__ = ['Entity', 'EntityMeta', 'entities', 
    4242           'Field', 'has_field', 'with_fields',  
    4343           'has_property', 'GenericProperty', 'ColumnProperty', 'Synonym', 
     
    9191 
    9292# default entity collection 
    93 entities = list() 
     93class AttributeEntityList(list): 
     94     
     95    def __init__(self): 
     96        self._entity_map = None 
     97        list.__init__(self) 
     98     
     99    def __getattr__(self, key): 
     100        if self._entity_map is None: 
     101            self._entity_map = {} 
     102            for entity in self: 
     103                self._entity_map[entity.__name__] = entity 
     104         
     105        return self._entity_map.get(key) 
     106             
     107entities = AttributeEntityList() 
    94108 
    95109