<<  1 ˌ 킯챉 3 ˌ ̹皂  >>

2 ˌ. ȍ˂

2.1.

ˌ˼ ˂ һ Ꭻ: ȍ. ̂, ݵ ˼ ̹, ȍ ˅ ݵ ﹚ ̹ , ׵ ۼ յ, ס굎 ፫ . , ﹚ ˂졒, в 빚 ﹚ ̉, 皲 ˜ 衒 ˅ ﹚ 禂 .

˟, ˅. ˼ ˧ 킯 ˅ . 뵎˼ 킯챉 ፲ Ҋ 篫. һ 򵌡 һ ۂ ; ˼ ˌ ݵ 굎 Տ .

Example 2.1. apihelper.py

, ˼ ꡜ ̴ ȍ ϵ ˅.(Windows, UNIX, Mac OS).

def help(object, spacing=10, collapse=1): 1 2 3
    """Print methods and doc strings.

    Takes module, class, list, dictionary, or string."""
    methodList = [method for method in dir(object) if callable(getattr(object, method))]
    processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s)
    print "\n".join(["%s %s" %
                      (method.ljust(spacing),
                       processFunc(str(getattr(object, method).__doc__)))
                     for method in methodList])

if __name__ == "__main__":                4 5
    print help.__doc__
1 ݵ˼ ˂ ﹚ 翫, help. ס˂ , ס˼ ˂ Ҽ ̑: object, spacing, collapse. ҫ ˞ Ҽ뵴, ס 廡 ȓ .
2 help ﹚ -˂ ը ը (doc string) 皫嵴 ס˼ ﹚˂ ˞ . ՠ ݱ޵ ˸ 즗; ﹚ 즗 ˤ ȍ .
3 ﹚ ˂ 򵌫 ӱ ˅.
4 "if __name__" Ӏ ˼ ˞ , ˤ ݵ ȍ վ , ռ ˅. , ˼ help﹚˂ doc string ̉傄.
5 if йը˼ '==' ˤ ȍ졒, 飫 .

help ﹚ , ˂ IDE ւ , ȍ . ס˼ ﹚ ˼ ޸ (ݵ ˼, ס˼ ﹚ 翫, ˼ ˼, ס˼ ޸ 翫) ̹ ̑ ﹚ ˂ doc string ̉傄.

Example 2.2. Sample usage of apihelper.py

>>> from apihelper import help
>>> li = []
>>> help(li)
append     L.append(object) -- append object to end
count      L.count(value) -> integer -- return number of occurrences of value
extend     L.extend(list) -- extend list by appending list elements
index      L.index(value) -> integer -- return index of first occurrence of value
insert     L.insert(index, object) -- insert object before index
pop        L.pop([index]) -> item -- remove and return item at index (default last)
remove     L.remove(value) -- remove first occurrence of value
reverse    L.reverse() -- reverse *IN PLACE*
sort       L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1

̉˼ ˅ е. - doc string˼ ˂ ̀翫, һ ȍ˼ collapse0 ۂ븇 ˅. ﹚˂ 10 ҄, ˼ spacing 빚 ۂ ̉ ˅.

Example 2.3. Advanced usage of apihelper.py

>>> import odbchelper
>>> help(odbchelper)
buildConnectionString Build a connection string from a dictionary Returns string.
>>> help(odbchelper, 30)
buildConnectionString          Build a connection string from a dictionary Returns string.
>>> help(odbchelper, 30, 0)
buildConnectionString          Build a connection string from a dictionary

    Returns string.

2.2. ˞ ˅ 빚

˼ ﹚ 빚 皵 ; ﹚ 빚 翫, 빚 ˂ ̑. , 빚˼ ˅ 빚 ȍ븇 Ц ۵ ˅. SQL ՚˂ Transact/SQL ˜ˌ 稵˼ ˅; SQL՚˂ Ү ˟ը, ˼ ˌ ǂ ˅.

