Skip to content

Instantly share code, notes, and snippets.

View async_file.py
# !/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 Nov 3, 2014
chinese url with tornado
View _.py
#!/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 Nov 3, 2014
Session with Python
View session.py
#!/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 Oct 28, 2014
k-means & k-means ++
View k-means.py
#!/usr/bin/env python
# coding:utf-8
from collections import defaultdict
from random import uniform
from math import sqrt
def point_avg(points):
""" 计算并返回给定点的中心值
"""
View linkedlist.py
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 Jun 28, 2014
Find anagram from dictionary with Python's coroutine
View find_anagram.py
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 Aug 29, 2015
Simple Huxiu Spider
View HuxiuSpider.py
#!/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: