前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >利用碎片时间站在别人肩膀上学些python

利用碎片时间站在别人肩膀上学些python

作者头像
赵云龙龙
发布2019-07-11 10:17:10
4560
发布2019-07-11 10:17:10
举报
文章被收录于专栏:python爱好部落python爱好部落

如今,网上的资源很多,好东西少,免费的少,免费的好东西,少之又少。

如果你想利用碎片时间,学点技能。现实总是很残酷,网上的东西很少,都是抄来抄去的。很多公众号,不是广告就是软文,还有搞知识星球的,花点钱,也不一定买到好东西。

当付费都得不到好内容时,免费就更加不用说了。

我这里总结出了一些免费获取好东西的方法。 当然你得对技术有兴趣,有想法,对啃代码有热情。

第一个,就是站在巨人的肩膀上。当然我们可能不是站在巨人的肩膀,我们可以站在别人的肩膀。 我们可以读各种框架的源码,优秀的源码可以给我们启发。 比如这个: 这个是webdriver里面的wait until 和wait until not

代码语言:javascript
复制
    def until(self, method, message=''):
        """Calls the method provided with the driver as an argument until the \
        return value is not False."""
        screen = None
        stacktrace = None

        end_time = time.time() + self._timeout
        while True:
            try:
                value = method(self._driver)
                if value:
                    return value
            except self._ignored_exceptions as exc:
                screen = getattr(exc, 'screen', None)
                stacktrace = getattr(exc, 'stacktrace', None)
            time.sleep(self._poll)
            if time.time() > end_time:
                break
        raise TimeoutException(message, screen, stacktrace)

    def until_not(self, method, message=''):
        """Calls the method provided with the driver as an argument until the \
        return value is False."""
        end_time = time.time() + self._timeout
        while True:
            try:
                value = method(self._driver)
                if not value:
                    return value
            except self._ignored_exceptions:
                return True
            time.sleep(self._poll)
            if time.time() > end_time:
                break
        raise TimeoutException(message)

我们可以改造成自己的轮子,直接作为装饰器

代码语言:javascript
复制
import time

timeout = 3
poll =1


class TimeoutException(Exception):
    "this is user's Exception for timeout"
    def __init__(self,message,screen=None,staktrace=None):
        self.message = message
        self.screen = screen
        self.staktrace = staktrace
    def __str__(self):
        print(self.message)
        if self.screen:
            print(self.screen)
        if self.staktrace:
            print(self.staktrace)

def ignored_exceptions(Exception):
    pass


def wait_until(message=''):
    def warper(method):
        def until(*args, **kwargs):
            print(message)

            screen = None
            stacktrace = None

            end_time = time.time() + timeout
            while True:
                try:
                    value = method(*args, **kwargs)
                    if value:
                        return value
                except ignored_exceptions as exc:
                    screen = getattr(exc, 'screen', None)
                    stacktrace = getattr(exc, 'stacktrace', None)
                time.sleep(poll)
                if time.time() > end_time:
                    break
            raise TimeoutException(message, screen, stacktrace)
        return until
    return warper

def wait_until_not(message=''):
    def warper(method):
        def until_not(*args, **kwargs):

            end_time = time.time() + timeout
            while True:
                try:
                    value = method(*args, **kwargs)
                    if not value:
                        return value
                except ignored_exceptions:
                    return True
                time.sleep(poll)
                if time.time() > end_time:
                    break
            raise TimeoutException(message)
        return until_not

    return warper

@wait_until(message="do not kick out")
def add_some(a,b):
    print(a)
    c = a+b
    print(c)
    return c

if __name__ == "__main__":
    d = add_some(120,210)
    print(d)

这样,我们需要执行任何需要等待的操作,都可以套上这个。

站在别人的肩膀第二招: 大家都知道全球最大的同性交友网站,github, 上面有很多好的源码,我们直接可以捞来看。看看别人都在用各种语言做什么。 我们可以用一个爬虫,将某种语言别人提交的最新代码列表弄下来,看看哪个符合自己的口味。

我这里用很简单的代码,就实现了

代码语言:javascript
复制
import pandas as pd

git_url = "https://www.github.com/github/"

page_result = pd.read_html(git_url)[0]
# print(page_result)
# print(page_result["语言"])
# print(page_result.loc[page_result["语言"] == "Python"])
file_name = "code.txt"

#
import os
my_path = os.path.split(os.path.realpath(__file__))[0]
my_file = my_path + "/" + file_name

print(my_file)

python_result = page_result.loc[page_result["语言"] == "Python"]

# sort_values has already replace sort
python_result.sort_values(["Star"],ascending=True)
# print(python_result)
#axis=1(按列方向操作)、inplace=True(修改完数据,在原数据上保存)

#按标签来删除列

python_result.drop(['语言'],axis=1,inplace=True)
python_result['名称']= "https://github.com/" + python_result['名称']
python_result.to_csv("code.txt",sep="\t")
#
import yagmail
#链接邮箱服务器
yag = yagmail.SMTP( user="username@163.com", password="password", host='smtp.163.com',smtp_ssl=True)

# 邮箱正文
contents = ['python code for github']

# 发送邮件
yag.send(to='snake@qq.com', subject='Read the code', contents=contents,attachments =my_file)

这里不局限于python, 如果你兴趣广泛,这里可以搜索各种语言,按照star的从高到低排列。 可以发到QQ邮箱,用这个QQ登陆微信,就可以在微信中看到了。这没有直接发微信消息,因为微信消息接收文字有限,而且很容易排列乱,不方便阅读。

可以集成在CI里面,定时发送。 当有邮件时,会有提醒:

打开邮箱

这样就可以阅读了

如果觉得还可以,在电脑上就可以将代码下载下来,仔细研究了。

当然这样也可以下载好的书籍,资讯,这样就可以充分利用好碎片时间来学习了。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-05-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 python爱好部落 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档