Example 2.4. help, a function with two optional arguments

def help(object, spacing=10, collapse=1):

spacing collapse ˞, Ҁ ס굎˼ ˂ 皱 ը. object 湚˞, Ҁ ס˼ ȡ 汉 ը. help һ˂ 빚 ̉, spacing ˼ 10 ȡ 졒 collapse 1 ȡ . help ˂ 빚 ̉, collapse ˟ 1 ȡ .

collapse һ˂ ۂ챉 spacing ˤ ˼ ޲ . ˂ ݲ, ˼ 񂈂 , Ҁ ˼ ț ˂ 빚 ﹚ ̉ ը. һ Ы, 빚 Ц, ۵ ˅.

Example 2.5. Valid calls of help

help(odbchelper)                    1
help(odbchelper, 12)                2
help(odbchelper, collapse=0)        3
help(spacing=15, object=odbchelper) 4
1 ˂ 빚, spacing˼ 10 collapse ȡ 1 翫.
2 ˂ 빚, collapse ȡ 1 翫.
3 ˼ collapse빚 ˞ 졒 ˂ ۂ. spacing˼ ˟ ˂ ȡ 10 翫.
4 皲 湚˞ 빚ֵ (object ˼, ס˼ ) 皲 ˅, ˅ 빚 Ц һ ˅.

빚 ȍ˟ ˟皫 ˼ ȗ . 빚 ﹚ ̉ “ȗ˞” վ˼ ̅ ˼ ﹚ ۵ 빚˂ ̧ ȫ. ˂ ̡, ˼ “ȗ˞” վ ﹚ ̉ , һ ˼ ˞ ¹ ȗ 翫.

Note
﹚ ̉챉 ˤ 삄 ˼ (ȡ е) 빚 ۂ ; վ Ы ˅.

ܲ

2.3. type, str, dir, ˌ ﹚

ˌ ﹚˂ ˼ ˅. ݵ ﹚˼ 僡Ҳ稹 ݵ «. ˼ ˂˞ 嵴, ݲ˂ (, 煲 ֡ ˼) Ү ݲ 남皫 ᡒ .

type ﹚ ݵ ˂˞ ̹˂ ՠ. ˼ types ݵ һе. ˼ (Ϯ) ﹚ ˂ ˅.

Example 2.6. Introducing type

>>> type(1)           1
<type 'int'>
>>> li = []
>>> type(li)          2
<type 'list'>
>>> import odbchelper
>>> type(odbchelper)  3
<type 'module'>
>>> import types      4
>>> type(odbchelper) == types.ModuleType
1
1 type ˼ ̑ ס˂ ՠ. ȡ ˼ ݵ . ۹(integers), ը(strings), (lists), ȍ˟(dictionaries), (tuples), ﹚(functions), (classes), ݵ(modules), 皲 (types) ݵ .
2 type ˼ ̑ ˂ ՠ ˅.
3 type ˼ ݵ .
4 ˼ typesݵ˂ ȗ ȍ ̹˂ ҂ ˅. help ﹚ , ס 廡 ȓ .

str ˼ ը ́ӫ. ݵ ˼ ը ϼ ˅.

Example 2.7. Introducing str

>>> str(1)          1
'1'
>>> horsemen = ['war', 'pestilence', 'famine']
>>> horsemen.append('Powerbuilder')
>>> str(horsemen)   2
"['war', 'pestilence', 'famine', 'Powerbuilder']"
>>> str(odbchelper) 3
"<module 'odbchelper' from 'c:\\docbook\\dip\\py\\odbchelper.py'>"
>>> str(None)       4
'None'
1 ۹ ˼ , ˼ str ȗ , Ҁ ˂ ݵ ݲ˼ ۹ ը 煫 ﹚ 皡 ˅ ը.
2 , str ˼ ˂ . ס˼ 禡禡 ҂ .
3 str ˼ ݵ . ˼ ݵ˂ ը ݸ˂ ݵ˂ ւ졒, ˂ ˼ .
4 str˂ ς ˤ ס ˂ null , None . ס˼ ը 'None' ՠ. help﹚ , ס 廡 ȓ .

