Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env python
# encoding: utf-8
import time
# Basic Decorator
class MyDecorator1(object):
def __init__(self, func):
self.func = func
#!/usr/bin/env python
# encoding: utf-8
import os
from selenium import webdriver
import requests
import lxml.html
from lxml.html import urljoin
import argparse
from multiprocessing import Pool
import log
#!/usr/bin/env python
# encoding: utf-8
def clear_bit():
x = 0xFF
# clear bit
x &= ~(0x01 << 1)
@countrymarmot
countrymarmot / pool.py
Created January 20, 2014 07:47
test multiprocesing.Pool
#!/usr/bin/env python
# encoding: utf-8
from multiprocessing import Pool
import time
def power(x):
return x**x
@countrymarmot
countrymarmot / stats.py
Created January 14, 2014 06:27
caculate Cp and Cpk using python numpy
#!/usr/bin/env python
# encoding: utf-8
import numpy as np
def Cp(mylist, usl, lsl):
arr = np.array(mylist)
arr = arr.ravel()
sigma = np.std(arr)
@countrymarmot
countrymarmot / gtrans.py
Created January 7, 2014 02:34
script to translate en to zh by google translate
#!/usr/bin/env python
# encoding: utf-8
import requests
import re
import argparse
rawstr = r"""\[(?P<result>"\w+?").+?\]"""
rx = re.compile(rawstr, re.UNICODE)
rawurl = "http://translate.google.com/translate_a/t?client=t&sl=en&tl=zh-CN&hl=zh-CN&sc=2&ie=UTF-8&oe=UTF-8&ssel=0&tsel=0&q="
@countrymarmot
countrymarmot / string_test.py
Created January 6, 2014 04:11
test the basic encypt method
def string_process(string, encrypt):
int_list = []
for s in string:
int_list.append(ord(s))
result = ''
for i in int_list:
if i >= ord('A') and i <= ord('z'):
if encrypt:
result += chr(i - 3)
@countrymarmot
countrymarmot / magic_test.py
Created December 31, 2013 06:51
magic method execution sequence
class Test(object):
def __init__(self):
print "hello"
def __enter__(self):
print "enter"
def __call__(self, msg):
print "call " + str(msg)
@countrymarmot
countrymarmot / timeout.py
Created December 26, 2013 05:10
decorator to calculate python function timeout and elapsed time.
import signal
import time
from functools import wraps
class TimeoutException(Exception):
pass
def timethis(func):
#
# Copyright (C) 2010-2012 Vinay Sajip. All rights reserved. Licensed under the new BSD license.
#
import ctypes
import logging
import os
class ColorizingStreamHandler(logging.StreamHandler):
# color names to indices
color_map = {