Skip to content

Instantly share code, notes, and snippets.

from gevent import monkey
monkey.patch_all()
import requests
import gevent
import traceback
from gevent.pool import Pool
def imap(urls, size, break_time=None):
@yejianye
yejianye / engineer-culture-at-glow
Last active August 29, 2015 14:21
Engieer Culture at Glow
# Engineer Culture at Glow
---
## Should we really care about culture at startups?
Good culture makes a company more efficient and scalable
- Lower communication cost
- Helps individual development, especially for junior members
- Makes the team unique
@yejianye
yejianye / community-json
Last active August 29, 2015 14:09
Description of community data in JSON format
[
{
"id": 58379, # 主题的ID
"title": "Welcome to Glow!", # 主题的标题
"content": "Tell us a little bit about yourself. Where are you from? How old are you?", # 主题的内容
"tag": "Glow Support", # 主题的标签
"views": 9993, # 主题的浏览次数
"time_created": 1415604639 # 主题的创建时间(unix时间戳)
"author": { # 主题的作者信息
"id": 72057594048471049,
@yejianye
yejianye / shell_commands.py
Last active August 29, 2015 14:02
Auto-evaluate variables in a string, especially useful when writing a shell-scripts in Python
from fabric.api import local
def sh(command, *args, **kwargs):
frame = inspect.currentframe()
ctx = frame.f_back.f_globals.copy()
ctx.update(frame.f_back.f_locals)
command = command.format(**ctx)
del frame
return local(command, *args, **kwargs)
@yejianye
yejianye / upload_progress.py
Created June 19, 2014 05:18
Show progress bar when uploading a file
# -*- coding: utf-8 -*-
# ############################################################################
# This example demonstrates how to use the MultipartEncoderMonitor to create a
# progress bar using clint.
# ############################################################################
from clint.textui.progress import Bar as ProgressBar
from requests_toolbelt import MultipartEncoder, MultipartEncoderMonitor
@yejianye
yejianye / flask_response_mime_type.py
Created November 15, 2013 09:17
Set mime type of response object in Flask
@app.route('/download', methods=['GET'])
def download():
data = json.dumps({"example" : "json string"})
response = make_response(data)
response.headers['Content-Type'] = 'text/json'
response.headers['Content-Disposition'] = 'attachment; filename=example.json'
return response
@yejianye
yejianye / doubly_linked_list.py
Last active December 27, 2015 21:39
Doubly linked list
class DoublyLinkedList(object):
# Copied from Python standard Library Lib/collections.py
# The circular doubly linked list starts and ends with a sentinel element.
# The sentinel element never gets deleted (this simplifies the algorithm).
# Each link is stored as a list of length three: [PREV, NEXT, VAL].
def __init__(self):
self.__root = root = [] # sentinel node
root[:] = [root, root, None]
def append(self, val):
@yejianye
yejianye / lxml-cssselect
Created June 30, 2013 16:58
lxml cssselect example
import lxml
root = lxml.html.parse(filename).getroot()
# in case the file contains unicode characters
parser = lxml.html.HTMLParser(encoding='utf-8')
root = lxml.html.parse(filename, parser=parser).getroot()
# get matched elements using css selector
els = root.cssselect('div.shop-info div.info-name h2 a')
@yejianye
yejianye / iosasset.py
Created March 11, 2013 17:02
generate ios assets from retina version (image@2x.png -> image.png)
#!/usr/bin/env python
from PIL import Image
from argh import arg, dispatch_command
import os
def gen_assets(rootdir, force=False):
for root, dirs, files in os.walk(rootdir):
for d in dirs:
gen_thumb(os.path.join(root, d), force=force)
for f in files: