Skip to content

Instantly share code, notes, and snippets.

# !/usr/bin/env python3
# coding: utf-8
import asyncio
import time
import fcntl
import os
import ctypes
import errno
""" Async file wrapper for asyncio.
kzinglzy /
Created November 3, 2014 12:46
chinese url with tornado
#!/usr/bin/env python
# coding:utf-8
from tornado.web import RequestHandler, Application
from tornado.ioloop import IOLoop
import urllib
class ChineseHandler(RequestHandler):
kzinglzy /
Created November 3, 2014 12:15
Session with Python
#!/usr/bin/env python
# coding:utf-8
from os import urandom
from base64 import urlsafe_b64decode, urlsafe_b64encode
import redis
import binascii
redis = redis.StrictRedis('localhost')
kzinglzy /
Created October 28, 2014 08:36
k-means & k-means ++
#!/usr/bin/env python
# coding:utf-8
from collections import defaultdict
from random import uniform
from math import sqrt
def point_avg(points):
""" 计算并返回给定点的中心值
class Node:
__slots__ = ('val', 'next')
def __init__(self, value=None, next=None):
self.val = value = next
def __str__(self):
kzinglzy /
Created June 28, 2014 05:36
Find anagram from dictionary with Python's coroutine
from collections import defaultdict
def find_anagram(input_data):
""" find all the anagram from a dictionary
Anagram: contruct by the same word but have a different order
e.g. 'abc' 'acb' 'bca' is anagram
this solution comes from <Programming Pearls> chapter 2:
kzinglzy /
Last active August 29, 2015 14:02
Simple Huxiu Spider
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin # python 3.x
from urlparse import urljoin # python 2.x
class Spider: