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">
3
<title>Twisted Documentation: Setting up the TwistedQuotes application</title>
4
<link href="stylesheet.css" rel="stylesheet" type="text/css"/>
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>
14
<h2>Goal<a name="auto0"/></h2>
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>
19
<h2>Setting up the TwistedQuotes project directory<a name="auto1"/></h2>
21
<p>In order to run the Twisted Quotes example, you will need to do the
25
<li>Make a <code>TwistedQuotes</code> directory on your system</li>
26
<li>Place the following files in the <code>TwistedQuotes</code> directory:
28
<li><div class="py-listing"><pre><p class="py-linenumber">1
31
</p><span class="py-src-string">"""
33
"""</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
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>
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>
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>
83
<span class="py-src-keyword">class</span> <span class="py-src-identifier">StaticQuoter</span>:
84
<span class="py-src-string">"""
85
Return a static quote.
86
"""</span>
88
<span class="py-src-variable">implements</span>(<span class="py-src-variable">quoteproto</span>.<span class="py-src-variable">IQuoter</span>)
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>
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>
99
<span class="py-src-keyword">class</span> <span class="py-src-identifier">FortuneQuoter</span>:
100
<span class="py-src-string">"""
101
Load quotes from a fortune-format file.
102
"""</span>
103
<span class="py-src-variable">implements</span>(<span class="py-src-variable">quoteproto</span>.<span class="py-src-variable">IQuoter</span>)
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>
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
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>
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>
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">"""
159
An object that returns quotes.
160
"""</span>
161
<span class="py-src-keyword">def</span> <span class="py-src-identifier">getQuote</span>():
162
<span class="py-src-string">"""
164
"""</span>
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>()
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">"""
177
A factory for the Quote of the Day protocol.
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
"""</span>
183
<span class="py-src-variable">protocol</span> = <span class="py-src-variable">QOTD</span>
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>
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>/tmp/TwistedQuotes</code>
193
add <code>/tmp</code> to your Python path. On UNIX this would be <code class="shell">export PYTHONPATH=/my/stuff:$PYTHONPATH</code>, on Microsoft
194
Windows change the <code class="shell">PYTHONPATH</code> variable through the
195
Systems Properites dialog to add <code class="shell">/my/stuff;</code> at the
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 "copyright", "credits" or "license" for more information.
203
>>> import TwistedQuotes
204
>>> # No traceback means you're fine.
211
<p><a href="index.html">Index</a></p>
212
<span class="version">Version: 10.0.0</span>
b'\\ No newline at end of file'