Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 8th, 2012  |  syntax: None  |  size: 1.03 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. class EntityFactory:
  2.     persistenceEngine = None
  3.     def __new__(cls, type_, bases, dict_):
  4.         ''' cls is the metaclass instance (EntityFactory)?
  5.            type_ is the Entity drived class
  6.            bases should be 'Entity'
  7.            dict_ should be empty'''
  8.         instance = object.__new__(cls)
  9.         instance.persistenceEngine = persistenceEngine
  10.         instance.isDirty = False
  11.         instance.isPersisted = False
  12.         return instance
  13.  
  14.     def __call__(self, *args, **kwds):
  15.         print("Not callable")
  16.         return
  17.  
  18.     def newA( id_ ):
  19.         a = EntityFactory.__new__(A.__name__, (Entity, ), {})
  20.         if id_ == None:
  21.             return a
  22.            
  23.         modelObject = persistenceEngine.getRecordForID(id_)
  24.         if modelObject == None:
  25.             return a
  26.  
  27.         a.modelObject = modelObject
  28.  
  29.         a.isPersisted = True
  30.         return a
  31.  
  32.  
  33. class Entity(object, metaclass=EntityFactory):
  34.         id_ = None
  35.  
  36.         #save and load to legacy file through model here
  37.  
  38. class A(Entity):
  39.     pass