diff --git a/Day01-15/code/.vscode/ipch/6adec43f2d8bef0d/mmap_address.bin b/Day01-15/code/.vscode/ipch/6adec43f2d8bef0d/mmap_address.bin deleted file mode 100644 index 71307aba69936426bbbcb4dbf2d50dfa1afc5d76..0000000000000000000000000000000000000000 Binary files a/Day01-15/code/.vscode/ipch/6adec43f2d8bef0d/mmap_address.bin and /dev/null differ diff --git a/Day01-15/code/.vscode/ipch/6adec43f2d8bef0d/vi.ipch b/Day01-15/code/.vscode/ipch/6adec43f2d8bef0d/vi.ipch deleted file mode 100644 index 92b7adab31ffbc2b02d35fd57c0e6c9be26f684b..0000000000000000000000000000000000000000 Binary files a/Day01-15/code/.vscode/ipch/6adec43f2d8bef0d/vi.ipch and /dev/null differ diff --git a/Day01-15/code/.vscode/settings.json b/Day01-15/code/.vscode/settings.json deleted file mode 100644 index 13be6695e0be3dc535ca55b7099e583b80a789b9..0000000000000000000000000000000000000000 --- a/Day01-15/code/.vscode/settings.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "python.pythonPath": "/Library/Frameworks/Python.framework/Versions/3.7/bin/python3", - "files.associations": { - "stdlib.h": "c" - } -} \ No newline at end of file diff --git a/Day01-15/code/Day05/perfect.py b/Day01-15/code/Day05/perfect.py index e798fde9e1bb1b598f6857e6dceb20eb67803eca..1144ce53fd8a9f9d1c0bc4fc8881fdfad799d124 100644 --- a/Day01-15/code/Day05/perfect.py +++ b/Day01-15/code/Day05/perfect.py @@ -10,7 +10,6 @@ Date: 2018-03-02 import time import math -start = time.clock() for num in range(1, 10000): sum = 0 for factor in range(1, int(math.sqrt(num)) + 1): @@ -20,7 +19,3 @@ for num in range(1, 10000): sum += num / factor if sum == num: print(num) -end = time.clock() -print("执行时间:", (end - start), "秒") - -# 通过比较上面两种不同的解决方案的执行时间 意识到优化程序的重要性 diff --git a/Day01-15/code/Day08/student.py b/Day01-15/code/Day08/student.py index 9a8d71e05f7f2a5cd4113017ec3b2103d11324be..845492d1718e9e88a1def7059bd26a2e99092567 100644 --- a/Day01-15/code/Day08/student.py +++ b/Day01-15/code/Day08/student.py @@ -28,7 +28,7 @@ class Student(object): if self.age < 18: print('%s只能观看《熊出没》.' % self.name) else: - print('%s正在观看岛国爱情动作片.' % self.name) + print('%s正在观看岛国大电影.' % self.name) def main(): diff --git a/Day16-20/code/root@39.100.102.28 b/Day16-20/code/root@39.100.102.28 deleted file mode 100644 index 315679e301ca0092817d72077d04b97aee4a7042..0000000000000000000000000000000000000000 --- a/Day16-20/code/root@39.100.102.28 +++ /dev/null @@ -1,50 +0,0 @@ -""" -多进程和进程池的使用 -多线程因为GIL的存在不能够发挥CPU的多核特性 -对于计算密集型任务应该考虑使用多进程 -time python3 example22.py -real 0m11.512s -user 0m39.319s -sys 0m0.169s -""" -import concurrent.futures -import math - -PRIMES = [ - 1116281, - 1297337, - 104395303, - 472882027, - 533000389, - 817504243, - 982451653, - 112272535095293, - 112582705942171, - 112272535095293, - 115280095190773, - 115797848077099, - 1099726899285419 -] * 5 - - -def is_prime(n): - """判断素数""" - if n % 2 == 0: - return False - - sqrt_n = int(math.floor(math.sqrt(n))) - for i in range(3, sqrt_n + 1, 2): - if n % i == 0: - return False - return True - - -def main(): - """主函数""" - with concurrent.futures.ProcessPoolExecutor() as executor: - for number, prime in zip(PRIMES, executor.map(is_prime, PRIMES)): - print('%d is prime: %s' % (number, prime)) - - -if __name__ == '__main__': - main() diff --git "a/\345\205\254\345\274\200\350\257\276/\346\226\207\346\241\243/res/chiling.jpg" "b/\345\205\254\345\274\200\350\257\276/\346\226\207\346\241\243/res/chiling.jpg" new file mode 100644 index 0000000000000000000000000000000000000000..dafd1f4faaf1a3bcef729dab546b15a218217e55 Binary files /dev/null and "b/\345\205\254\345\274\200\350\257\276/\346\226\207\346\241\243/res/chiling.jpg" differ diff --git "a/\345\205\254\345\274\200\350\257\276/\346\226\207\346\241\243/res/frame.jpg" "b/\345\205\254\345\274\200\350\257\276/\346\226\207\346\241\243/res/frame.jpg" new file mode 100644 index 0000000000000000000000000000000000000000..6a8a808372d3ecf8bbbba777d290c8b5d6c0791b Binary files /dev/null and "b/\345\205\254\345\274\200\350\257\276/\346\226\207\346\241\243/res/frame.jpg" differ diff --git "a/\345\205\254\345\274\200\350\257\276/\346\226\207\346\241\243/res/requests.png" "b/\345\205\254\345\274\200\350\257\276/\346\226\207\346\241\243/res/requests.png" new file mode 100644 index 0000000000000000000000000000000000000000..585f41ed53ca2a9883df95aa9e78880090e59c62 Binary files /dev/null and "b/\345\205\254\345\274\200\350\257\276/\346\226\207\346\241\243/res/requests.png" differ diff --git "a/\345\205\254\345\274\200\350\257\276/\346\226\207\346\241\243/\345\245\275\347\216\251\347\232\204Python.md" "b/\345\205\254\345\274\200\350\257\276/\346\226\207\346\241\243/\345\245\275\347\216\251\347\232\204Python.md" new file mode 100644 index 0000000000000000000000000000000000000000..36e84f5bb7b663f9c3180eae960c8958d2283e7c --- /dev/null +++ "b/\345\205\254\345\274\200\350\257\276/\346\226\207\346\241\243/\345\245\275\347\216\251\347\232\204Python.md" @@ -0,0 +1,229 @@ +## 好玩的Python + +因为下面的代码都非常简单,简单到直接使用Python的交互式环境就能完成。当然,官方Python自带的交互式环境比较难用,推荐大家使用ipython,可以使用下面的命令来安装ipython,安装成功后键入ipython命令就能进入交互式环境。 + +```Shell +pip install ipython +``` + +或 + +```Shell +pip3 install ipython +``` + +ipython最直观的优点: + +1. 可以用?或者??来获取帮助。 +2. 可以用!调用系统命令。 +3. 可以使用Tab键自动补全。 +4. 可以使用魔法指令,如:%timeit。 + +### 没有工具用代码也能P图 + +1. 安装pillow三方库。 + + PIL(Python Imaging Library)是Python平台事实上的图像处理标准库了。PIL功能非常强大,而API却非常简单易用。但是PIL仅支持到Python 2.7,而且很多年都没有人维护了,于是一群志愿者在PIL的基础上创建了兼容的版本,名字叫[Pillow](https://github.com/python-pillow/Pillow),除了支持Python 3.x还加入了很多有用且有趣的新特性。 + + ```Shell + pip install pillow + ``` + + 或 + + ```Shell + pip3 install pillow + ``` + +2. 加载图片。 + + ```Python + from PIL import Image + + chiling = Image.open('chiling.jpg') + chiling.show() + ``` + +3. 使用滤镜。 + + ```Shell + from PIL import ImageFilter + + chiling.filter(ImageFilter.EMBOSS).show() + chiling.filter(ImageFilter.CONTOUR).show() + ``` + +4. 图像剪裁和粘贴。 + + ```Python + rect = 220, 690, 265, 740 + watch = chiling.crop(rect) + watch.show() + blured_watch = watch.filter(ImageFilter.GaussianBlur(4)) + chiling.paste(blured_watch, (220, 690)) + chiling.show() + ``` + +5. 生成镜像。 + + ```Python + chiling2 = chiling.transpose(Image.FLIP_LEFT_RIGHT) + chiling2.show() + ``` + +6. 生成缩略图。 + + ```Python + width, height = chiling.size + width, height = int(width * 0.4), int(height * 0.4) + chiling.thumbnail((width, height)) + ``` + +7. 合成图片。 + + ```Python + frame = Image.open('frame.jpg') + frame.show() + frame.paste(chiling, (210, 150)) + frame.paste(chiling2, (522, 150)) + frame.show() + ``` + +上面的知识在[Python-100-Days](https://github.com/jackfrued/Python-100-Days)项目的[第15天]()中也有对应的内容。 + +### 向微信好友群发祝福视频 + +1. 安装itchat三方库。 + + [itchat]()是一个开源的微信个人号接口,使用Python调用微信从未如此简单。 + + ```Shell + pip install itchat + ``` + + 或 + + ```Shell + pip3 install itchat + ``` + +2. 登录微信。 + + ```Python + import itchat + + itchat.auto_login() + ``` + + > 说明:用自己的微信扫描屏幕上出现的二维码就完成了登录操作,登录之后才能获取自己的好友信息以及发送消息给自己的好友。 + +3. 查找自己的朋友。 + + ```Python + friends_list = itchat.get_friends(update=True) + print(len(friends_list)) + luohao = friends_list[0] + props = ['NickName', 'Signature', 'Sex'] + for prop in props: + print(luohao[prop]) + ``` + + > 说明:friends_list相当于是一个列表,列表中的第一个元素是自己。 + +4. 随机选出5个朋友,获得他们的用户名、昵称、签名。 + + ```Python + lucky_friends = random.sample(friends_list[1:], 5) + props = ['NickName', 'Signature', 'City'] + for friend in lucky_friends: + for prop in props: + print(friend[prop] or '没有此项信息') + print('-' * 80) + ``` + +5. 给朋友发送文字消息。 + + ```Python + itchat.send_msg('急需一个红包来拯救堕落的灵魂!!!', toUserName='@8e06606db03f0e28d0ff884083f727e6') + ``` + +6. 群发视频给幸运的朋友们。 + + ```Python + lucky_friends = random.sample(friends_list[1:], 5) + for friend in lucky_friends: + username = friend['UserName'] + itchat.send_video('/Users/Hao/Desktop/my_test_video.mp4', toUserName=username) + ``` + +利用itchat还能做很多事情,比如有好友给自己发了消息又撤回了,如果想查看这些被撤回的消息,itchat就可以做到(注册一个接收消息的钩子函数,请参考[CSDN上的一篇文章]());再比如,有时候我们想知道某个好友有没有把我们删除或者拉入黑名单,也可以利用itchat封装的群聊功能,非好友和黑名单用户不会被拉入群聊,通过创建群聊函数的返回值就可以判定你和指定的人之间的关系。 + +### 不用客户端查看热点新闻 + +1. 安装requests库。(点击常看[官方文档]()) + + ![](./res/requests.png) + + ```Shell + pip install requests + ``` + + 或 + + ```Shell + pip3 install requests + ``` + +2. 爬取新闻数据或者通过API接口获取新闻数据。 + + ```Python + import requests + + resp = requests.get('http://api.tianapi.com/allnews/?key=请使用自己申请的Key&col=7&num=50') + ``` + + > 说明:上面使用了天行数据提供的数据接口,需要的话可以自行去[天行数据]()的网站注册开通。 + +3. 使用反序列化将JSON字符串解析为字典并获取新闻列表。 + + ```Python + import json + + newslist = json.loads(resp.text)['newslist'] + ``` + +4. 对新闻列表进行循环遍历,找到感兴趣的新闻,例如:华为。 + + ```Python + for news in newslist: + title = news['title'] + url = news['url'] + if '华为' in title: + print(title) + print(url) + ``` + +5. 调用短信网关发送短信到手机上,告知关注的新闻标题并给出链接。 + + ```Python + import re + + pattern = re.compile(r'https*:\/\/[^\/]*\/(?P.*)') + matcher = pattern.match(url) + + if matcher: + url = matcher.group('url') + resp = requests.post( + url='http://sms-api.luosimao.com/v1/send.json', + auth=('api', 'key-请使用你自己申请的Key'), + data={ + 'mobile': '13548041193', + 'message': f'发现一条您可能感兴趣的新闻 - {title},详情点击https://news.china.com/{url} 查看。【Python小课】' + }, + timeout=10, + verify=False + ) + ``` + + > 说明:上面的代码使用了[螺丝帽]()的短信网关,利用短信网关发送短信是需要支付费用的,但是一般的平台都会提供免费测试的短信,但是发送短信必须遵守平台的规则,违规的短信是无法发送的。上面发短信时使用的短信模板(“发现一条您可能感兴趣的新闻 - ###,详情点击https://news.china.com/### 查看。”)和短信签名(“【Python小课】”)需要登录螺丝帽管理平台进行配置,如果不清楚如何配置,这些平台都是有客服的哟。 + diff --git "a/\345\205\254\345\274\200\350\257\276/\350\247\206\351\242\221/\350\247\206\351\242\221\351\223\276\346\216\245.txt" "b/\345\205\254\345\274\200\350\257\276/\350\247\206\351\242\221/\350\247\206\351\242\221\351\223\276\346\216\245.txt" new file mode 100644 index 0000000000000000000000000000000000000000..f9db120c31c2a03567a70b3e6d6f8e8664d01369 --- /dev/null +++ "b/\345\205\254\345\274\200\350\257\276/\350\247\206\351\242\221/\350\247\206\351\242\221\351\223\276\346\216\245.txt" @@ -0,0 +1 @@ +链接:https://pan.baidu.com/s/1YMFb-dv8pHAfeSPXQoDoHA 密码:obdu \ No newline at end of file