Twisted Documentation: Setting up the TwistedQuotes application

# Setting up the TwistedQuotes application

## Goal

This document describes how to set up the TwistedQuotes application used in a number of other documents, such as designing Twisted applications.

## Setting up the TwistedQuotes project directory

In order to run the Twisted Quotes example, you will need to do the following:

1. Make a `TwistedQuotes` directory on your system
2. Place the following files in the `TwistedQuotes` directory:

   -     1
         2
         3
         """
         Twisted Quotes
         """

     Source listing - listings/TwistedQuotes/__init__.py
     (this file marks it as a package, see this section of the Python tutorial for more on packages)

   -      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
         from random import choice

         from zope.interface import implements

         from TwistedQuotes import quoteproto

         class StaticQuoter:
             """
             Return a static quote.
             """

             implements(quoteproto.IQuoter)

             def __init__(self, quote):
                 self.quote = quote

             def getQuote(self):
                 return self.quote

         class FortuneQuoter:
             """
             Load quotes from a fortune-format file.
             """
             implements(quoteproto.IQuoter)

             def __init__(self, filenames):
                 self.filenames = filenames

             def getQuote(self):
                 quoteFile = file(choice(self.filenames))
                 quotes = quoteFile.read().split('\n%\n')
                 quoteFile.close()
                 return choice(quotes)

     Source listing - listings/TwistedQuotes/quoters.py

   -      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
         from zope.interface import Interface

         from twisted.internet.protocol import Factory, Protocol

         class IQuoter(Interface):
             """
             An object that returns quotes.
             """
             def getQuote():
                 """
                 Return a quote.
                 """

         class QOTD(Protocol):
             def connectionMade(self):
                 self.transport.write(self.factory.quoter.getQuote()+'\r\n')
                 self.transport.loseConnection()

         class QOTDFactory(Factory):
             """
             A factory for the Quote of the Day protocol.

             @type quoter: L{IQuoter} provider
             @ivar quoter: An object which provides L{IQuoter} which will be used by
                 the L{QOTD} protocol to get quotes to emit.
             """
             protocol = QOTD

             def __init__(self, quoter):
                 self.quoter = quoter

     Source listing - listings/TwistedQuotes/quoteproto.py
3. Add the `TwistedQuotes` directory's parent to your Python path. For example, if the TwistedQuotes directory's path is `/mystuff/TwistedQuotes` or `c:\mystuff\TwistedQuotes` add `/mystuff` to your Python path. On UNIX this would be `export PYTHONPATH=/mystuff:$PYTHONPATH`, on Microsoft Windows change the `PYTHONPATH` variable through the Systems Properties dialog by adding `;c:\mystuff` at the end.
4. Test your package by trying to import it in the Python interpreter:

       Python 2.1.3 (#1, Apr 20 2002, 22:45:31)
       [GCC 2.95.4 20011002 (Debian prerelease)] on linux2
       Type "copyright", "credits" or "license" for more information.
       >>> import TwistedQuotes
       >>> # No traceback means you're fine.

Index

Version: 11.1.0