This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #coding=utf8 | |
| import itchat, time | |
| SINCERE_WISH = u'祝%s新年快乐!' | |
| REAL_SINCERE_WISH = u'祝%s新年快乐!!' | |
| def send_wishes(): | |
| friendList = itchat.get_friends(update=True)[1:] | |
| for friend in friendList: | |
| # 如果不是演示目的,把下面的方法改为itchat.send即可 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import time | |
| import string | |
| import pickle | |
| from operator import itemgetter | |
| from nltk.corpus import stopwords as sw | |
| from nltk.corpus import wordnet as wn | |
| from nltk import wordpunct_tokenize |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #http://geekgirl.io/concurrent-http-requests-with-python3-and-asyncio/ | |
| Concurrent HTTP Requests with Python3 and asyncio | |
| My friend who is a data scientist had wipped up a script that made lots (over 27K) of queries to the Google Places API. The problem was that it was synchronous and thus took over 2.5hours to complete. | |
| Given that I'm currently attending Hacker School and get to spend all day working on any coding problems that interests me, I decided to go about trying to optimise it. | |
| I'm new to Python so had to do a bit of groundwork first to determine which course of action was best. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import asyncio | |
| from contextlib import closing | |
| import aiohttp | |
| async def download_file(session: aiohttp.ClientSession, url: str): | |
| async with session.get(url) as response: | |
| assert response.status == 200 | |
| # For large files use response.content.read(chunk_size) instead. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async def main(): | |
| coroutine1 = do_some_work(1) | |
| coroutine2 = do_some_work(2) | |
| coroutine3 = do_some_work(4) | |
| tasks = [ | |
| asyncio.ensure_future(coroutine1), | |
| asyncio.ensure_future(coroutine2), | |
| asyncio.ensure_future(coroutine3) | |
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import re | |
| import random | |
| import base64 | |
| from scrapy import log | |
| class RandomProxy(object): | |
| def __init__(self, settings): | |
| self.proxy_list = settings.get('PROXY_LIST') | |
| fin = open(self.proxy_list) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import random | |
| from scrapy.conf import settings | |
| class RandomUserAgentMiddleware(object): | |
| def process_request(self, request, spider): | |
| ua = random.choice(settings.get('USER_AGENT_LIST')) | |
| if ua: | |
| request.headers.setdefault('User-Agent', ua) | |
| class ProxyMiddleware(object): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from bokeh.io import output_notebook | |
| output_notebook() | |
| from bokeh.io import show, vplot | |
| from bokeh.models import ColumnDataSource, CustomJS | |
| from bokeh.models.layouts import HBox | |
| from bokeh.models.widgets import Button, DataTable, Select, Slider, TableColumn | |
| from bokeh.sampledata.periodic_table import elements |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import bokeh.embed | |
| import bokeh.io | |
| import bokeh.models | |
| import bokeh.models.widgets | |
| import bokeh.plotting | |
| import pandas as pd | |
| from pandas_datareader import wb | |
| bokeh.plotting.output_notebook() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import re | |
| from weakref import WeakKeyDictionary | |
| class AdvancedDescriptor(object): | |
| """ | |
| Base class for descriptors, is hard to understand but works for most cases. | |
| from https://www.youtube.com/watch?v=P92z7m-kZpc | |
| """ | |
| def __init__(self, name=None): |