Dive into python 实例学python (2) —— 自省,apihelper
apihelper.py
def info(object, spacing=10, collapse=1):
"""Print methods and doc strings. Takes module, class, list, dictionary, or string."""
methodList = [e for e in dir(object) if callable(getattr(object, e))]
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__
1、可选参数和命名参数
2、内置函数
type(obj)返回obj的数据类型
str(data)将数据强制转化为字符串
dir(obj)返回obj的属性和方法列表
callable(c)测试c是否可以调用
3、getattr()获取对象的引用
4、过滤列表
[mapping-expression for element in source-list if filter-expression]
5、and 和 or
and返回第一个假值,如果都为真,返回最后一个真值。
or返回第一个真值,如果都为假,返回最后一个假值。
>>> a = "first"
>>> b = "second"
>>> 1 and a or b 1
'first'
>>> 0 and a or b 2
'second'
类似于: bool ? a : b
安全使用:
>>> a = ""
>>> b = "second"
>>> (1 and [a] or [b])[0] 1
6、lambda表达式
测试代码
import unittest
import apihelper
import sys
from StringIO import StringIO class Redirector(unittest.TestCase):
def setUp(self):
self.savestdout = sys.stdout
self.redirect = StringIO()
sys.stdout = self.redirect def tearDown(self):
sys.stdout = self.savestdout class KnownValues(Redirector):
def testApiHelper(self):
"""info should return known result for apihelper"""
apihelper.info(apihelper)
self.redirect.seek(0)
self.assertEqual(self.redirect.read(),
"""info Print methods and doc strings. Takes module, class, list, dictionary, or string.
""") class ParamChecks(Redirector):
def testSpacing(self):
"""info should honor spacing argument"""
apihelper.info(apihelper, spacing=20)
self.redirect.seek(0)
self.assertEqual(self.redirect.read(),
"""info Print methods and doc strings. Takes module, class, list, dictionary, or string.
""") def testCollapse(self):
"""info should honor collapse argument"""
apihelper.info(apihelper, collapse=0)
self.redirect.seek(0)
self.assertEqual(self.redirect.read(),
"""info Print methods and doc strings. Takes module, class, list, dictionary, or string.
""")
Dive into python 实例学python (2) —— 自省,apihelper的更多相关文章
- Dive into python 实例学python (1) —— 函数和测试
odbchelper.py def buildConnectionString(params): """Build a connection string from a ...
- Python必学Django框架,入门到精通学习视频教程全都在这可以领
“人生苦短,我用python”,学python的小伙伴应该都了解这句话的含义.但是,学python,你真正了了解强大的Django框架吗!? 据说Django还是由吉普赛的一个吉他手的名字命名的呢,有 ...
- 通过游戏学python 3.6 第一季 第九章 实例项目 猜数字游戏--核心代码--猜测次数--随机函数和屏蔽错误代码--优化代码及注释--简单账号密码登陆--账号的注册查询和密码的找回修改--锁定账号--锁定次数--菜单功能'menufile
通过游戏学python 3.6 第一季 第九章 实例项目 猜数字游戏--核心代码--猜测次数--随机函数和屏蔽错误代码--优化代码及注释--简单账号密码登陆--账号的注册查询和密码的找回修改--锁 ...
- 通过游戏学python 3.6 第一季 第八章 实例项目 猜数字游戏--核心代码--猜测次数--随机函数和屏蔽错误代码--优化代码及注释--简单账号密码登陆--账号的注册查询和密码的找回修改--锁定账号--锁定次数
通过游戏学python 3.6 第一季 第八章 实例项目 猜数字游戏--核心代码--猜测次数--随机函数和屏蔽错误代码--优化代码及注释--简单账号密码登陆--账号的注册查询和密码的找回修改--锁定账 ...
- 通过游戏学python 3.6 第一季 第二章 实例项目 猜数字游戏--核心代码--猜测次数 可复制直接使用 娱乐 可封装 函数
猜数字游戏--核心代码--猜测次数 #猜数字--核心代码--猜测次数 number=33 amount=3 count=0 while count<=amount: conversion ...
- 通过游戏学python 3.6 第一季 第一章 实例项目 猜数字游戏--核心代码 可复制直接使用 娱乐 可封装 函数
本文实例讲述了python实现的简单猜数字游戏.分享给大家供大家参考.具体如下: 给定一个1-99之间的数,让用户猜数字,当用户猜错时会提示用户猜的数字是过大还是过小,知道用户猜对数字为止,猜对数字用 ...
- Python 中的反射和自省
本文主要介绍Python中的反射和自省,以及该机制的简单应用 熟悉Java的程序员,一定经常和Class.forName打交道.即使不是经常亲自调用这个方法,但是在很多框架中(spring,eclip ...
- 学Python后到底能干什么?
Python是一种什么语言? Python是一种计算机程序设计语言.你可能已经听说过很多种流行的编程语言,比如非常难学的C语言,非常流行的Java语言,适合初学者的Basic语言,适合网页编程的Jav ...
- Python 实例: 备份文件
都说生命苦短,我用python, 所以这两天我也开始学python了. 昨天搞了下语法,今天搞出来个实例,备份文件.尽管编码相当烂,但是测试了一下,还真能用. 它读取一个任务文件, 根据指定的任务参数 ...
随机推荐
- 详解在bash脚本中如何获取自身路径
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 这是stac ...
- P2661 信息传递 TODO-TARJAN算法
http://www.cnblogs.com/zbtrs/p/5762788.html http://blog.csdn.net/loi_yzs/article/details/52795093 都是 ...
- Flink Internals
https://cwiki.apache.org/confluence/display/FLINK/Flink+Internals Memory Management (Batch API) In ...
- x5设置经典门户登录
runtime\UIServer\index.jsp java.lang.String url = request.getContextPath() + "/portal2/proces ...
- (转)android 在电脑上显示真机屏幕
http://my.oschina.net/u/202293/blog/199954 方法一: 可以用360手机助手等实现. 方法二: 想把手机屏幕显示在电脑屏幕上时就需要使用Android Scre ...
- WSDL Style和use的组合方式说明
原文地址:http://www.ibm.com/developerworks/cn/webservices/ws-whichwsdl/#listing9 use属性可以为literal,encoded ...
- go安装windows源码
直接安装就好,下面是安装地址http://pan.baidu.com/s/1gdDFi9t
- IOS应用程序生命周期的AppDelegate详解
IOS 中的 AppDelegate.m/h 文件是很重要的呢,因为它是对 Application 的整个生命周期进行管理的. 先明白,每个iPhone应用程序都有一个UIApplication,UI ...
- [LeetCode] Letter Combinations of a Phone Number(bfs)
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 转:ASP.NET MVC利用TryUpdateModel来做资料更新 (一)
前言有使用 ASP.NET MVC 的朋友们一定多多少少有听过 TryUpdateModel,之前就看了很多有关它的文章,但在专案实务上都未曾实际使用过,而 TryUpdateModel 不仅能利用 ...