Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1745652
  • 博文数量: 335
  • 博客积分: 4690
  • 博客等级: 上校
  • 技术积分: 4341
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-08 21:38
个人简介

无聊之人--除了技术,还是技术,你懂得

文章分类

全部博文(335)

文章存档

2016年(29)

2015年(18)

2014年(7)

2013年(86)

2012年(90)

2011年(105)

分类: Python/Ruby

2011-07-01 19:40:30

4.8. Putting It All Together

4.8 拼装

The last line of code, the only one you haven't deconstructed yet, is the one that does all the work. But by now the work is easy, because everything you need is already set up just the way you need it. All the dominoes are in place; it's time to knock them down.

最后一行,同时也是唯一的一个一行迄今还没有分解的语句,完场了所有的工作。但是前面的工作很简单,这是因为你所需要的知识都按照你所需要的方式学习。所有的多米诺模块都摆放整齐,是时候推到它了。

This is the meat of apihelper.py:

这是apihelper.py函数的核心

  1. print "\n".join(["%s %s" %
  2.                       (method.ljust(spacing),
  3.                        processFunc(str(getattr(object, method).__doc__)))
  4.                      for method in methodList])

   Note that this is one command, split over multiple lines, but it doesn't use the line continuation character (\). Remember when I said that some expressions can be split into multiple lines without using a backslash? A list comprehension is one of those expressions, since the entire expression is contained in square brackets.

