Initial import to Tizen
[profile/ivi/python-twisted.git] / doc / core / howto / quotes.html
1 <?xml version="1.0" encoding="utf-8"?><!DOCTYPE html  PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN'  'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html lang="en" xmlns="http://www.w3.org/1999/xhtml">
2   <head>
3 <title>Twisted Documentation: Setting up the TwistedQuotes application</title>
4 <link href="stylesheet.css" rel="stylesheet" type="text/css"/>
5   </head>
6
7   <body bgcolor="white">
8     <h1 class="title">Setting up the TwistedQuotes application</h1>
9     <div class="toc"><ol><li><a href="#auto0">Goal</a></li><li><a href="#auto1">Setting up the TwistedQuotes project directory</a></li></ol></div>
10     <div class="content">
11
12 <span/>
13
14 <h2>Goal<a name="auto0"/></h2>
15
16 <p>This document describes how to set up the TwistedQuotes application used in
17 a number of other documents, such as <a href="design.html" shape="rect">designing Twisted applications</a>.</p>
18
19 <h2>Setting up the TwistedQuotes project directory<a name="auto1"/></h2>
20
21 <p>In order to run the Twisted Quotes example, you will need to do the
22 following:</p>
23
24 <ol>
25 <li>Make a <code>TwistedQuotes</code> directory on your system</li>
26 <li>Place the following files in the <code>TwistedQuotes</code> directory:
27     <ul>
28     <li><div class="py-listing"><pre><p class="py-linenumber">1
29 2
30 3
31 </p><span class="py-src-string">&quot;&quot;&quot;
32 Twisted Quotes
33 &quot;&quot;&quot;</span>
34 </pre><div class="caption">Source listing - <a href="listings/TwistedQuotes/__init__.py"><span class="filename">listings/TwistedQuotes/__init__.py</span></a></div></div> (this
35         file marks it as a package, see <a href="http://docs.python.org/tutorial/modules.html#packages" shape="rect">this section</a> of the Python tutorial for more on packages)</li>
36     <li><div class="py-listing"><pre><p class="py-linenumber"> 1
37  2
38  3
39  4
40  5
41  6
42  7
43  8
44  9
45 10
46 11
47 12
48 13
49 14
50 15
51 16
52 17
53 18
54 19
55 20
56 21
57 22
58 23
59 24
60 25
61 26
62 27
63 28
64 29
65 30
66 31
67 32
68 33
69 34
70 35
71 36
72 37
73 38
74 39
75 </p><span class="py-src-keyword">from</span> <span class="py-src-variable">random</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">choice</span>
76
77 <span class="py-src-keyword">from</span> <span class="py-src-variable">zope</span>.<span class="py-src-variable">interface</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">implements</span>
78
79 <span class="py-src-keyword">from</span> <span class="py-src-variable">TwistedQuotes</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">quoteproto</span>
80
81
82
83 <span class="py-src-keyword">class</span> <span class="py-src-identifier">StaticQuoter</span>:
84     <span class="py-src-string">&quot;&quot;&quot;
85     Return a static quote.
86     &quot;&quot;&quot;</span>
87
88     <span class="py-src-variable">implements</span>(<span class="py-src-variable">quoteproto</span>.<span class="py-src-variable">IQuoter</span>)
89
90     <span class="py-src-keyword">def</span> <span class="py-src-identifier">__init__</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">quote</span>):
91         <span class="py-src-variable">self</span>.<span class="py-src-variable">quote</span> = <span class="py-src-variable">quote</span>
92
93
94     <span class="py-src-keyword">def</span> <span class="py-src-identifier">getQuote</span>(<span class="py-src-parameter">self</span>):
95         <span class="py-src-keyword">return</span> <span class="py-src-variable">self</span>.<span class="py-src-variable">quote</span>
96
97
98
99 <span class="py-src-keyword">class</span> <span class="py-src-identifier">FortuneQuoter</span>:
100     <span class="py-src-string">&quot;&quot;&quot;
101     Load quotes from a fortune-format file.
102     &quot;&quot;&quot;</span>
103     <span class="py-src-variable">implements</span>(<span class="py-src-variable">quoteproto</span>.<span class="py-src-variable">IQuoter</span>)
104
105     <span class="py-src-keyword">def</span> <span class="py-src-identifier">__init__</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">filenames</span>):
106         <span class="py-src-variable">self</span>.<span class="py-src-variable">filenames</span> = <span class="py-src-variable">filenames</span>
107
108
109     <span class="py-src-keyword">def</span> <span class="py-src-identifier">getQuote</span>(<span class="py-src-parameter">self</span>):
110         <span class="py-src-variable">quoteFile</span> = <span class="py-src-variable">file</span>(<span class="py-src-variable">choice</span>(<span class="py-src-variable">self</span>.<span class="py-src-variable">filenames</span>))
111         <span class="py-src-variable">quotes</span> = <span class="py-src-variable">quoteFile</span>.<span class="py-src-variable">read</span>().<span class="py-src-variable">split</span>(<span class="py-src-string">'\n%\n'</span>)
112         <span class="py-src-variable">quoteFile</span>.<span class="py-src-variable">close</span>()
113         <span class="py-src-keyword">return</span> <span class="py-src-variable">choice</span>(<span class="py-src-variable">quotes</span>)
114 </pre><div class="caption">Source listing - <a href="listings/TwistedQuotes/quoters.py"><span class="filename">listings/TwistedQuotes/quoters.py</span></a></div></div></li>
115     <li><div class="py-listing"><pre><p class="py-linenumber"> 1
116  2
117  3
118  4
119  5
120  6
121  7
122  8
123  9
124 10
125 11
126 12
127 13
128 14
129 15
130 16
131 17
132 18
133 19
134 20
135 21
136 22
137 23
138 24
139 25
140 26
141 27
142 28
143 29
144 30
145 31
146 32
147 33
148 34
149 35
150 36
151 </p><span class="py-src-keyword">from</span> <span class="py-src-variable">zope</span>.<span class="py-src-variable">interface</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">Interface</span>
152
153 <span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">internet</span>.<span class="py-src-variable">protocol</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">Factory</span>, <span class="py-src-variable">Protocol</span>
154
155
156
157 <span class="py-src-keyword">class</span> <span class="py-src-identifier">IQuoter</span>(<span class="py-src-parameter">Interface</span>):
158     <span class="py-src-string">&quot;&quot;&quot;
159     An object that returns quotes.
160     &quot;&quot;&quot;</span>
161     <span class="py-src-keyword">def</span> <span class="py-src-identifier">getQuote</span>():
162         <span class="py-src-string">&quot;&quot;&quot;
163         Return a quote.
164         &quot;&quot;&quot;</span>
165
166
167
168 <span class="py-src-keyword">class</span> <span class="py-src-identifier">QOTD</span>(<span class="py-src-parameter">Protocol</span>):
169     <span class="py-src-keyword">def</span> <span class="py-src-identifier">connectionMade</span>(<span class="py-src-parameter">self</span>):
170         <span class="py-src-variable">self</span>.<span class="py-src-variable">transport</span>.<span class="py-src-variable">write</span>(<span class="py-src-variable">self</span>.<span class="py-src-variable">factory</span>.<span class="py-src-variable">quoter</span>.<span class="py-src-variable">getQuote</span>()+<span class="py-src-string">'\r\n'</span>)
171         <span class="py-src-variable">self</span>.<span class="py-src-variable">transport</span>.<span class="py-src-variable">loseConnection</span>()
172
173
174
175 <span class="py-src-keyword">class</span> <span class="py-src-identifier">QOTDFactory</span>(<span class="py-src-parameter">Factory</span>):
176     <span class="py-src-string">&quot;&quot;&quot;
177     A factory for the Quote of the Day protocol.
178
179     @type quoter: L{IQuoter} provider
180     @ivar quoter: An object which provides L{IQuoter} which will be used by
181         the L{QOTD} protocol to get quotes to emit.
182     &quot;&quot;&quot;</span>
183     <span class="py-src-variable">protocol</span> = <span class="py-src-variable">QOTD</span>
184
185     <span class="py-src-keyword">def</span> <span class="py-src-identifier">__init__</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">quoter</span>):
186         <span class="py-src-variable">self</span>.<span class="py-src-variable">quoter</span> = <span class="py-src-variable">quoter</span>
187 </pre><div class="caption">Source listing - <a href="listings/TwistedQuotes/quoteproto.py"><span class="filename">listings/TwistedQuotes/quoteproto.py</span></a></div></div></li>
188     </ul>
189 </li>
190 <li>Add the <code>TwistedQuotes</code> directory's <em>parent</em> to your Python
191 path. For example, if the TwistedQuotes directory's path is
192  <code>/mystuff/TwistedQuotes</code> or <code>c:\mystuff\TwistedQuotes</code>
193 add <code>/mystuff</code> to your Python path. On UNIX this would be <code class="shell">export PYTHONPATH=/mystuff:$PYTHONPATH</code>, on Microsoft
194 Windows change the <code class="shell">PYTHONPATH</code> variable through the
195 Systems Properties dialog by adding <code class="shell">;c:\mystuff</code> at the
196 end.</li>
197 <li>
198 Test your package by trying to import it in the Python interpreter:
199 <pre class="python-interpreter" xml:space="preserve">
200 Python 2.1.3 (#1, Apr 20 2002, 22:45:31)
201 [GCC 2.95.4 20011002 (Debian prerelease)] on linux2
202 Type &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
203 &gt;&gt;&gt; import TwistedQuotes
204 &gt;&gt;&gt; # No traceback means you're fine.
205 </pre>
206 </li>
207 </ol>
208
209 </div>
210
211     <p><a href="index.html">Index</a></p>
212     <span class="version">Version: 12.1.0</span>
213   </body>
214 </html>