当前时区为 UTC + 8 小时



发表新帖 回复这个主题  [ 4 篇帖子 ] 
作者 内容
1 楼 
 文章标题 : 三十分钟学会Zope3(整理帖)
帖子发表于 : 2005-10-13 12:24 
头像

注册: 2005-04-10 16:54
帖子: 2625
地址: 温州大学
送出感谢: 0 次
接收感谢: 1
为方便大家查阅,现将三十分钟学会Zope3整理成本帖,请指正:
=======================================
Zope3 In 30 Minutes
三十分钟学会Zope3
Author: Baiju M <baiju.m.mail AT gmail.com>
Version: 0.1.4
Copyright: (C) 2005 Baiju M, Placed under GNU GPL version 2, or (at your option) any later version
翻译:firehare
校正:leal
组织:ubuntu.org.cn

Contents
目录
* Why 30? 为什么是30分钟
* And why should I ...? 为什么我要学...
* Keywords 关键词
* So let's start... 那么让我们开始
* BookMarker : Your first Zope3 app BookMarker:你的第一个Zope3应用
* Interfaces 接口
* Unit Testing 单元测试
* Real coding! 正式编码
* Configuration 配置
* Just run it
* Views 视图
* Functional testing 功能测试
* Now what? 现在学什么?


Important Note: I assume you are using Debian 3.1 (Sarge) or similar systems.
特别注意:我假设你正在使用Debian3.1(Sarge)或相似系统

Why 30?
为什么是30分钟


Sorry! Zope3 cannot be introduced in 10 minutes.
很抱歉!Zope3是不可能在10分钟内介绍清楚的

And why should I ...?
为什么我要学?


If you are looking for a Pythonic framework for web application development, just continue reading. And I am not going to make a new definition for What is Pythonic? :) . Here I will try to answer your "why?" questions.
如果你想为基于Web的应用开发作一个Pythonic的框架,那么请继续。我并不打算对什么是Pythonic作任何新的解释:),主要试图回答“为什么”的问题。

Keywords
关键词


Python, Zope, Interface, Component, ZCML, ZMI, Adapter, View, Event, Service, Utility, Principal.

So let's start...
那么让我们开始...


Zope3 is the third generation of Zope, a framework for web applications. You can download Zope 3.1 from http://www.zope.org/Products/Zope3 . To install untar Zope3 source package, then as root:
Zope3是第三代的Zope,一个Web应用框架。你可以从http://www.zope.org/Products/zope3下载Zope3.1,解压Zope3源包,并以根用户权限运行:
代码:
# cd Zope-3.1.0
# ./configure;make;make install

译者的话:
如果是在Ubuntu下,请按下面命令运行:
代码:
$tar -zxvf Zope-3.1.0.tgz -C /tmp
(如果是Breezy下,请安装Python2.3 Python2.3-dev等相关包,因为Zope3是基于Python2.4.1和Python2.3.5开发的,而Breezy默认是Python2.4.2,所以必须安装Python2.3的包文件了)
$./configure --prefix /opt/zope
$make
$make check
$make install