˂ help ﹚˂ ˌ 傄 dir﹚ ˅. dir ﹚ ̹ ޸ ˂ ՠ: ݵ(modules), ﹚(functions), ը(strings), (lists), ȍ˟(dictionaries)... ˼ .

Example 2.8. Introducing dir

>>> li = []
>>> dir(li)           1
['append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>> d = {}
>>> dir(d)            2
['clear', 'copy', 'get', 'has_key', 'items', 'keys', 'setdefault', 'update', 'values']
>>> import odbchelper
>>> dir(odbchelper)   3
['__builtins__', '__doc__', '__file__', '__name__', 'buildConnectionString']
1 li , dir(li) ˂ ݵ ޸ ՠ. ˼ ՠ ޸˂ , ޸ ̹ 즗, ը ւ .
2 d ȍ˟, dir(d) ˼ ȍ˟ ޸˂ ՠ. ˞ 굎 һ, (keys), ւ .
3 ˍ˅ 皫 . odbchelper ݵ, dir(odbchelper) , ˌ  ւ, __name__ __doc__, ˂ ݵ  ޸ ˼, ݵ ˂ ݵ ˂ 굎 һ ՠ. , odbchelper һ˂ ȍ-˂ ޸, buildConnectionString ﹚ 皫嵴 킯챉˂ ˌ 낄 ˅.

, ̉ ﹚ ̹ ̑ ̹ ̉, 1 ՠ졒 , 0 ՠ. ̉ ̹ ﹚(functions), (class) ޸(methods), 皲 򸁱皵 ւ. ( 3 ˌ Ꭻ.)

Example 2.9. Introducing callable

>>> import string
>>> string.punctuation           1
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
>>> string.join                  2
<function join at 00C55A7C>
>>> callable(string.punctuation) 3
0
>>> callable(string.join)        4
1
>>> print string.join.__doc__    5
join(list [,sep]) -> string

    Return a string composed of the words in list, with
    intervening occurrences of sep.  The default separator is a
    single space.

    (joinfields and join are synonymous)
1 string ݵ ˅ ﹚˼ (˼ ȍֵ ˟ join ﹚ ȍ 졒), һ ݵ˼ string.punctuation ˼ ˼ ﹚ ւ졒 ˅, ﹚ ݵ ˞ ը ւ.
2 string.join ը˂ ¡ 煫 ﹚.
3 string.punctuation ˼ ̉ ; ס˼ ը. (ը˼ ̉ ޸ 皡 ˅, һ ը ̹ ̉ .)
4 string.join˼ ̉; ﹚ ˂ 빚 皫 ﹚.
5 ̉ ̹ doc string ˅. ̉ (callable) ﹚ ̹˂  ȍ,  범 (methods, functions, classes) Ǹ̂챉 (constants, etc.) , ̹ 굵 ȍ˟ е, ۂ ˅ .

type, str, dir, ˂ ݵ һ ˌ ﹚˼ 皲 '__builtins__' ݵ «. (ס˼ ˂ կ ͡ 翫.) , ˼ ˸ ˞ 'from __builtins__ import *' ࡢ ˅, ס˼ ݵ “ˌ(built-in)” ﹚ 졒 ˼ ס굎 ˞ ȍ ˅.

վ ࡢ ˼ __builtins__ ݵ ۼ յ븇 ݵ ˌ ﹚  һ˂ 碱 ˅ . 貮̧ , ס ˤ һ˂ ﹚ 皡 ˅; ס˼ help . ̵ 皱 f ; ﹚ . (AttributeError ˼, ˌ ˼ ւ .)

Example 2.10. Built-in attributes and functions

>>> from apihelper import help
>>> help(__builtins__, 20)
ArithmeticError      Base class for arithmetic errors.
AssertionError       Assertion failed.
AttributeError       Attribute not found.
EOFError             Read beyond end of file.
EnvironmentError     Base class for I/O related errors.
Exception            Common base class for all exceptions.
FloatingPointError   Floating point operation failed.
IOError              I/O operation failed.

[...snip...]
Note
˫嵴, ס ˼ ˟ ۵ ϡ ݵ ݵ Տ . һ ˂ ݲ ˼ ݵ ȍ ˤ (˼ , ˼, ȗһ, MSDN) 禂졒 ˅ 򸁵 է, ˼ ̹-ը(self-documenting) ˅.

ܲ

2.4. getattr ̹˂ յ챉

˼ ﹚ ̹ . ˼ getattr ﹚ ȍ, ̱ е ﹚˂ յ ˅ .

Example 2.11. Introducing getattr

>>> li = ["Larry", "Curly"]
>>> li.pop                       1
<built-in method pop of list object at 010DF884>
>>> getattr(li, "pop")           2
<built-in method pop of list object at 010DF884>
>>> getattr(li, "append")("Moe") 3
>>> li
["Larry", "Curly", "Moe"]
>>> getattr({}, "clear")         4
<built-in method clear of dictionary object at 00F113D4>
>>> getattr((), "pop")           5
Traceback (innermost last):
  File "<interactive input>", line 1, in ?
AttributeError: 'tuple' object has no attribute 'pop'
1 ˼ ˂ pop޸ յ. ˼ pop޸ ̉ 즗 즗; ̉˼ li.pop() . ˼ ޸ .
2 pop ޸ ՠ, һ , ޸ getattr ﹚ ը 빚 ۵. getattr˼ Ҕ ۵ ˌ ﹚ ̹˂  ՠ. , ̹ , ˼ pop ޸.
3 һ 皡 ˧ , ̵ : getattr˂ ՠ ˼ ޸ , ˼ ס li.append("Moe") ˞ Ȃ ꡜ ̉ ˅. һ ˼ ﹚ ˞ ̉ ; ˼ ﹚˂ ը ۂ.
4 getattr˼ ȍ˟ .
5 ˞, ޸ , getattr˼ , getattr˼  皦 է́ .

getattr ˌ ˤ ˼ . ס˼ ݵ .

Example 2.12. getattr in apihelper.py

>>> import odbchelper
>>> odbchelper.buildConnectionString             1
<function buildConnectionString at 00D18DD4>
>>> getattr(odbchelper, "buildConnectionString") 2
<function buildConnectionString at 00D18DD4>
>>> object = odbchelper
>>> method = "buildConnectionString"
>>> getattr(object, method)                      3
<function buildConnectionString at 00D18DD4>
>>> type(getattr(object, method))                4
<type 'function'>
>>> import types
>>> type(getattr(object, method)) == types.FunctionType
1
>>> callable(getattr(object, method))            5
1
1 ˼ odbchelper ݵ ˅ buildConnectionString ﹚ ՠ, ס 킯챉 낧. ( 16 煹 ȁ ۂ ; ˂ ̉塇 .)
2 getattr ȍ, ˼ ﹚ ˼ յ ˅. ՠ˞, getattr(object, "attribute") object.attribute . ̹(object) ݵ, (attribute)˼ ݵ ˂ ˅: ﹚(a function), (class), ˼ ˟ (global variable).
3 help﹚ ȍ . ̹(object) 빚 ﹚ ҄翫; ޸(method) ը ޸ ˼ ﹚˂ .
4 , method ﹚˂ , ס ס˂ (type) յ븇 ˅.
5 ޸(method) ﹚Ղ, ס˼ ̉.

2.5. ȱ

̂, ˼, տ , ߫ 傄 皡 ˅. ˼ ހǫ瘡 ͵ ˅嵴, ˅ ˼ 皲 皡 굎˼ ˟ ǵ翫.

Example 2.13. List filtering syntax

[mapping-expression for element in source-list if filter-expression]

˼ 衒 ˅ 粮 տ˂ ˌ տ 3˂ 2 ; if , һ ˼ ȱ . ˼ ˼ ˅ (ס˼ ˂ ݵ ). ߱ ւ﵃ . ݵ ˼ Ǹ̵, ס굎˼ ߱ 埡 ̉ ւ﵂ .

Example 2.14. Introducing list filtering

>>> li = ["a", "mpilgrim", "foo", "b", "c", "b", "d", "d"]
>>> [elem for elem in li if len(elem) > 1]       1
['mpilgrim', 'foo']
>>> [elem for elem in li if elem != "b"]         2
['a', 'mpilgrim', 'foo', 'c', 'd', 'd']
>>> [elem for elem in li if li.count(elem) == 1] 3
['a', 'mpilgrim', 'foo', 'c']
1 ߱ ˼ (ס˼ ˂ ՠ), 秂. ˼ , ; , 皲稹 ߱ ˂ ՠ ւ. ˼ ݵ -ը ը ȡ ˅ , ס ը˂ ӡ翫.
2 ˼ , b ȫ . ˼ b˂ ݵ ̉ ȫ 뵴, Ҁ ס ̉ , ˼ ը .
3 count ޸ һ˂ һһ 鸹 ՠ. ˂ һ˂ ȍ ւ ՠ, ˼ һ˂ 秼 ϡ ࡢ 皵 . һ , Ҁ ˂ տ һһ ˼ ( , b d) ˟ ը. 秼 ϡ վൎ ˅ , һ ̴˼ .

Example 2.15. Filtering a list in apihelper.py

    methodList = [method for method in dir(object) if callable(getattr(object, method))]

˼ ˉ , ˼ ˉ, һ 禫 . ˟̹ ˼ һ˂ ՠ졒, methodList 񫍵. ˂ ˟ՠ ߱ . ߱ ˼ ; ס˼ ˂ ՠ. dir(object) ̹(object)˂  ޸˂ ՠ; ߡ ˅ . if˂ ț .

˼ ᮡ , һ . ˼ callable, getattr, in . ˟ , object ݵ e ݵ ˅ ﹚˂ , 'getattr(object, method)'˼ ﹚̹ ՠ.

˼ object 皲, һ˂ ̹ ̑, ̹˂ , ޸, ﹚ ݵ 굎˂ յ, Ȳ ݵ 굎 ϡ. , getattr﹚ , /޸/﹚˂ ̑ 굎 յ븇 ϡ . ̹ ̉ 肄, ס˼ ޸ ﹚, (˂ pop޸ ˼) ˌ ﹚ (odbchelper ݵ˂ buildConnectionString﹚ ˼) ȍ-˂﹚ ˅. ݵ ݵ ˌ ˅ __name__ ˼, ˼ ȗ .

ܲ

2.6. and or˂ 킄 ȹ

, and or ȗ Ғ , һ ׵˼ ՠ ; ס굎˼ ҂졒 ˅ ˂ һ ՠ.

Example 2.16. Introducing and

>>> 'a' and 'b'         1
'b'
>>> '' and 'b'          2
''
>>> 'a' and 'b' and 'c' 3
'c'
1 and ȍ , ˼ ը . '0, '', [], (), {}, None'˼ ݵ ը ; ݵ ˼ .[3] ݵ ը , and ՠ. , and 'a' , 졒 'b' , 'b' ՠ.
2 ը , and տ ՠ. , '' տ .
3 ݵ ˼ , and , 'c' ՠ.

Example 2.17. Introducing or

>>> 'a' or 'b'          1
'a'
>>> '' or 'b'           2
'b'
>>> '' or [] or {}      3
{}
>>> def sidefx():
...     print "in sidefx()"
...     return 1
>>> 'a' or sidefx()     4
'a'
1 or ȍ, ˼ ը and , . , or 畸 ՠ. , 'a' տ .
2 or˼ '', 졒, 'b', , 'b' ՠ.
3 ݵ , or ՠ. or '' 졒, [] , , {} , {} ՠ.
4 ˼ or˼ ը տ ̣ , һ Ǹ̂ . ˅ ˼ . , sidefx﹚ ૑ ̉ , Ҁ or 'a' 졒, 畸 'a' ՠ챉 ը.

C , ˼ Ӳ 'bool ? a : b' ւ , ס˼ bool , ab . and or վը, ˼ ˼ ޹ ˅.

Example 2.18. Introducing the and-or trick

>>> a = "first"
>>> b = "second"
>>> 1 and a or b 1
'first'
>>> 0 and a or b 2
'second'
1 ը˼ C ˂ 'bool ? a : b' . ˟̹ ˼ , and ˜ . 1 and 'first' 'first', 'first' or 'second' 'first' .
2 0 and 'first' 0 , 0 or 'second' 'second' .

, ˂ ˼ Ғ, ݲ˂ 禡 Ղ, ˂ and-or Ӏ C ˂ 'bool ? a : b' ը ȍ һ˂ , , ǵ ˍ. a˂ , ˼ ȗ . (˼ ȡ Ҳ 킯 ˅? տ ȗ?)

Example 2.19. When the and-or trick fails

>>> a = ""
>>> b = "second"
>>> 1 and a or b 1
'second'
1 a ըՂ, ס ˼ ը 煂Ղ, 1 and ''˼ '' , '' or 'second' 'second' . ! ס˼ .
Important
and-or Ӏ, bool and a or b, a ը C 'bool ? a : b' .

, and-or Ӏ ˂ Ӏ 騸 a˂ ૑ 浵 . ՠ˞ վ˼ a [a] ಔ ҅ b into [b] ಔ ҅, ՠ ˂ տ ̑ 뵴, ס˼ a b .

Example 2.20. Using the and-or trick safely

>>> a = ""
>>> b = "second"
>>> (1 and [a] or [b])[0] 1
''
1 [a] ˅ ˼ Ղ, ס˼ ૑ . a 0 '' ˼ 皦, [a] Ҁ ס˼ һ˂ 皱 ը.

皱, Ӏ ס 皫 鞵 皵 . ˼, , if йը ˼ ޹, ˉ ? ˸, ˼ , ˼ ˂ ȗ ȍ 졒, ˼ ը ȍ ˅ ۂ , Ҁ ˼ a һ ȗ 豉 ը. ˉ ˟ ȍ ˅ 皦, ̾܂ ˅; ֫(lambda) ﹚ , Ы if йը ˅.

ܲ

2.7. lambda﹚ ȍ챉

˼ ˍ˅ ը ϡ嵴 ˼ ס -﹚ ˂ ˅. , lambda ﹚ ﹚ һ ȍ ˅.

Example 2.21. Introducing lambda functions

>>> def f(x):
...     return x*2
...     
>>> f(3)
6
>>> g = lambda x: x*2  1
>>> g(3)
6
>>> (lambda x: x*2)(3) 2
6
1 ˼ lambda ﹚ ˤ˂ ȗ˞ ﹚ ˼ ޹. ǵ ը 즗: 빚 ˤ 飡 , return ˅ (ס˼ ¸˞, Ҁ ˟̹ ﹚ һ˂ ˅ ը). , ﹚ , һ ס˼ 񫍵 ̉ ˅.
2 ˼ 皲 lambda ﹚ 񫍂 桒 ס ȍ ˅. ȗ ˌ ﹚ , ס˼ ֫ - ﹚ 煡 ˅.

ՠЂ, lambda ﹚ ҹ˂ 빚 ̑ (˞ 빚 ւ) ˂ ՠ ﹚. lambda ﹚ ჲ ւ һ ȗ˂ ւ . ˼ lambda ﹚ ̈ ҅硒 ; ˉ , ȗ˞ ﹚ ˂ 󹼂쵵 즗.

Note
lambda ﹚ ˂ ը. ס굎 ȍ ૑˞ 湚˞ ˼ ; ס굎 ȍ ˅ һ, ˼ ˂ ȗ˞ ﹚ ˂졒 ȍ ˅. һ , ˍȍ 򵌵 Ђ졒 ס굎 ȍ ˼ ˼ ﹚ һ˂ .

Example 2.22. lambda functions in apihelper.py

    processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s)

