1、help(模块或函数名) 可以显示帮助信息
2、import sys
sys.path --显示python搜索模块的路径
2.5、本地安装python,参考 http://blog.csdn.net/cighao/article/details/47860041
3、>>> dir() --显示所有已经导入的模块
['__builtins__', '__doc__', '__loader__', '__name__', '__package__']
>>> dir(__builtins__) --显示__builtins__模块的所有一级函数和对象
['ArithmeticError', 'AssertionError','dir','len',...
>>> dir(dir) --显示dir这个函数对象的属性
['__call__', '__class__', '__delattr__', '__dir__', '__doc__',
help(dir) 显示的是 dir.__doc__ 的内容
>>> help(dir)
Help on built-in function dir in module builtins:
dir(...)
dir([object]) -> list of strings
If called without an argument, return the names in the current scope.
Else, return an alphabetized list of names comprising (some of) the attributes
of the given object, and of attributes reachable from it.
If the object supplies a method named __dir__, it will be used; otherwise
the default dir() logic is used and returns:
for a module object: the module's attributes.
for a class object: its attributes, and recursively the attributes
of its bases.
for any other object: its attributes, its class's attributes, and
recursively the attributes of its class's base classes.
4、使用日志打印
其它知识点,第一行用来声明编码格式,#开头,包含coding= 和utf-8 即可。=右边可以有空格,左边不能有空格。不用=用:也可以。前面和后面,都可以乱敲字母。
# luanqi coding= utf-8 uuuu
import logging
log = logging.getLogger('fb')
log.setLevel(logging.DEBUG)
hdr=logging.StreamHandler()
formatter=logging.Formatter('[%(asctime)s]%(name)s:%(levelname)s:%(message)s')
hdr.setFormatter(formatter)
log.addHandler(hdr)
log.info("aa")
log.error(u"bb高峰")
log.debug(11)
log.info(__name__)
log.info(__file__)
log.info("hello,%s!"%"abc")
log.info("%d+%d=%d"%(3,5,3+5))
import logging
import logging.handlers
def hello():
formatstr = '%(asctime)s %(filename)s[line:%(lineno)d][%(funcName)s][%(levelname)s]%(message)s'
logging.basicConfig(level=logging.INFO,format=formatstr)
logging.debug("dd")
logging.info("hh")
logging.warning("hello")
logging.error("hello")
logging.fatal("%s,%s",'111','222')
logging.fatal("%(ab)s,%(cd)s", {'ab':'111', 'cd':'222'})
def h2():
s=logging.StreamHandler()
s.setFormatter(logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d]%(message)s'))
logging.getLogger().addHandler(s)
logging.info(logging.handlers.RotatingFileHandler)
hello()
5、断言
# coding=utf8
import nose
nose.tools.ok_(1==1,"hello")
nose.tools.eq_([1,2],[1,2],"mesg")
nose.tools.eq_({'a':'b'},{'a1':'b'},"ddd")
源码
def ok_(expr, msg=None):
"""Shorthand for assert. Saves 3 whole characters!
"""
if not expr:
raise AssertionError(msg)
def eq_(a, b, msg=None):
"""Shorthand for 'assert a == b, "%r != %r" % (a, b)
"""
if not a == b:
raise AssertionError(msg or "%r != %r" % (a, b))
# coding=utf8
import httplib2
# 获取HTTP对象
h = httplib2.Http(timeout=60)
h.timeout
# 发出同步请求,并获取内容
# resp, content = h.request("http://www.baidu.com")
# print resp
# print content
resp, content = h.request("http://www.baidu2464656.com")
print resp
print content
线程:t1.setDaemon(True)线程会随着主程序介绍而结束(即使线程还没有执行完)
如果不设置,则即使主程序结束,线程还会运行,直到线程结束,整个程序才会结束。
# coding=utf8
import threading
import time
def f1():
for i in range(10):
print i
time.sleep(0.3)
def f2():
for i in range(10):
print i + 100
time.sleep(1)
t1 = threading.Thread(target=f1)
t2 = threading.Thread(target=f2)
t1.setDaemon(True)
t2.setDaemon(True)
t1.start()
t2.start()
#
# t1.join()
# t2.join()
time.sleep(5)
print "ok"
mysql
1、下载驱动。在http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python 页面下载MySQL_python-1.2.5-cp27-none-win32.whl。然后到下载目录下,pip install MySQL_python-1.2.5-cp27-none-win32.whl。(64位的话,就要下载64位的版本)
有坑:如果直接用pip install mysql-python安装的话,就会提示“需要安装vc++编译器”,下载并安装(Download Microsoft Visual C++ Compiler for Python 2.7)之后,重启电脑,再次安装mysql-python,又提示_mysql.c(42) : fatal error C1083: Cannot open include file: 'config-win.h': No such file。这条路很难走通。
>>> import MySQLdb
>>> db=MySQLdb.connect("127.0.0.1","root","","test")
>>> con=db.cursor();
>>> con.execute("select * from t2")
2L
>>> print con.fetchone()[0] //获取第一行第0列
dddd/d/abc/
>>> print con.fetchone()[0] //获取下一行第0列
dddd
>>>db.close()
http://www.cnblogs.com/fnng/p/3565912.html