File: factory.rst

package info (click to toggle)
zope.component 4.3.0-3
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 780 kB
  • sloc: python: 5,600; makefile: 130
file content (153 lines) | stat: -rw-r--r-- 3,104 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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)

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 a keyword argument
  
.. doctest::

   >>> kl.args
   (1, 2)
   >>> kl.kw
   {'foo': 3}
   
   >>> 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)
   >>> isinstance(kl, Klass)
   True
   >>> kl.args
   (1, 2)
   >>> kl.kw
   {'foo': 3}

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'>)]