҄煱 皡 ˅.
, and-or Ӏ ȍ, ס˼ ȗ , Ҁ lambda ը ȗ ը. (ס˼ lambda ﹚ ՠ ˼ . ﹚ ȗ ; ﹚˂ ՠ ˼ ˅.)

, split ﹚ 빚 ȍ졒 ˅. ˼ ס 1 ˼ 2˂ 빚 ȍ ˅, һ 빚 ס˼ Վ 僡 .

Example 2.23. split with no arguments

>>> s = "this   is\na\ttest"  1
>>> print s
this   is
a	test
>>> print s.split()           2
['this', 'is', 'a', 'test']
>>> print " ".join(s.split()) 3
'this is a test'
1 ˼ ը뵴, ȕ ̉ ը ˂. \n ; \t ը.
2 split Վ 僡. ˂ Վ, , ը ݵ .
3 ˼ ը 僡ҡ һ ס ˂ Վ ͂븇 Վ ˅. - ˂ doc string ˤ help﹚ .

helplambda ﹚, splits, and-or Ӏ 皡 Dz 졒 ˅ ?

Example 2.24. Assigning a function to a variable

    processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s)

processFunc ﹚, ﹚ collapse ˂ ˅. collapse , processFunc(string) Վ ˂ Վ ̈ ; , processFunc(string) ˼ ˂ 빚 ׫ ՠ .

