Skip to content

Instantly share code, notes, and snippets.

# def next() for Python pre-2.6
class Throw(object): pass
throw = Throw() # easy sentinel hack
def next(iterator, default=throw):
"""next(iterator[, default])
Return the next item from the iterator. If default is given
and the iterator is exhausted, it is returned instead of
raising StopIteration.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# unzip-gbk.py
import os
import sys
import zipfile
print "Processing File " + sys.argv[1]
>>> class AAA(object):
... def go(self):
... self.one = 'hello'
...
>>> class BBB(object):
... def go(self):
... one = 'hello'
...
>>> a1 = AAA()
>>> a1.go()
from multiprocessing import Pool, cpu_count
from time import sleep
def f(x):
for i in range(10):
print 'counting %d --- Process:%d ' % (i, x)
sleep(1.0+float(x % cpu_count())/5)
def main():
@hahastudio
hahastudio / counting.py
Last active December 27, 2015 20:09
Spigot algorithm for digits pi & e
from itertools import count, imap
import time
import sys
def stream(lo, hi, f):
def approx((a,b,c), n): return (a * n + b) // c
def mul((a,b,c), (d,e,f)): return a * d, a * e + b * f, c * f
xs = ((n, a*d, d) for n, d, a in imap(f, count(1)))
z = 1, 0, 1
while True:
@hahastudio
hahastudio / ESWN.py
Created March 5, 2013 04:30
东南西北
#coding:utf-8
from random import choice
paper = [{'E': "天才", 'S': "笨蛋", 'W': "猪八戒", 'N': "大师兄"}, {'E': "Hentai", 'S': "绅士", 'W': "高富帅", 'N': "矮穷挫"}]
command = raw_input("Please input Direction and count, like: W25\n>>> ")
direction = command[0]
count = choice([0, 1]) + int(command[1:])
print paper[count % 2][direction]
from PyQt4 import QtCore
import glob
import os
import time
def all_files(pattern, search_path, pathsep=os.pathsep):
for path in search_path.split(pathsep):
for match in glob.glob(os.path.join(path, pattern)):
yield match
@hahastudio
hahastudio / TextEdit.py
Created December 20, 2012 13:49
A TextEdit editor that sends editingFinished events when the text was changed and focus is lost.
from PyQt4 import QtCore, QtGui
class TextEdit(QtGui.QTextEdit):
"""
A TextEdit editor that sends editingFinished events
when the text was changed and focus is lost.
"""
editingFinished = QtCore.pyqtSignal()
receivedFocus = QtCore.pyqtSignal()
#!/usr/bin/env python
import dpkt, socket
f = open('t.cap', 'rb')
pcap = dpkt.pcap.Reader(f)
c = 0
ptr_c = 0
log = open('4-seg-ip-dns-filter','w')
import dpkt, socket
f = open('t.cap', 'rb')
pcap = dpkt.pcap.Reader(f)
c = 0
for ts, buf in pcap:
# make sure we are dealing with IP traffic
# ref: http://www.iana.org/assignments/ethernet-numbers