D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
usr
/
share
/
doc
/
python-zope-component-4.1.0
/
html
/
_sources
/
Filename :
factory.txt
back
Copy
Factories ========= The Factory Class ----------------- .. doctest:: >>> from zope.interface import Interface >>> class IFunction(Interface): ... pass >>> class IKlass(Interface): ... pass >>> from zope.interface import implements >>> class Klass(object): ... implements(IKlass) ... ... def __init__(self, *args, **kw): #* ... self.args = args ... self.kw = kw >>> from zope.component.factory import Factory >>> factory = Factory(Klass, 'Klass', 'Klassier') >>> factory2 = Factory(lambda x: x, 'Func', 'Function') >>> factory3 = Factory(lambda x: x, 'Func', 'Function', (IFunction,)) Calling a Factory ~~~~~~~~~~~~~~~~~ Here we test whether the factory correctly creates the objects and including the correct handling of constructor elements. First we create a factory that creates instanace of the `Klass` class: .. doctest:: >>> factory = Factory(Klass, 'Klass', 'Klassier') Now we use the factory to create the instance .. doctest:: >>> kl = factory(1, 2, foo=3, bar=4) and make sure that the correct class was used to create the object: .. doctest:: >>> kl.__class__ <class 'Klass'> Since we passed in a couple positional and keyword arguments .. doctest:: >>> kl.args (1, 2) >>> kl.kw {'foo': 3, 'bar': 4} >>> factory2(3) 3 >>> factory3(3) 3 Title and Description ~~~~~~~~~~~~~~~~~~~~~ .. doctest:: >>> factory.title 'Klass' >>> factory.description 'Klassier' >>> factory2.title 'Func' >>> factory2.description 'Function' >>> factory3.title 'Func' >>> factory3.description 'Function' Provided Interfaces ~~~~~~~~~~~~~~~~~~~ .. doctest:: >>> implemented = factory.getInterfaces() >>> implemented.isOrExtends(IKlass) True >>> list(implemented) [<InterfaceClass __builtin__.IKlass>] >>> implemented2 = factory2.getInterfaces() >>> list(implemented2) [] >>> implemented3 = factory3.getInterfaces() >>> list(implemented3) [<InterfaceClass __builtin__.IFunction>] The Component Architecture Factory API -------------------------------------- .. doctest:: >>> import zope.component >>> factory = Factory(Klass, 'Klass', 'Klassier') >>> gsm = zope.component.getGlobalSiteManager() >>> from zope.component.interfaces import IFactory >>> gsm.registerUtility(factory, IFactory, 'klass') Creating an Object ~~~~~~~~~~~~~~~~~~ .. doctest:: >>> kl = zope.component.createObject('klass', 1, 2, foo=3, bar=4) >>> isinstance(kl, Klass) True >>> kl.args (1, 2) >>> kl.kw {'foo': 3, 'bar': 4} Accessing Provided Interfaces ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. doctest:: >>> implemented = zope.component.getFactoryInterfaces('klass') >>> implemented.isOrExtends(IKlass) True >>> [iface for iface in implemented] [<InterfaceClass __builtin__.IKlass>] List of All Factories ~~~~~~~~~~~~~~~~~~~~~ .. doctest:: >>> [(name, fac.__class__) for name, fac in ... zope.component.getFactoriesFor(IKlass)] [(u'klass', <class 'zope.component.factory.Factory'>)]
Name
Size
Last Modified
Owner
Permissions
Actions
api
Dir
February 10 2022 8:31:22
root
0755
api.txt
0.231
KB
February 28 2013 2:31:18
root
0644
configure.txt
1.027
KB
February 28 2013 2:31:18
root
0644
event.txt
3.577
KB
February 28 2013 2:31:18
root
0644
factory.txt
3.063
KB
February 28 2013 2:31:18
root
0644
hacking.txt
10.157
KB
February 28 2013 2:31:18
root
0644
hooks.txt
2.726
KB
February 28 2013 2:31:18
root
0644
index.txt
0.299
KB
February 28 2013 2:31:18
root
0644
narr.txt
12.351
KB
February 28 2013 2:31:18
root
0644
persistentregistry.txt
0.202
KB
February 28 2013 2:31:18
root
0644
socketexample.txt
21.257
KB
February 28 2013 2:31:18
root
0644
testlayer.txt
3.351
KB
February 28 2013 2:31:18
root
0644
zcml.txt
31.853
KB
February 28 2013 2:31:18
root
0644
2017 © D7net | D704T team