琲 ֡ , ݲ , ˼ ﹚ һ 󹼂硒 , ﹚ ը һ һ˂ collapse 빚 ̑ if йը ȍ ˂ Վ ̈ ۂ졒, ˞ ՠ. ̾܂, Ҁ ﹚ ݵ ፲ ˅ ը; ס ̉ , ס˼ ˅ ˟ ˂ Վ ̈ ۂ ˅ . , ˼ ﹚ Ғ ˅ ˂ 騂 ( ס) 煵 ̧ ˍ ֫ ﹚ ˂ ˅. ˞, , , -ࡢ--빚-ՠ--ꡜ ˼ ˂ ȫ ˅.

ܲ

2.8. ݵ ̀

ֱ ܂ ˼ 삄 , ˂ 򵌫, ݵ . һ 皱 ˼ , Ҁ ݵ ˼ վ 篼 ˅ ը. ݵ ґ 禡˼ қ; ס굎 ҄ ̡.

Example 2.25. The meat of apihelper.py

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

˼ , ˂ ჲ 즗, һ ס˼ ¹ ը ȍ (“\”). Վ ȍ 桒 ˼ 僡Ҳ ˅ ȡ Ȃ ? տ˼ һ, Ҁ ˟̹ ˂ ˅ ը.

, ӬȲ ± . ˸˂

for method in methodList

տ ˅. ̂, methodList object 皫 ݵ ޸ ˅ . method 졒 ˅ .

Example 2.26. Getting a doc string dynamically

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

    Returns string.
1 help ﹚, object 졒 ˅ ̹, 빚 ҄翫.
2 methodList 졒 ˅ , method ˍ ޸˂ .
3 getattr ﹚ ȍ, object ݵ ˅ method ﹚ յ.
4 , ޸˂ doc string ̉ ˼ .

˂ ˸ 禡˼ doc string˂ ˜ str ȍ . 皵 , str ˌ﹚ ը ϼ. һ doc string˼ ȗ ը, ᱵ str ﹚ 圡 ? ˼ ݵ ﹚ doc string ˼ 첔, , ˂ __doc__ ˼ None .

Example 2.27. Why use str on a doc string?

>>> {}.keys.__doc__         1
>>> {}.keys.__doc__ == None 2
1
>>> str({}.keys.__doc__)    3
'None'
1 ȍ˟˂ keysdoc string , ˂ __doc__ ˼ None. ﵵ, __doc__  ˞ , ˂ IDE ǡ굵 啲 嵴, ס ࡢ ˅ , һ ˟ .
2 ˼ __doc__ ˂ None ˞ ҂븇 ˅.
3 str﹚ null ̑ ס˂ ը , 'None' ՠ.
Note
SQL, ˼ null ҂챉 ˤ = NULL IS NULL ȍ . , ը ; ˼ 坡 顡 == None ȍ.

