Skip to content

Instantly share code, notes, and snippets.

@neoecos
Created September 12, 2013 01:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neoecos/6531991 to your computer and use it in GitHub Desktop.
Save neoecos/6531991 to your computer and use it in GitHub Desktop.
Power of python dynamic classes
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