Differences between revisions 4 and 5
Revision 4 as of 2008-08-15 17:03:37
Size: 1166
Comment:
Revision 5 as of 2008-08-15 17:07:36
Size: 1310
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
the EMAbstractFactory class in is defined in EMAN2.py The EMAbstractFactory class is defined in EMAN2.py. It can be used for designing factory based python code. It is initially taken from [http://code.activestate.com/recipes/86900/ a factory in python]
Line 4: Line 4:
        >>> from EMAN2 import EMAbstractFactory         >>from EMAN2 import EMAbstractFactory

The EMAbstractFactory class is defined in EMAN2.py. It can be used for designing factory based python code. It is initially taken from [http://code.activestate.com/recipes/86900/ a factory in python]

   1         >>from EMAN2 import EMAbstractFactory
   2         >>f=EMAbstractFactory()
   3         >>class A:pass
   4         >>f.register("createA",A)
   5         >>f.createA()
   6         <__main__.A instance at 01491E7C>
   7         
   8         >>> class B:
   9         ...     def __init__(self, a,b=1):
  10         ...             self.a=a
  11         ...             self.b=b
  12         ...             
  13         >>> f.register("createB",B,1,b=2)
  14         >>> f.createB()
  15         >>> b=f.createB()
  16         >>> 
  17         >>> b.a
  18         1
  19         >>> b.b
  20         2
  21         
  22         >>> class C:
  23         ...     def __init__(self,a,b,c=1,d=2):
  24         ...             self.values = (a,b,c,d)
  25         ... 
  26         >>> f.register("createC",C,1,c=3)
  27         >>> c=f.createC(2,d=4)
  28         >>> c.values
  29         (1, 2, 3, 4)
  30         
  31         >>> f.register("importSerialization",__import__,"cPickle")
  32         >>> pickle=f.importSerialization()
  33         >>> pickle
  34         <module 'cPickle' (built-in)>
  35         >>> f.register("importSerialization",__import__,"marshal")
  36         >>> pickle=f.importSerialization()
  37         >>> pickle
  38         <module 'marshal' (built-in)>
  39         
  40         >>> f.unregister("importSerialization")
  41         >>> f.importSerialization()
  42         Traceback (most recent call last):
  43           File "<interactive input>", line 1, in 
  44         AttributeError: Factory instance has no attribute 'importSerialization'

Eman2FactoriesInPython (last edited 2011-01-15 05:44:20 by SteveLudtke)