騸 ը 皡 Ղ, ը processFunc ҄ ˅, ס ﹚ ˂ 嵴 ﹚ ˂ Վ ̈졁һ ˼ ̈ . ˼ None ը 챉 ˤ str ȍ 킯. processFunc ը 빚 ۂ졒 ˅ ˂ split ޸ ̉嵴, None ҄篫 ס ̾ . Ҁ None˼ split޸ 汉 ը.

禱 һ, ˼ ը ȍ methodljust ޸ ՠ processFunc˂ ՠ ¡졒 ˅ . ˼ ˟ ț ը ޸.

Example 2.28. Introducing the ljust method

>>> s = 'buildConnectionString'
>>> s.ljust(30) 1
'buildConnectionString         '
>>> s.ljust(20) 2
'buildConnectionString'
1 ljust ը 煲 ̧ Վ . help ﹚ ȍ ˂ ̉ տ ݵ doc string ဂ.
2 煲 ը˂ , ljust ը ׫ ՠ . ס˼ ը ˧ .

˂ ҵ. ljust ޸ Վ ޸˂ processFunc ̉ ( ˂ Վ ̈) doc string 煲翫, ¡ ˂ ը . methodList ߱ Ղ, ը˂ 翫. ը "\n"˂ join ޸ ȍ, ˂ ը ͂, ˂ ˂ , ̉傄.

