python模块与包

**模块就是一个个.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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值