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
kzinglzy / _.py
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
kzinglzy / session.py
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')
R_SESION = "SESSION:%s"
@kzinglzy
kzinglzy / k-means.py
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
self.next = next
def __str__(self):
@kzinglzy
kzinglzy / find_anagram.py
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
kzinglzy / HuxiuSpider.py
Last active August 29, 2015 14:02
Simple Huxiu Spider
#!/usr/bin/python3
import requests
from bs4 import BeautifulSoup
try:
from urllib.parse import urljoin # python 3.x
except:
from urlparse import urljoin # python 2.x
class Spider: