zoukankan      html  css  js  c++  java
  • 几个Python小程序

    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'         1
    'b'
    >>> '' and 'b' 2
    ''
    >>> 'a' and 'b' and 'c' 3
    'c'

    如果有假值,则返回第一个假值,否则返回最后一个真值

    or例子:

    >>> 'a' or 'b'          1
    'a'
    >>> '' or 'b' 2
    'b'
    >>> '' or [] or {}      3
    {}

    如果有真值,返回第一个真值,否则返回最后一个假值

    lambda例子:

    >>> 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


  • 相关阅读:
    FPGA--Cyclone中的时钟资源
    网易的翻译蛋
    程序员的健康问题,水桶腰
    如何像如何像 NASA 顶级程序员一样编程 — 10 条重要原则
    FOC_矢量控制相关资料
    BLDC 无刷电机FOC驱动 STM32官方培训资料
    ueditor插入百度音乐无法播放-403 问题
    phpstudy APACHE支持.htaccess以及 No input file specified解决方案
    linux数据库备份脚本
    阿里云centos 搭建SVN
  • 原文地址:https://www.cnblogs.com/macula7/p/1960422.html
Copyright © 2011-2022 走看看