Created
September 12, 2013 01:00
-
-
Save neoecos/6531991 to your computer and use it in GitHub Desktop.
Power of python dynamic classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def find_or_create_entity_with_data(*args, **kwargs): | |
cls_entity_data = args[0] if len(args) > 0 else None | |
cls_entity = None | |
if cls_entity_data is not None and issubclass(cls_entity_data, CaliopeEntityData): | |
cls_entity = cls_entity_data.entity_type | |
if cls_entity is not None: | |
code = kwargs['code'] if 'code' in kwargs else None | |
if code is not None: | |
data_entities = cls_entity_data.index.search(code=code) | |
if len(data_entities) == 0: | |
entity = cls_entity() | |
entity.save() | |
entity.init_entity_data(**kwargs) | |
return entity | |
else: | |
return data_entities.current.single() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment