Skip to content

Instantly share code, notes, and snippets.

View guerbai's full-sized avatar
🎯
Focusing

guerbai

🎯
Focusing
View GitHub Profile
@guerbai
guerbai / commands.py
Created January 30, 2017 15:24
Python获得调用系统命令在命令行的输出.
import commands
def get_location(ip):
cmd = "curl http://www.ip.cn/{}".format(ip)
result = commands.getoutput(cmd)
location = result.split("\n")[-1].split(":")[-1]
return location
@guerbai
guerbai / json_zh_dump.py
Created January 30, 2017 15:28
Python输出格式化带中文的json文件.
# -*- coding:utf-8 -*-
import json
import codecs
a = { "name": "一个"}
b = json.dumps(a, indent=4)
print type(b)
f = codecs.open('test3.py', 'wb', encoding='utf-8')
f.write(b.decode("unicode_escape"))
@guerbai
guerbai / cloghandler_use.py
Last active January 31, 2017 04:21
达到一定大小自动切分的写log文件,运行一下便有效果.
#coding:utf-8
import logging
import logging.handlers
from cloghandler import ConcurrentRotatingFileHandler as RFHandler
logger=None
def getLogger(product):
global logger
if not logger:
logging.basicConfig()
logger = logging.getLogger(product)
@guerbai
guerbai / closure.js
Created February 19, 2017 05:57
一个闭包,多传一个参数.
function createComparisonFunciton(propertyName) {
return function(object1, object2) {
var value1 = object1[propertyName];
var value2 = object2[propertyName];
if (value1 < value2) {
return -1;
} else if (value1 > value2) {
return 1;
@guerbai
guerbai / compress_pic.py
Last active June 2, 2019 03:59
网上下载图片并压缩
def __get_proper_size_pic_from_url(url, uplimit, workbench=tempfile.mkdtemp(), pic_name=str(time.time()*1000000)):
pic_loc = os.path.join(workbench, pic_name+'.'+url.split('.')[-1])
r = requests.get(url, stream=True)
if r.status_code == 200:
with open(pic_loc, 'wb') as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)
else:
logger.warning('download pic failed with url {0}'.format(url))
return
@guerbai
guerbai / cut_text.py
Last active June 2, 2019 03:59
获取包括汉字在内的字符串长度
# -*- coding: utf-8 -*-
# 一个汉字算两个字符
def _chars_fit_baohong_limit(element):
element_len = 0
for index, char in enumerate(unicode(element)):
if element_len >= BAOHONG_TEXT_LENGTH_LIMIT:
index -= 2
break
if u'\u4e00' <= char <= u'\u9fff': # 是汉字.
element_len += 2
@guerbai
guerbai / kindlehelper.py
Last active June 2, 2019 04:00
kindle标注行按书名整理
# -*- coding:utf-8 -*-
from collections import defaultdict
shinewords = open('cli.txt','r').read().decode('utf-8')
section = shinewords.strip().split("==========\r\n")
books = defaultdict(lambda : "")
for i in section:
try:
i = i.split('\n')
books[i[0]] += i[3]+'\n\n'
@guerbai
guerbai / suite_use.py
Last active June 2, 2019 04:00
python测试代码组织
import unittest
# 1situation.
def emailsuite():
suite = unittest.TestSuite()
tests = [
'test_normal_sendemail',
'test_no_dealnocb_work'
]
return unittest.TestSuite(map(testemail.EmailTestCase, tests))
# 2situation.
@guerbai
guerbai / jincheng.py
Last active June 2, 2019 04:01
python并发几种方式的演示
# -*- coding: utf-8 -*-
# import os
# print 'Process (%s) start...' % os.getpid()
# pid = os.fork()
# if pid==0:
# print 'I am child process (%s) and my parent is %s.' % (os.getpid(), os.getppid())
# else:
# print 'I (%s) just created a child process (%s).' % (os.getpid(), pid)
@guerbai
guerbai / gmailuse.py
Last active June 2, 2019 04:02
连接gmail
import imaplib
def open_connection(verbose=False):
connection = imaplib.IMAP4_SSL('imap.gmail.com', 993)
# username = gmail["username"]
# password = gmail["password"]
username = 'silverdismond@gmail.com'
password = 'qwer`123'
connection.login(username, password)
return connection