Example 2.29. Printing a list

>>> li = ['a', 'b', 'c']
>>> print "\n".join(li) 1
a
b
c
1 ˼ ւ ՚ Ӏ. , ˼ ȗ ւ.

˂ 禡. 򵌫 ˟ 킯 .

Example 2.30. The meat of apihelper.py, revisited

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

2.9.

apihelper.py ᴡ ˂ ̉˼ ˟ 킯 .

Example 2.31. apihelper.py

def help(object, spacing=10, collapse=1):
    """Print methods and doc strings.

    Takes module, class, list, dictionary, or string."""
    methodList = [method for method in dir(object) if callable(getattr(object, method))]
    processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s)
    print "\n".join(["%s %s" %
                      (method.ljust(spacing),
                       processFunc(str(getattr(object, method).__doc__)))
                     for method in methodList])

if __name__ == "__main__":
    print help.__doc__

Example 2.32. Output of apihelper.py

>>> from apihelper import help
>>> li = []
>>> help(li)
append     L.append(object) -- append object to end
count      L.count(value) -> integer -- return number of occurrences of value
extend     L.extend(list) -- extend list by appending list elements
index      L.index(value) -> integer -- return index of first occurrence of value
insert     L.insert(index, object) -- insert object before index
pop        L.pop([index]) -> item -- remove and return item at index (default last)
remove     L.remove(value) -- remove first occurrence of value
reverse    L.reverse() -- reverse *IN PLACE*
sort       L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1

˸ ˌ ˟, ݵ 嵴 還 즗:



[3] ˸, ˂ ݵ . ˞, ˂ ̹ ը , һ ˼ ޸ ˂ ˂ ̹ ˅. ˼ ޸ ݵ 3 ˌ Տ .


<<  1 ˌ 킯챉 3 ˌ ̹皂  >>