After installation you have to make an instance (don't worry! just do it). To do so:
安装完成后,你必须建立一个Zope Instance(别担心!只需照作就行)。命令如下:
代码:
$ cd /usr/local/Zope-3.1.0/bin
$ ./mkzopeinstance --dir=$HOME/myzope --user=admin:secret123


To start Zope3 server, go to your instance directory, then:
然后是启动你的Zope3服务,到你的Instance目录下,然后运行:
代码:
$ cd ~/myzope
$ ./bin/runzope


If you get a port error, check check 8080 and 8021 is already used by other programs; for the time being, just stop it. Start your browser , the open http://localhost:8080 . What you see is the Zope Management Interface (ZMI). ZMI is your Python prompt, hmm... no! Zope prompt, got it?. You can login and look around to see what's happening. If you played enough with ZMI, stop it from terminal (Control + C).
如果你得到一个端口错误,请检查8080和8021端口是否被其他程序占用;暂时停止运行该程序。打开你的浏览器,然后输入http://localhost:8080。你将看到Zope Management Interface(ZMI)。ZMI是你的Python提示符,嗯...不!应该是Zope的提示符,看到了吗?你可以登录进去并且到处点击看看。如果你在ZMI玩够了的话,在终端用Ctrl+C中断它。

BookMarker : Your first Zope3 app
BookMarker:你的第一个Zope3应用


Yes! we are going to create a Zope3 application, an online book marker. Our app will display links to websites and a description for each link.
是的!我们打算开始创建一个Zope3应用,一个在线书签。我们的应用将显示到网站的链接和每条链接的描述。

So, what you have to think about when you start a Zope3 project. Oh! sorry! I can't put it in one sentence, you better learn and practice Extreme Programming http://en.wikipedia.org/wiki/Extreme_programming . Anyway, after your initial design, you will write interfaces. Let us hope Python 3.0 will make it much easier. Then you will write unit tests, now your ideas become very concrete!. At last write your real code. You will be satisfied when you implement interfaces one by one and unit tests succeeds!. I have given the source code of BookMarker here: boom.tar.bz2
那么,在开始一个Zope3项目前,你得做何准备呢?哦!很抱歉!我没法一言以蔽之,你最好先学习并实践XP(极限编程,一种轻量级的软件开发过程)http://en.wikipedia.org/wiki/Extreme_programming 。总之,在你初步设计之后,就将编写接口。让我们期待Python 3.0 会让它变得容易些吧!然后开始编写单元测试,至此你的想法已非常具体!最后编写正式代码。当你一个接一个的实现接口并通过单元测试,你会拥有(前所未有的)满足感! 我已经在这里给出了BookMarker应用的源代码:boom.tar.bz2

Our code will be placed at $HOME/myzope/lib/python/boom
我们的代码将放在$HOME/myzope/lib/python/boom目录中

First create a file interfaces.py where we will keep our interfaces. Later we will implement these interfaces one by one, with strong support of unit testing.
首先创建一个interfaces.py的文件,我们将在该文件内保存所有接口。后面我们将在单元测试的强力支持下,一个接一个的实现这些接口。

Interfaces
接口


Here is our interfaces.py:
下面就是我们的interfaces.py:
代码:
from zope.interface import Interface
from zope.schema import Text, TextLine, Field

from zope.app.container.constraints import ContainerTypesConstraint
from zope.app.container.constraints import ItemTypePrecondition
from zope.app.container.interfaces import IContained, IContainer

class IMark(Interface):
    """This is the book mark object."""

    url = TextLine(
        title=u"URL/Link",
        description=u"URL of the website",
        default=u"http://www.zope.org",
        required=True)

    description = Text(
        title=u"Description",
        description=u"Description of the website",
        default=u"",
        required=False)

class IBookMarker(IContainer):
    """This is the container for all book marks."""

    name = TextLine(
        title=u"Name of BookMarker",
        description=u"A name for BookMarker",
        default=u"",
        required=True)

    def __setitem__(name, obj):
        pass

    __setitem__.precondition = ItemTypePrecondition(IMark)


class IMarkContained(IContained):
    """A book mark can only contain in a BookMarker"""

    __parent__ = Field(
        constraint = ContainerTypesConstraint(IBookMarker))


Our first interface IMark has two attributes, one is the URL of the site and the other one is the description. Please note, IMark is not a class even though we used Python's class definition. We inherited from Interface to make it an interface. Second one is a container interface, which is an extended IContainer interface. By using this container interface we can persist our data (instances of IMark implementations). We will put all objects of IMark in a container object of IBookMarker. We will implement IMark along with IMarkContained as a constraint interface. So that IMark object will be only contained in an IBookMarker object.
我们的第一个接口IMark有两个属性,一个是站点的URL,另 一个是它的描述。请注意,IMark不是一个类,尽管我们使用了Python的类定义。IMark继承自Interface,因此是个接口。第二个是一个容器接口,它是一个扩展的Icontainer接口。我们可以使用这个容器接口来保存我们的数据(IMark实现的实例)。我们将IMark的所有对象放到IBookMarker的一个容器对象里。我们连同IMarkContained(作为一个约束接口)一起来实现IMark。所以IMark对象只能包含在IBookMarker对象中。

Unit Testing
单元测试


Now create tests.py and put the following code there:
现在创建tests.py文件,输入下列语句:
代码:
import unittest
from zope.testing.doctestunit import DocTestSuite

from zope.app.container.tests.test_icontainer import TestSampleContainer

from boom.bookmarker import BookMarker, Mark


class BookMarkerContainerTest(TestSampleContainer):
 
    def makeBookMarkerObject(self):
        return BookMarker()

def test_suite():
    return unittest.TestSuite((
        DocTestSuite('boom.bookmarker'),
        unittest.makeSuite(BookMarkerContainerTest),
        ))
         
if __name__ == '__main__':
    unittest.main(defaultTest='test_suite')


Actually, we are not written any unit tests here, but this will make our doc tests working automatically.
实际上我们在这里并没有编写任何的单元测试,不过它会自动对我们的文档进行测试

To run the unit test:
然后运行单元测试
代码:
$ cd $HOME/myzope/lib
$ ../bin/test -vpu --dir boom


Real coding!
正式编码


Now let's move on to the implementation (bookmarker.py):
现在让我们继续去实现(bookmarker.py)
代码:
__docformat__ = 'restructuredtext'

from zope.interface import implements
from zope.app.container.btree import BTreeContainer
from zope.app.container.contained import Contained

from boom.interfaces import IMark, IMarkContained, IBookMarker

class Mark(Contained):
    """Implementation of IMark

    Make sure that the `Mark` implements the `IMark` interface::

      >>> from zope.interface.verify import verifyClass
      >>> verifyClass(IMark, Mark)
      True

    Make sure that the `Mark` implements the `IMarkContained` interface:

      >>> from zope.interface.verify import verifyClass
      >>> verifyClass(IMarkContained, Mark)
      True

    An example of checking the url of Mark::

      >>> mk = Mark()
      >>> mk.url
      u'http://www.zope.org'
      >>> mk.url = u'http://www.python.org'
      >>> mk.url
      u'http://www.python.org'
 
    An example of checking the description of Mark::

      >>> mk = Mark()
      >>> mk.description
      u''
      >>> mk.description = u'Zope Project Web Site'
      >>> mk.description
      u'Zope Project Web Site'
    """

    implements(IMark, IMarkContained)

    url = u"http://www.zope.org"
    description = u""


class BookMarker(BTreeContainer):
    """Implementation of IBookMarker using B-Tree Container

    Make sure that the `BookMarker` implements the `IBookMarker` interface::

      >>> from zope.interface.verify import verifyClass
      >>> verifyClass(IBookMarker, BookMarker)
      True

    An example of changing the name of BookMarker::

      >>> bm = BookMarker()
      >>> bm.name
      u''
      >>> bm.name = u'MyBookMarker'
      >>> bm.name
      u'MyBookMarker'
    """

    implements(IBookMarker)

    name = u""


We have written doctests along with the implementations. Doctests are accompanied with examples, so it is called example driven unit testing.
我们已经在实现的同时也编写了Doctests,该Doctests是同例子一起的,所以这种实现方式我们称之为事例驱动单元测试

Configuration
配置


Now configuration (save in configure.zcml):
现在编写配置(保存在configure.zcml文件中):

代码:
<configure
    xmlns="http://namespaces.zope.org/zope"
    xmlns:browser="http://namespaces.zope.org/browser">

  <interface
      interface=".interfaces.IBookMarker"
      type="zope.app.content.interfaces.IContentType"
      />

  <content class=".bookmarker.BookMarker">
    <implements
        interface="zope.app.annotation.interfaces.IAttributeAnnotatable"
        />
    <implements
        interface="zope.app.container.interfaces.IContentContainer"
        />
    <factory
        id="boom.bookmarker.BookMarker"
        description="Book Marker"
        />
    <require
        permission="zope.ManageContent"
        interface=".interfaces.IBookMarker"
        />
    <require
        permission="zope.ManageContent"
        set_schema=".interfaces.IBookMarker"
        />
  </content>

  <interface
      interface=".interfaces.IMark"
      type="zope.app.content.interfaces.IContentType"
      />

  <content class=".bookmarker.Mark">
    <implements
        interface="zope.app.annotation.interfaces.IAttributeAnnotatable"
        />
    <factory
        id="boom.bookmarker.Mark"
        description="A book mark."
        />
    <require
        permission="zope.ManageContent"
        interface=".interfaces.IMark"/>
    <require
        permission="zope.ManageContent"
        set_schema=".interfaces.IMark"
        />
  </content>

  <browser:addform
      label="Add Book Marker"
      name="AddBookMarker.html"
      schema="boom.interfaces.IBookMarker"
      content_factory="boom.bookmarker.BookMarker"
      fields="name"
      permission="zope.ManageContent"
      />

  <browser:addMenuItem
      class=".bookmarker.BookMarker"
      title="Book Marker"
      permission="zope.ManageContent"
      view="AddBookMarker.html"
      />

  <browser:editform
      schema="boom.interfaces.IBookMarker"
      for="boom.interfaces.IBookMarker"
      label="Change Book Marker"
      name="edit.html"
      permission="zope.ManageContent"
      menu="zmi_views" title="Edit"
      />

<browser:containerViews
    for="boom.interfaces.IBookMarker"
    index="zope.View"
    contents="zope.View"
    add="zope.ManageContent"
    />

<browser:addform
    label="Add Mark"
    name="AddMark.html"
    schema="boom.interfaces.IMark"
    content_factory="boom.bookmarker.Mark"
    fields="url description"
    permission="zope.ManageContent"
    />

<browser:addMenuItem
    class="boom.bookmarker.Mark"
    title="Mark"
    description="URL of Website"
    permission="zope.ManageContent"
    view="AddMark.html"
    />

<browser:editform
    schema="boom.interfaces.IMark"
    for="boom.interfaces.IMark"
    label="Change Mark"
    fields="url description"
    name="edit.html"
    permission="zope.ManageContent"
    menu="zmi_views" title="Edit"
    />

<browser:page
    name="marks.html"
    for="boom.interfaces.IBookMarker"
    class=".browser.BookMarks"
    template="marks.pt"
    permission="zope.Public"
    menu="zmi_views"
    title="Marks"
    />

</configure>


Is it self explanatory? "no...!" then ok! we will discuss Zope Configuration Markup Language (ZCML) briefly later. Actually, if you are familiar with ZCML this configuartion will be more than self explanatory. It will give you an overall idea about the entire application. Now you might think, it is not Pythonic :( Hey! think twice!.
它能不言自明(即能够自我解释,XML之类的标记语言都有这个特点)吗?“不...!”好吧,我们将稍后简单的谈谈Zope Configuration Markup Language(ZCML,Zope配置标记语言)。实际上,如果你对ZCML熟悉的话,你会发现这个配置不止是不言自明,它将带给你的是对整个应用的总体思路。现在你可能在想,这并不Pythonic嘛 :( ,嗨!再想想!

Just run it

As the last step to work our application, put the following line in:$HOME/myzope/etc/package-icludes/boom-configure.zcml:
最后一步就是要让我们的应用运行起来,在文件$HOME/myzope/etc/package-icludes/boom-configure.zcml中插入下列行:
代码:
<include package="boom"/>


Now you registered your package.
现在你已经注册了你的包。

Run zope again, then open your browser, add a BookMarker and few book marks.
重新启动Zope,然后打开你的浏览器,添加一个BookMarker和一些书签。

Now you want to arrange your your book marks in a better way, don't you?. For the time being, just relax, then we will create a view for book marks.
现在,你不想让你的书签排列得更好点吗?先暂时休息一下,之后我们将为书签创建一个视图。

Views
视图


Now create a file named browser.py with following code:
现在新建一个名为browser.py的文件,并输入下列代码:
代码:
from boom.interfaces import IMark

class BookMarks:

    def __init__(self, context, request, base_url=''):
        self.context = context
        self.request = request
        self.base_url = base_url

    def listMarks(self):
        marks = []
        for name, child in self.context.items():
            if IMark.providedBy(child):
                info = {}
                info['url'] = child.url
                info['description'] = child.description
                marks.append(info)
        return marks

Then one template (marks.pt):
然后创建一个模板(marks.pt)
代码:
<html metal:use-macro="views/standard_macros/view">
  <body>
    <div metal:fill-slot="body">

      <div class="row">
        <div class="label">Book Marks:</div>
        <br/><br/>
          <li tal:repeat="item view/listMarks">

            <a href="" tal:attributes="href item/url">
               <span tal:content="item/url">Link</span>
            </a>
            <pre tal:content="item/description">Description</pre>
            <br/>

          </li>
      </div>

    </div>
  </body>
</html>


Now by clicking on "Marks" tab you can see all book marks.
现在点击一下“Marks”标签,你就可以看到所有的书签了。

Ok! this is not the end, just the beginning of your study.
好的!这样可不能算是结束,这仅仅是你学习的开始。

Functional testing
功能测试


Let's finish our example writing a functional test for view (ftests.py):
让我们为视图写一个功能测试来结束我们的例子(ftests.py)
代码:
import unittest
from zope.app.testing.functional import BrowserTestCase

class BookMarksTest(BrowserTestCase):

    def testMarksListing(self):
        pass


def test_suite():
    return unittest.TestSuite((
        unittest.makeSuite(BookMarksTest),
        ))

if __name__ == '__main__':
    unittest.main(defaultTest='test_suite')

To run the functional test:
然后运行该功能测试:
代码:
$ cd $HOME/myzope/lib
$ ../bin/test -vpf --dir boom


Now what?
现在学什么?


Now you can start learning Zope3 in detail, using Zope3 book. Also join zope3-users mailing list.
现在你可以通过Zope3 Book开始详细学习Zope3了。也可以加入到zope3-user邮件列表里。

There is a good Zope3 quick start guide by Benji York: http://www.benjiyork.com/quick_start/
这里有Benji York写的一个不错的Zope3快速入门指南: http://www.benjiyork.com/quick_start/
A good introductory book is also available in print, visit: http://worldcookery.com/
还有一本已出版的介绍性图书,很不错,请访问: http://worldcookery.com/

Just one more thing: I want to improve this document, so don't hesitate to write your feedback to: baiju.m.mail AT gmail.com
还有一点:我想不断完善这份文档,因此有何意见/建议千万别犹豫,请发送到: baiju.m.mail@gmail.com


_________________
我心无畏,源自于我心无知。
图片


页首
 用户资料  
 
2 楼 
 文章标题 :
帖子发表于 : 2005-10-13 18:24 

注册: 2005-09-26 19:26
帖子: 108
送出感谢: 0 次
接收感谢: 0 次
ubuntu+python
看趋势将成为黄金搭档。
如果python有java那么好的GUI,那完全可以成为linux下应用开发的主力。


页首
 用户资料  
 
3 楼 
 文章标题 :
帖子发表于 : 2005-10-19 15:03 

注册: 2005-05-24 16:44
帖子: 55
送出感谢: 0 次
接收感谢: 0 次
python 下的gui 开发也是很爽的,个人认为比java的要好

你可以看一下wxPython


页首
 用户资料  
 
4 楼 
 文章标题 :
帖子发表于 : 2005-10-19 15:34 

注册: 2005-05-17 14:45
帖子: 334
送出感谢: 0 次
接收感谢: 0 次
:D 好贴,谢谢了


_________________
现在还是在学java!


页首
 用户资料  
 
显示帖子 :  排序  
发表新帖 回复这个主题  [ 4 篇帖子 ] 

当前时区为 UTC + 8 小时


在线用户

正在浏览此版面的用户:没有注册用户 和 1 位游客


不能 在这个版面发表主题
不能 在这个版面回复主题
不能 在这个版面编辑帖子
不能 在这个版面删除帖子
不能 在这个版面提交附件

前往 :  
本站点为公益性站点,用于推广开源自由软件,由 DiaHosting VPSBudgetVM VPS 提供服务。
我们认为:软件应可免费取得,软件工具在各种语言环境下皆可使用,且不会有任何功能上的差异;
人们应有定制和修改软件的自由,且方式不受限制,只要他们自认为合适。

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
简体中文语系由 王笑宇 翻译