几个Python小程序
2010-06-25 04:48
316 查看
1:
def buildConnectionString(param):
"""
"""
return ";".join("%s=%s" %(k,v) for k,v in param.items())
if __name__=="__main__":
myParams={"server":"mpilgrim", \
"database":"master", \
"uid":"sa", \
"pwd":"secret" \
}
print buildConnectionString(myParams)
--------------------------
没什么好说的
2:
def info(object,spacing=10,collapse=1):
"""
list the api of the object
"""
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 info.__doc__
li=[]
info(li)
-----------------------------------------------------
methodList=[method for method in dir(object) if callable(getattr(object,method))]
注意句法, XX for in XXX if ***
processFunc=collapse and (lambda s:" ".join(s.split())) or (lambda s:s)
lambda表达式 and or
and例子
>>> 'a' and 'b'
'b'
>>> '' and 'b'
''
>>> 'a' and 'b' and 'c'
'c'
如果有假值,则返回第一个假值,否则返回最后一个真值
or例子:
>>> 'a' or 'b'
'a'
>>> '' or 'b'
'b'
>>> '' or [] or {}
{}
如果有真值,返回第一个真值,否则返回最后一个假值
lambda例子:
>>> def f(x):
... return x*2
...
>>> f(3)
6
>>> g = lambda x: x*2
>>> g(3)
6
>>> (lambda x: x*2)(3)
6
def buildConnectionString(param):
"""
"""
return ";".join("%s=%s" %(k,v) for k,v in param.items())
if __name__=="__main__":
myParams={"server":"mpilgrim", \
"database":"master", \
"uid":"sa", \
"pwd":"secret" \
}
print buildConnectionString(myParams)
--------------------------
没什么好说的
2:
def info(object,spacing=10,collapse=1):
"""
list the api of the object
"""
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 info.__doc__
li=[]
info(li)
-----------------------------------------------------
methodList=[method for method in dir(object) if callable(getattr(object,method))]
注意句法, XX for in XXX if ***
processFunc=collapse and (lambda s:" ".join(s.split())) or (lambda s:s)
lambda表达式 and or
and例子
>>> 'a' and 'b'
'b'
>>> '' and 'b'
''
>>> 'a' and 'b' and 'c'
'c'
如果有假值,则返回第一个假值,否则返回最后一个真值
or例子:
>>> 'a' or 'b'
'a'
>>> '' or 'b'
'b'
>>> '' or [] or {}
{}
如果有真值,返回第一个真值,否则返回最后一个假值
lambda例子:
>>> def f(x):
... return x*2
...
>>> f(3)
6
>>> g = lambda x: x*2
>>> g(3)
6
>>> (lambda x: x*2)(3)
6
相关文章推荐
- 写了几个有关operaminimod的python小程序
- Python的几个文件操作小程序
- python学习—几个简单小程序
- 树莓派用Python写几个简单程序
- python几个简单的入门程序
- 解决同时运行几个Python程序的问题
- 用python实现一个socket echo程序 && tcp socket的几个关闭状态
- Python的几个小程序
- python几个小程序
- 用python实现一个socket echo程序 && tcp socket的几个关闭状态
- python程序的输入输出(acm的几个小程序)
- 树莓派用Python写几个简单程序3_UART_BT-HM10
- Python应用-[用Python实现一个socket echo程序 && tcp socket的几个关闭状态]
- 树莓派用Python写几个简单程序4_UART
- 用python实现一个socket echo程序 && tcp socket的几个关闭状态
- [转]几个python 入门程序--例2( 如何将多个模块打包成一个模块)
- 树莓派用python写几个简单程序2_GPIO控制
- 树莓派用Python写几个简单程序3_i2c
- python程序的输入输出(acm的几个小程序)
- python学习—几个简单小程序