html tool

2013年1月27日星期日

Dive into Python 的我的翻译 XXX------8.6 IV


       Example 8.15. More dictionary−based string formatting

def unknown_starttag(self, tag, attrs):
        strattrs = "".join([' %s="%s"' % (key, value) for key, value in attrs])
        self.pieces.append("<%(tag)s%(strattrs)s>" % locals()) 
  • [原文]P113
    When this method is called, attrs is a list of key/value tuples, just like the items of a dictionary, which means you can use multi−variable assignment to iterate through it. This should be a familiar pattern by now, but there's a lot going on here, so let's break it down:
    a. Suppose attrs is [('href', 'index.html'), ('title', 'Go to home page')].
    b. In the first round of the list comprehension, key will get 'href', and value will get 'index.html'.
    c.The string formatting ' %s="%s"' % (key, value) will resolve to ' href="index.html"'. This string becomes the first element of the list  comprehension's return value.
    d. In the second round, key will get 'title', and value will get 'Go to home page'.
    e. The string formatting will resolve to ' title="Go to home page"'.
    The list comprehension returns a list of these two resolved strings, and strattrs will join both elements of this list together to form ' href="index.html" title="Go to home page"'.


    [pope改进译]
    通过此方法的调用,attrs 是一个key/value 元组{tuples} 的列表,就像字典条目似的,这以为着你可以以多变量赋值之法遍历之(参见3.4.2. Assigning Multiple Values at Once)。它看起来很相似,但里面有不少的内容,所以让我们分解一下{break it down}:
    a. 假定{suppose} attrs 是[('href', 'index.html'), ('title', 'Go to home page')].
    b.此列表的首次 解析为,key如果是‘href’,那么value就会是‘index html’
    c.字符串样式{formatting} ' %s="%s"' % (key, value) 将被替换为  ' href="index.html"'。字符串替换为列表中可理解的(comprehension)返回值的第一个元素。
    d.在第二次循环时,key将是‘title’,value将是'Go to home page'.
    e.这个字符串将替换为' title="Go to home page"'.
    此列表解析后返回  被 分解成的两个的字符串 ,并且strattrs将两个list 元素 连接为' href="index.html" title="Go to home page"'。


    ------------------------------------------------------------------------

    [popexizhi]:意识到python的一个good之处 “strattrs = "".join([' %s="%s"' % (key, value) for key, value in attrs])”的表达如果让自己用c++写可能会是下面这样的:
    string key, value //这里还没有定义字符的长度,先使用这个类型吧
    for (i=0,i          { key = attres[i].[0];
               value = attres[i].[1];
               strattrs = strattres +“ ”+ key + “=”+ ‘”’+value+‘“’}

    嘻嘻,这个还有不少限制条件,也可能是自己的c++表达不够精炼吧?!

    ----------------------------------
    [pope译]
    通过此方法的调用,attrs 是一个key/value 元组{tuples} 的列表,就像字典条目似的,这以为着你可以使用它对多变量引用(参见3.4.2. Assigning Multiple Values at Once)。这个看起来很相似,但里面有不少的内容,所以让我们分解一下{break it down}:
    a. 假定{suppose} attrs 是[('href', 'index.html'), ('title', 'Go to home page')].
    b.首先对列表的理解{comprehension}是,key如果是‘href’,那么value就会是‘index html’
    c.字符串样式{formatting} ' %s="%s"' % (key, value) 将被替换为  ' href="index.html"'。字符串替换为列表中可理解的(comprehension)返回值的第一个元素。
    d.在第二次循环时,key将是‘title’,value将是'Go to home page'.
    e.这个字符串将替换为' title="Go to home page"'.
    可理解的(comprehension)列表返回两个替换字符后的列表,并且strattrs 将连接列表中的这两个元素为 ' href="index.html" title="Go to home page"'

    [net 译 来源:http://woodpecker.org.cn/diveintopython/html_processing/dictionary_based_string_formatting.html]
    当这个模块被调用时,attrs 是一个键/值 tuple 的 list,就像一个 dictionary 的 items。这就意味着我们可以使用多变量赋值来遍历它。到现在这将是一种熟悉的模式,但是这里有很多东西,让我们分开来看:
    a.假设 attrs 是 [('href', 'index.html'), ('title', 'Go to home page')]。
    b.在这个列表解析的第一轮循环中,key 将为 'href',value 将为 'index.html'。
    c.字符串格式化 ' %s="%s"' % (key, value) 将生成 ' href="index.html"'。这个字符串就作为这个列表解析返回值的第一个元素。
    d.在第二轮中,key 将为 'title',value 将为 'Go to home page'。
    e.字符串格式化将生成 ' title="Go to home page"'。
    这个 list 理解返回两个生成的字符串 list,并且 strattrs 将把这个 list 的两个元素连接在一起形成 ' href="index.html" title="Go to home page"'。

    [popexizhi
    which means you can use multi−variable assignment to iterate through it
    [pope译]
    这以为着你可以使用它对多变量引用(参见3.4.2. Assigning Multiple Values at Once)
    [net 译]
    这就意味着我们可以使用多变量赋值来遍历它
    [popexizhi]
    遍历这个字典的items为多个变量赋值,pope的问题是use multi-variable assigment to 翻译的还是有问题,感觉net翻译的也说的过去,但是很牵强,a list of key/value tuple的目的成为iterate through ,which是解释这个attrs的lists 的tuple的结构的说明用的,而attrs不目的可不是用来遍历的,遍历应该是种方式,目的是为multi-variable assignment 吧!一个不恰当的比喻,我们不能说“通过不挨饿的方式吃饭”吧?!这个在现代汉语中至少是不太接受,如果说古文吗?直译为"以多变量赋值之法遍历之",这样也是存在上面的问题。做为一个问题好好想想吧!

    b. In the first round of the list comprehension
    [pope译]首先对列表的理解{comprehension}是
    [net 译]在这个列表解析的第一轮循环中
    [popexizhi] pope太牛了,竟然把round 直接忽略了,再翻译,好吧,难怪结果面目全非。纠正一下应该是:“列表的首次 解析为”

    This string becomes the first element of the list  comprehension's return value.
    [pope译]字符串替换为列表中可理解的(comprehension)返回值的第一个元素。
    [net 译]这个字符串就作为这个列表解析返回值的第一个元素。
    [popexizhi]pope的句序没有问题,但是the 的确实翻译,让pope的翻译读起来一头雾水;the list comprehension 翻译为“此列表解析”更符合中文的语感,发现the 这种小词其实在句子中是不可忽略的翻译,否则感觉句子的feel差值千里啊。
    [pope改进译]此字符串成为 此列表解析 后返回的第一个元素。

    The list comprehension returns a list of these two resolved strings, and strattrs will join both elements of this list together to form ' href="index.html" title="Go to home page"'.
    [pope译]可理解的(comprehension)列表返回两个替换字符后的列表,并且strattrs 将连接列表中的这两个元素为 ' href="index.html" title="Go to home page"'
    [net 译]这个 list 理解返回两个生成的字符串 list,并且 strattrs 将把这个 list 的两个元素连接在一起形成 ' href="index.html" title="Go to home page"'。
    [popexizhi] the list comprehension 应该当作专用的翻译吧:)
    [pope改进译]此列表解析后返回  被 分解成的两个的字符串 ,并且strattrs将两个list 元素 连接为' href="index.html" title="Go to home page"'。
    ]
    [原文]P113
    self.pieces.append("<%(tag)s%(strattrs)s> " % locals())
    Now, using dictionary−based string formatting, you insert the value of tag and strattrs into a string. So if tag is 'a', the final result would be '',  and that is what gets appended to self.pieces.
    [popexizhi]strattrs 是上面定义过的,tag 是def 的input .原文的eg said:if tag = ‘a’,then 结果是''。直接翻译”that is what gets appended to self.pieces.“ 就是追加这个结果到self.pieces中。问题是这样看来这个self.pieces.append就是追加的意思,嘻嘻,再看吧!
    [原文]P113
    Using dictionary−based string formatting with locals is a convenient way of making complex string formatting
    expressions more readable, but it comes with a price. There is a slight performance hit in making the call to locals,
    since locals builds a copy of the local namespace.

    [popexizhi]use locals 格式化字符串是方便,但是有代价。就是调用locasls时,系统要给一个locasls的拷贝。这样分析就是每一次使用一个locasls时,就copy一个,要是比较大时,确实资源使用比较多    

没有评论:

发表评论