注意这是一个命令行语句,通过多行来分隔,但是没有使用续行符’\n’来分隔。你需要记住的是,每当我说某些表达式可以分割成多行时而不必要使用续行符(\n?list复合操作就是其中的一个表达式,因为整个表达式都被包含在方括号中。

Now, let's take it from the end and work backwards. The

现在让我们从后向前分析该表达式:

 

  1. for method in methodList

shows that this is a list comprehension. As you know, methodList is a list of all the methods you care about in object. So you're looping through that list with method.

该句说明这是一个列表复合操作。Methodlist是一个包含了对象中你所关心的所有方法的列表。因此你用变量method来循环给列表。

Example 4.22. Getting a doc string Dynamically

4.22 动态获取文档化字符串

  1. >>> import odbchelper
  2. >>> object = odbchelper
  3. >>> method = 'buildConnectionString'
  4. >>> getattr(object, method)
  5. <function buildConnectionString at 010D6D74>
  6. >>> print getattr(object, method).__doc__
  7. Build a connection string from a dictionary of parameters.
  8.  
  9.     Returns string.

1

In the info function, object is the object you're getting help on, passed in as an argument.

info函数中,object是你想获取帮助的对象,作为一个参数传递进去

2

As you're looping through methodList, method is the name of the current method.

在你循环methodlist是,method是当前方法的名字。

3

Using the getattr function, you're getting a reference to the method function in the object module.

调用getattr函数,你获得了object模块method函数的一个引用

4

Now, printing the actual doc string of the method is easy.

现在打印method函数的实际doc string就变得很容易了。

The next piece of the puzzle is the use of str around the doc string. As you may recall, str is a built-in function that coerces data into a string. But a doc string is always a string, so why bother with the str function? The answer is that not every function has a doc string, and if it doesn't, its __doc__ attribute is None.

下面的饿代码是对doc string使用str函数。你或许会记得,str是一个内置函数,它能将数据强制转换成字符串。但是doc string 本身就是一个字符串,那为什么还要使用str函数呢?这是因为并不是每个函数都定义有doc string,如果函数没有定义,那么__doc__属性值就是None

Example 4.23. Why Use str on a doc string?

4.23 doc string上使用str的原因

  1. >>> >>> def foo(): print 2
  2. >>> >>> foo()
  3. 2
  4. >>> >>> foo.__doc__
  5. >>> foo.__doc__ == None
  6. True
  7. >>> str(foo.__doc__)
  8. 'None'

1

You can easily define a function that has no doc string, so its __doc__ attribute is None. Confusingly, if you evaluate the __doc__ attribute directly, the Python IDE prints nothing at all, which makes sense if you think about it, but is still unhelpful.

你可以很轻松的定义一个没有doc string属性的函数,这样,__doc__属性的值就是None。容易混淆的是,如果你直接对__doc__求值,Python IDE不打印任何值,这看起来和你想的一样,但是这是没有任何帮助的。

 

 

2

You can verify that the value of the __doc__ attribute is actually None by comparing it directly.

你可以通过比较来确定__doc__的属性实际值 None

 

 

3

The str function takes the null value and returns a string representation of it, 'None'.

Str函数传递进去空值,然后返回一个代表空值的字符串,‘None’。

 

 

 

Note

 

 

In SQL, you must use IS NULL instead of = NULL to compare a null value. In Python, you can use either == None or is None, but is None is faster.

在SQL中,你必须使用 is NULL而不是NULL来判断一个值是否是空值。而在Python中,== None is None 都可以使用,但是 is None 速度很快。

 

 

Now that you are guaranteed to have a string, you can pass the string to processFunc, which you have already defined as a function that either does or doesn't collapse whitespace. Now you see why it was important to use str to convert a None value into a string representation. processFunc is assuming a string argument and calling its splitmethod, which would crash if you passed it None because None doesn't have a split method.

现在你可以肯定的获取到一个字符串,你将string传递给processFunc,该函数早已经定义,用来决定是否压缩空格。现在使用strNone值转换成字符串的重要性就看出来了。Processfunc被认为是一个字符然后调用该函数的split函数。如果你将None传递给该函数,该函数会崩溃,这是因为None函数没有split函数。

Stepping back even further, you see that you're using string formatting again to concatenate the return value of processFunc with the return value of method's ljust method. This is a new string method that you haven't seen before.

在多退回去一点,你发现你正在使用格式化字符串来连接函数processFuncmethodljust方法当前的返回值的返回值。这是一个新的字符串方法,前面没有遇到过。

Example 4.24. Introducing ljust

4.24 ljust使用说明

  1. >>> s = 'buildConnectionString'
  2. >>> s.ljust(30)
  3. 'buildConnectionString '
  4. >>> s.ljust(20)
  5. 'buildConnectionString'

1

ljust pads the string with spaces to the given length. This is what the info function uses to make two columns of output and line up all the doc strings in the second column.

Ljust使用空格来填补给定的长度。这就是info函数来确保输出的两列和所有的doc string变成一行在第二列。

2

If the given length is smaller than the length of the string, ljust will simply return the string unchanged. It never truncates the string.

如果给定的长度小于字符串的实际长度,ljust仅仅是简单的返回时间的字符串。它不对字符串进行截断。

You're almost finished. Given the padded method name from the ljust method and the (possibly collapsed) doc string from the call to processFunc, you concatenate the two and get a single string. Since you're mapping methodList, you end up with a list of strings. Using the join method of the string "\n", you join this list into a single string, with each element of the list on a separate line, and print the result.

整个语句基本解释完了。当你在映射完methodlist以后,获得一个字符串list,通过填补空格的方法ljust和压缩空格的方法processFunc,你可以将两个字符串连接成一个字符串。通过使用字符串’\n’join方法,你将一个list连接成一个字符串,list中的每一个元素单独成行,然后打印成结果。

Example 4.25. Printing a List

  1. >>> li = ['a', 'b', 'c']
  2. >>> print "\n".join(li)
  3. a
  4. b
  5. c

1

This is also a useful debugging trick when you're working with lists. And in Python, you're always working with lists.

在你调试list是,这是一个非常有用的调试技巧。在Python中,你一直在使用list

That's the last piece of the puzzle. You should now understand this code.

这是这个难题的最后一部分。现在你能够完全的理解这段代码。

   

  1. print "\n".join(["%s %s" %
  2.                       (method.ljust(spacing),
  3.                        processFunc(str(getattr(object, method).__doc__)))
  4.                      for method in methodList])

 

阅读(940) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~