| 「五冠真品」李小龙类似款手感巨好纳帕革皮衣-感动 20699 | ||
| 199.0元 | ![]() | |
原文:http://www.klipdas.com/blog/?p=setting-up-twistedquotes-application
2.3 建立 TwistedQuotes应用
2.3.1 目标.
本文档介绍如何使用其他一些文档来建立TwistedQuotes应用,例如Twisted应用设计(24页)。
2.3.2 建立TwistedQuotes项目目录
为运行Twisted Quotes示例,你会需要做下面的工作:
在系统中创建TwistedQuotes目录;
将下面的文件放到TwistedQuotes目录下:
__init__.py
"""Twisted Quotes."""
quoters.py
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)
quoteproto.py
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
plugins.tml
register("Quote of the Day TAP Builder",
"TwistedQuotes.quotetap",
description="""
Example of a TAP builder module.
""",
type="tap",
tapname="qotd")
将TwsitedQuotes目录的上一级目录添加到Python path,例如,如果TwistedQuotes目录是/tmp/TwistedQuotes,那么就将/tmp添加到Python path。在Unix下,可以使用export PYTHONPATH=/my/stuff:$PYTHONPATH,Winodws下,在系统变量PYTHONPATH前添加/my/stuff;。
在Python解释器中试着导入包:
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.
本文介绍如何搭建TwistedQuotes应用,包括创建项目目录、编写核心代码并配置环境变量等步骤。


309

被折叠的 条评论
为什么被折叠?



