潜入python书中对kgp.py程序的具体怀疑

Vai*_*pai 3 python xml

深入Python:XML处理 -

这里我指的是一部分kgp.py节目 -

def getDefaultSource(self):
  xrefs = {}
  for xref in self.grammar.getElementsByTagName("xref"):
    xrefs[xref.attributes["id"].value] = 1
  xrefs = xrefs.keys()
  standaloneXrefs = [e for e in self.refs.keys() if e not in xrefs]
  if not standaloneXrefs:
    raise NoSourceError, "can't guess source, and no source specified"
  return '<xref id="%s"/>' % random.choice(standaloneXrefs)
Run Code Online (Sandbox Code Playgroud)

self.grammar:解析XML表示(using xml.dom.minidom) -

<?xml version="1.0" ?>
<grammar>
<ref id="bit">
  <p>0</p>
  <p>1</p>
</ref>
<ref id="byte">
  <p><xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/>\
<xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/></p>
</ref>
</grammar>
Run Code Online (Sandbox Code Playgroud)

self.refs:是他们所有refs上述XML密钥的缓存id


我对这段代码有两个疑问:

疑惑1:

  for xref in self.grammar.getElementsByTagName("xref"):
    xrefs[xref.attributes["id"].value] = 1
  xrefs = xrefs.keys()
Run Code Online (Sandbox Code Playgroud)

eventaully xrefs将id值保存在列表中.难道我们不能简单地通过 -

  xrefs = [xref.attributes["id"].value 
           for xref in self.grammar.getElementsByTagName("xref")]
Run Code Online (Sandbox Code Playgroud)

疑惑2:

  standaloneXrefs = [e for e in self.refs.keys() if e not in xrefs]
  ...
  return '<xref id="%s"/>' % random.choice(standaloneXrefs)
Run Code Online (Sandbox Code Playgroud)

在这里,我们都节省了ref,从self.refs我们并不在我们的计算见xrefs.但接下来不是创建<ref>元素,而是创建<xref>具有相同ID 的元素.这让我们向后退了一步,因为以后我们无论如何都会找到这个计算的交叉引用<xref>并最终到达<ref>.我们本来可以从一开始就开始这样<ref>做.


放弃

我绝不想试着在这本书上发表评论.我甚至没有资格.

我很喜欢读这本书的每一刻.我发现很少有章节已经过时,但我喜欢Mark Pilgrim的写作风格,我不能停止阅读.

Gar*_*ees 7

Dive Into Python已经七年了(2004年出版),并不总是包含最现代的代码.所以你需要轻松上手:潜入Python 3可能是一个更好的选择.

你怀疑的建议1改变了代码的含义:将id放入字典的键中然后再将它们删除可以消除重复,而列表理解包括重复.现代的方法是使用集合理解:

 xrefs = {xref.attributes["id"].value 
          for xref in self.grammar.getElementsByTagName("xref")}
Run Code Online (Sandbox Code Playgroud)

但这在2004年没有.

关于你的疑问2,我不完全确定我看到了问题.是的,从某种意义上讲,这是一种浪费,但另一方面,代码已经有了xref案例处理程序,因此重新使用该处理程序而不是添加额外的特殊情况是有意义的.

该示例中还有其他几个代码可以进行现代化.例如,

source and source or self.getDefaultSource()
Run Code Online (Sandbox Code Playgroud)

现在会source or self.getDefaultSource().和线

standaloneXrefs = [e for e in self.refs.keys() if e not in xrefs]
Run Code Online (Sandbox Code Playgroud)

将更好地表达为设置差异操作,例如:

standaloneXrefs = set(self.refs) - set(xrefs)
Run Code Online (Sandbox Code Playgroud)

但随着语言变得更具表现力,会发生这种情况:旧代码开始看起来相当不优雅.