zoukankan      html  css  js  c++  java
  • Dive into python学习笔记

    http://woodpecker.org.cn/diveintopython/index.html

    1.第一个程序odbchelper.py

     1 def buildConnectionString(params):
     2     """Build a connection string from a dictionary
     3     
     4     Returns string.
     5     """
     6     return ";".join(["%s=%s" % (k, v) for k, v in params.items()])
     7 
     8 if __name__ == "__main__":
     9     myParams = {"server":"mpilgrim", 
    10                 "database":"master", 
    11                 "uid":"sa", 
    12                 "pwd":"secret"
    13                 }
    14     print buildConnectionString(myParams)

    关键点理解:

    1.三重引号

    """Build a connection string from a dictionary
     
     Returns string.
     """
    常用于定义doc string,文档化python函数

    2.  ";".join(["%s=%s" % (k, v) for k, v in params.items()])

    params.items()表明params必然是directory,调用items()方法返回键值对元组列表,利用多重赋值取出键值(k,v)元组,然后调用字符串对象的join方法将这些键值对用;隔开,join方法将list中的元素连接成单个字符串,打印的结果也就不再是列表的形式;注:join只能用于元素师字符串的list

    运行结果为:server=mpilgrim;uid=sa;database=master;pwd=secret

    附:split与join正好相反,它是将一个字符串分割成多元素list

    >>>s.split(";") (1)

    ['server=mpilgrim', 'uid=sa', 'database=master', 'pwd=secret']

     3.if __name__ == "__main__":用于测试模块

    2.自省apihelper.py

     1 def info(object, spacing=10, collapse=1):
     2     """Print methods and doc strings.
     3 
     4     Takes module, class, list, dictionary, or string."""
     5     methodList = [e for e in dir(object) if callable(getattr(object, e))]
     6     processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s)
     7     print "
    ".join(["%s %s" %
     8                      (method.ljust(spacing),
     9                       processFunc(str(getattr(object, method).__doc__)))
    10                      for method in methodList])
    11 
    12 if __name__ == "__main__":
    13     print help.__doc__
  • 相关阅读:
    win10自带输入法突然变成了繁体
    Eclipse 包视图折叠
    Unknown column '字段名' in 'field list' 错误解决方案
    The method getContextPath() from the type HttpServletRequest
    Eclipse 设置新建文件默认编码为 utf-8 的方法
    Java 混淆器
    程序员,不能缺少的几张图
    北漂程序员,扬帆起航的地方
    数据是啥?数据都去哪儿了?
    7行代码搞定WEB服务
  • 原文地址:https://www.cnblogs.com/CoolRandy/p/3298897.html
Copyright © 2011-2022 走看看