import getpass
# getpass.getpass()
import time
# time.sleep(2)
# time.time()
# time.ctime()
import math
# math.pow(2,3)
import os
# print(os.listdir('/var/log/'))
import random
import string
random.random()
random.randint(1,4)
random.choice('hello') # 从序列中拿出一个值出来
random.sample(string.ascii_letters + string.digits, 4) # 从序列中拿出指定个值出来
random.shuffle(list(range(1,10))) # 打乱顺序
import functools
functools.reduce(lambda x,y: x+y, range(10))
2.第三方模块
import itchat
import time
import random
itchat.auto_login(hotReload=True)
# while True:
# #1. 给手机助手发送消息
# itchat.send('hello', toUserName="filehelper")
# itchat.send_file('/etc/passwd', toUserName='filehelper')
# time.sleep(random.randint(1, 3))
# 2. 统计你的好友的男女比例
# friends是一个类似列表的数据类型, 其中第一个是自己的信息, 除了第一个之外是你的好友信息.
friends = itchat.get_friends()
info = {} # 'male':1, 'female':, 'other':
for friend in friends[1:]:
if friend['Sex'] == 1:
info['male'] = info.get('male', 0) + 1
elif friend['Sex'] == 2:
info['female'] = info.get('female', 0) + 1
else:
info['other'] = info.get('other', 0) + 1
print(info)
3.微信聊天机器人
import random
import itchat
import requests
import time
def get_tuling_response(_info):
print(_info)
# 图灵机器人的网址
api_url = "http://www.tuling123.com/openapi/api"
data = {
'key': '5ea0f11b5b6146239c52a47849387484',
'info': _info,
'userid':'wechat-robot'
}
# 发送数据到指定网址,获取网址返回的数据(字典数据类型)
res = requests.post(api_url, data).json()
# print(res, type(res))
# 给用户返回的内容
print(res['text'])
return res['text']
# get_tuling_response("给我讲个故事")
# get_tuling_response("不好笑")
# get_tuling_response("不好玩")
# 时刻监控好友发送的文本消息, 并且给予一个回复,
# isGroupChat=True接收群聊消息中的文本信息, 并让图灵机器人自动回复;
# isMapChat=True接收群聊消息中的文本信息, 并让图灵机器人自动回复;
@itchat.msg_register(itchat.content.TEXT, isFriendChat=True)
def text_reply(msg):
# 需求: 只对固定的群聊消息, 实现机器人聊天.
# 获取好友发送消息的内容
content = msg['Content']
# 将好友的消息发送给机器人处理, 处理结果就是返回给好友的消息
returnContent = get_tuling_response(content)
time.sleep(random.randint(1,10))
return returnContent
if __name__ == "__main__":
itchat.auto_login(hotReload=True)
itchat.run()
4.给指定好友发送消息
import itchat
import time
itchat.auto_login(hotReload=True)
# 根据好友的昵称查找好友的信息, 返回值为列表, 有多个元素;
res = itchat.search_friends("向杰")
# 通过索引获取该好友的详细信息(字典数据类型)
xiangJieInfo = res[0]['UserName']
while True:
itchat.send("hello", toUserName=xiangJieInfo)
time.sleep(3)
5.生成二维码
import qrcode
# img = qrcode.make("hello world")
img = qrcode.make("http://www.baidu.com")
img.save("hello.png")
6.模块的默认查找路径
import sys
# 模块的查找顺序: 内存中已经加载的模块 --> 内置模块 > sys.path目录里面的模块
# 内存中已经加载的模块 :
# sys.modules查看, python解释器启动式默认会加载的模块内容.
# 注意:
# 自定义,模块千万不要跟内置模块冲突.
print(sys.path)
7.模块讲解
import os
# 系统目录间的分隔符,
# Linux: /var/log/messages
# Windows: C:\\Project\hello.py
import itchat
print(os.path.sep)
# 在Linux里面执行shell命令
# 1. 第一种方式: 可以判断命令是否执行成功; 返回值为0, 执行成功, 返回值不为0, 执行失败
res = os.system('hostnam1e')
print("res:", res)
# 2. 第二种方式: 用来保存命令的执行结果
res = os.popen('hostname').read()
print("res:", res)
@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
# 需求: 当文件助手发送消息, 执行发送的内容,
# 1). 如果执行成功, 【显示执行成功】:执行结果
# 2). 反之, 显示之心失败 .
# print(msg)
if msg['ToUserName'] == 'filehelper':
# 获取要执行命令的内容
command = msg['Content']
# print(command)
# 让电脑执行命令代码
# 如果执行成功, 返回值为0
if os.system(command) == 0:
res = os.popen(command).read()
result = "【返回值】- 命令执行成功, 执行结果:\n" + res
itchat.send(result, 'filehelper')
# 命令执行失败, 请重新输入命令
else:
result = "【返回值】- 命令%s执行失败, 请重试" %(command)
itchat.send(result, 'filehelper')
if __name__ == "__main__":
itchat.auto_login(hotReload=True)
itchat.run()
8.模块于包的总结
一、模块导入
- 定义
Python 模块(Module),是一个 Python 文件,以 .py 结尾,包含了 Python 对象定义和Python语句。
模块让你能够有逻辑地组织你的 Python 代码段。
把相关的代码分配到一个模块里能让你的代码更好用,更易懂。
模块能定义函数,类和变量,模块里也能包含可执行的代码。
包括:内置模块,自定义模块,第三方模块;
作用
最大的好处是大大提高了代码的可维护性。其次,编写代码不必从零开始。
当一个模块编写完毕,就可以被其他地方引用。我们在编写程序的时候,也经常引用其他模块,
包括Python内置的模块和来自第三方的模块。使用模块还可以避免函数名和变量名冲突。
相同名字的函数和变量完全可以分别存在不同的模块中,
因此,我们自己在编写模块时,不必考虑名字会与其他模块冲突。
但是也要注意,尽量不要与内置函数名字冲突。
模块导入
import
import导入模块执行的操作:
- 产生一个新的名称空间;
- 在新建的名称空间俩面执行模块(.py)的内容
- 拿到了一个模块名, 执行模块文件产生的名称空间.
import …. as …
对于导入的模块重命名
from …. import ….
from …. import ….. as ….
从模块里面导入函数, 变量, 装饰器等…..
- 解决问题
问题: 导入模块时, 模块里面执行代码结果也会显示
解决方法:
name, 所在文件没有当作模块导入是, 结果为main;
name, 所在文件被当作模块导入是, 结果为模块的名称;
# 判断模块是否被导入, 如果没有被导入, 则执行下面的代码.
if __name__ == "__main__":
print("这是写好的模块里面的显示.......")
print(timeit(hello))
print(__name__)
all决定用来控制from xxx import *导入的内容
模块的分类
- 内置模块(random, string…..)
- 自定义模块(自己写的模块)
- 第三方模块(别人写好的模块)
如何安装第三方模块
- pip3 install 模块名
- 通过pycharm安装模块
模块的默认搜索路径