Skip to content

Instantly share code, notes, and snippets.

@ficapy
ficapy / func_defaylt_values.py
Created Jul 31, 2015
函数参数不应该使用可变变量,只是也不是完全没有作用,比如可以缓存结果啊Orz,把以前写的一个函数改写了下 直观了一些
View func_defaylt_values.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Ficapy
# Create: '15/7/31'
# https://docs.python.org/3/faq/programming.html#why-are-default-values-shared-between-objects
import requests
import time
@ficapy
ficapy / check_status.py
Last active Aug 29, 2015
多线程请求
View check_status.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Ficapy
# Create: '15/8/6'
import time
import math
import requests
from concurrent.futures import ThreadPoolExecutor, as_completed
from xlrd import open_workbook
@ficapy
ficapy / regex_whole_file.py
Created Aug 20, 2015
不进行迭代文件使用正则匹配
View regex_whole_file.py
import codecs
import re
import mmap
with codecs.open('a.txt', 'r+', 'utf-8') as f:
data = mmap.mmap(f.fileno(), 0)
print(re.findall(b'AccountType\=(.*)', data))
@ficapy
ficapy / adding_method.py
Created Aug 22, 2015
adding a method to an exists object&Monkey Path
View adding_method.py
class A(object):
pass
a = A()
# 给类添加方法
def baz(self):
print('baz')
A.baz = baz
# ==========或者
View json_output_chinese.py
# http://stackoverflow.com/questions/18337407/saving-utf-8-texts-in-json-dumps-as-utf8-not-as-u-escape-sequence
print(json.dumps({u'卧槽': u'卧槽'}, indent=4).decode('unicode-escape').encode('utf8'))
print(json.dumps({u'卧槽': u'卧槽'}, indent=4, ensure_ascii=False))
View chardetect.py
total = 2292627
import sys
import codecs
from encodings.aliases import aliases
import mmap
all_encoding = aliases.values()
all_encoding = list(set(all_encoding))
@ficapy
ficapy / test.py
Last active Sep 16, 2015
将装饰器改成with语句,使用with语句实现错误重试
View test.py
import random
from tt import retrys
def g():
pass
class A():
def __init__(self):
View traverse_id.py
from concurrent.futures import ThreadPoolExecutor, as_completed
from user_agent import generate_user_agent
import requests
from pyquery import PyQuery as pq
def singal_parse(id):
url = 'http://www.miaoss.net/reg.php?id={}'.format(id)
ret = requests.get(url, params={'User-Agent': generate_user_agent()}, timeout=30)
ret.encoding = 'utf-8'
View ubuntu_install_lxml
# ubuntu install lxml requires
sudo apt-get install libxml2-dev libxslt-dev python-dev lib32z1-dev
# 在digitalocean 512M内存机器上报错,升级到1G内存正常
View random_file.txt
OSX:
http://osxdaily.com/2013/05/31/create-large-file-mac-os-x/
mkfile -n size[b|k|m|g] filename
Others:
http://www.skorks.com/2010/03/how-to-quickly-generate-a-large-file-on-the-command-line-with-linux/
dd if=/dev/zero of=somefile bs=1 seek=1G count=0