**模块就是一个个.py的python脚本
1.1##先写一个计算函数运行时间的程序module01,里面用到Timer装饰器
#coding:utf-8
import time
def Timer(fun):
def wrapper(*args, **kwargs):
start_time = time.time()
fun(*args, **kwargs)
end_time = time.time()
print "%s 函数运行时间为%s s" %(fun.__name__, end_time-start_time)
return wrapper
@Timer
def login():
time.sleep(0.1)
print "login......"
if __name__== "__main__": ##只执行主函数
print "模块名称为:%s" %(__name__)
login()
1.2##写第二个程序module02,里面也要用到Timer装饰器,为了方便,可将module01程序当作模块加入module02
#coding:utf-8
import time
import module01
@module01.Timer
def logout():
time.sleep(0.3)
print "logout......"
logout()
##import time #导入time模块
##import time as t #导入time模块并取别名为t
##from pack1 import module01 #从pack1包中导入模块module01,或从一个模块中导入某个装饰器
##如果要导入的模块与本程序在同一级(即同一个目录下)可直接用import x ,若不在同一级则要用fromy importx
2.0微信找朋友:
#coding:utf-8
import itchat
itchat.auto_login(hotReload=True)
friends_info = itchat.get_friends()[1:]
#print friends_info
for friend in friends_info:
city = friend['City']
print city
female = male = other = 0
for friend in friends_info:
sex = friend['Sex']
if sex == 1:
male += 1
elif sex == 2:
female += 1
else:
other += 1
print "我的好友总数为:%d" %(len(friends_info))
print "男性好友:%d" %(male)
print "女性好友:%d" %(female)
print "其他好友:%d" %(other)