建立 Twistedquotes应用

本文介绍如何搭建TwistedQuotes应用,包括创建项目目录、编写核心代码并配置环境变量等步骤。
「五冠真品」李小龙类似款手感巨好纳帕革皮衣-感动 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.

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值