This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from compat import URLPattern, URLResolver | |
from django.core.urlresolvers import resolve, get_resolver | |
def find_url_pattern_by_name(name): | |
if name is None: | |
return None | |
def deep_find(rs): | |
for r in rs.url_patterns: | |
if isinstance(r, URLResolver): | |
result = deep_find(r) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib | |
import urlparse | |
def url_add_params(url, **params): | |
pr = urlparse.urlparse(url) | |
query = dict(urlparse.parse_qsl(pr.query)) | |
query.update(params) | |
prlist = list(pr) | |
prlist[4] = urllib.urlencode(query) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def filter_invalid_str(text): | |
""" | |
过滤非BMP字符 | |
""" | |
try: | |
# UCS-4 | |
highpoints = re.compile(u'[\U00010000-\U0010ffff]') | |
except re.error: | |
# UCS-2 | |
highpoints = re.compile(u'[\uD800-\uDBFF][\uDC00-\uDFFF]') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = '\u4e2d\u6587' | |
print a.decode('unicode-escape') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def git_shell(git_command): | |
try: | |
return os.popen(git_command).read().strip() | |
except: | |
return None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# 修改自标准库的copytree,可覆盖文件 | |
import os | |
import shutil | |
def copytree_override(src, dst, symlinks=False, ignore=None): | |
names = os.listdir(src) | |
if ignore is not None: | |
ignored_names = ignore(src, names) | |
else: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# 清除Windows记事本自动添加的BOM | |
def clean_bom(file_name): | |
with open(file_name, 'r+') as f: | |
content = f.read() | |
content = re.sub(r"\xfe\xff", "", content) | |
content = re.sub(r"\xff\xfe", "", content) | |
content = re.sub(r"\xef\xbb\xbf", "", content) | |
f.seek(0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import requests | |
from pyquery import PyQuery as pq | |
login_url = "https://passport.jd.com/new/login.aspx" | |
login_post_url = "http://passport.jd.com/uc/loginService" | |
# 用户名和密码 | |
username = "your_name" | |
password = "your_password" |