0%

Python-第三方工具

对第三方软件、工具进行操作或辅助的模块,如:

  • wxpy:微信的 Python 工具
  • itchat:微信的 Python 工具
  • mycli:带提示的 mysql 命令行工具
  • gitpython:与 Git 库交互的 Python 库。

概述

wxpy

wxpy:微信的 Python 工具

1
2
3
4
5
6
7
8
9
10
11
from  wxpy import Bot
bot=Bot(console_qr=2,cache_path="botoo.pkl")

# 填入你朋友的微信昵称,注意这里不是备注,也不是微信帐号
my_friend = bot.friends().search(u'苍茫游客')[0]
# 发送微信消息
my_friend.send(u"Helloworld")

# # 每隔 86400 秒(1 天),发送 1 次
# t = Timer(86400, auto_send)
# t.start()

itchat

itchat:微信的 Python 工具

1
2
3
4
5
6
7
8
9
import itchat
itchat.auto_login(hotReload=True)
# 根据好友昵称查找好友的信息,返回值是一个列表,有多个元素
res = itchat.search_friends('') # 备注名
#print(res)
# 通过索引获取该好友的详细信息
ly = res[0]['UserName']
while True: # 死循环发送
itchat.send('python',toUserName=ly)

mycli

mycli:带提示的 mysql 命令行工具

1
mycli -u root

GitPython

#最佳实践

gitpython:操作 Git 版本库的 Python 库。

1
2
3
4
pip install GitPython

# import git
import git
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

import os
import git


class GitRepository:
"""Git 仓库管理

Returns:
[type]: [description]
"""

def __init__(self, local_path, repo_url=None, branch='master'):
self.local_path = local_path
self.repo_url = repo_url
self.repo = None
self.initial(repo_url, branch)

def initial(self, repo_url, branch):
os.makedirs(self.local_path, exist_ok=True)
git_local_path = os.path.join(self.local_path, '.git')
if os.path.exists(git_local_path):
print('已存在git')
self.repo = git.Repo(self.local_path)
else:
self.repo = git.Repo.clone_from(repo_url, to_path=self.local_path, branch=branch)

def add(self, file_list):
self.repo.index.add(file_list)

def pull(self):
self.repo.git.pull()

def commit(self):
# 提交到版本库 git commit -m
# repo.index.commit("创建test文件")
commit_log = self.repo.git.log(
'--pretty={"commit":"%h","author":"%an","summary":"%s","date":"%cd"}',
max_count=50,
date='format:%Y-%m-%d %H:%M',
)
log_list = commit_log.split("\n")
return [eval(item) for item in log_list]

def get_now_branch(self):
# 获取当前所在分支
return self.repo.active_branch

def get_branch_list(self):
branches = self.repo.remote().refs
return [
item.remote_head
for item in branches
if item.remote_head
not in [
'HEAD',
]
]

def get_tag_list(self):
return [tag.name for tag in self.repo.tags]

def change_to_branch(self, branch):
self.repo.git.checkout(branch)

def change_to_commit(self, branch, commit):
self.change_to_branch(branch=branch)
self.repo.git.reset('--hard', commit)

def change_to_tag(self, tag):
self.repo.git.checkout(tag)


if __name__ == '__main__':
git_repo = GitRepository()
# 添加版本号
git_repo.repo.create_tag("v0.1")

# 创建分支 git branch dev
git_repo.repo.create_head("dev")

# 回退到某个版本 git reset --hard hash
git_repo.repo.index.reset(commit="哈希地址", head=True)

给博主来一杯卡布奇诺