Skip to content

Instantly share code, notes, and snippets.

View MOOOWOOO's full-sized avatar

Jux Liu MOOOWOOO

  • Chongqing, China
View GitHub Profile
@MOOOWOOO
MOOOWOOO / calculate.py
Last active March 11, 2016 08:05
分:秒.毫秒 的python计算方式
def calculate(last_time, current_time):
time_delta = \
datetime.datetime.strptime(current_time, '%M:%S.%f') -\
datetime.datetime.strptime(last_time, '%M:%S.%f')
return str(time_delta)[2:-4]
@MOOOWOOO
MOOOWOOO / config.ini
Last active March 12, 2016 08:43
python发邮件
[mail]
smtp_server = smtp.your-service.com
to_addr = target@email.com
from_addr = your@email.com
nickname = nick-name
password = your-password
prefix = subject-prefix
port = 25
@MOOOWOOO
MOOOWOOO / py-lineno_funcname
Last active March 12, 2016 08:44
python 打印当前行数和所在的函数名
import sys
def get_cur_info():
_ = sys._getframe()
return (_.f_code.co_name, _.f_back.f_lineno) # funcname, lineno
@MOOOWOOO
MOOOWOOO / getPathFileList.py
Last active March 12, 2016 09:24
遍历路径下的所有文件名
from os import listdir
from os.path import splitext, basename, isfile, isdir, join
def GetFileList(dir, fileList):
newDir = dir
if isfile(dir):
fileList.append(dir.decode('utf-8'))
elif isdir(dir):
for s in listdir(dir):
# 如果需要忽略某些文件夹,使用以下代码
@MOOOWOOO
MOOOWOOO / safeCopyFile.py
Created March 14, 2016 01:51
拷贝文件,不存在则跳过
from os.path import exists
if exists(r'/home/test.txt'):
shutil.copyfile(r'/home/test.txt', r'/home/root/')
else:
pass
@MOOOWOOO
MOOOWOOO / get_MAC_address.py
Created April 13, 2016 08:20
获取MAC地址
# -*- coding:utf-8 -*-
from uuid import getnode as get_mac, UUID
macaddr=UUID(int=get_mac()).hex[-12:].upper() # FFFFFFFFFFFF
return(":".join([macaddr[e:e+2] for e in range(0,11,2)]))
@MOOOWOOO
MOOOWOOO / urllib2-upload.py
Created April 13, 2016 10:16
urllib2上传文件
# -*- coding:utf-8 -*-
import mmap
import urllib2
def Upload(fname, url):
f = open(fname, 'rb')
mmapped_file_as_string = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
# Do the request
@MOOOWOOO
MOOOWOOO / pycurl-upload.py
Last active April 13, 2016 10:16
pycurl 上传文件
# -*- coding:utf-8 -*-
import pycurl
def Upload(file_name, url):
pc = pycurl.Curl()
pc.setopt(pycurl.URL, url) # URL
pc.setopt(pycurl.HTTPPOST,
[('logfile', (pc.FORM_FILE, file_name,))]) # {'logfile': xxx}
pc.perform()
@MOOOWOOO
MOOOWOOO / Decorator-Trace.py
Created April 19, 2016 16:46
程序跟踪装饰器,用于程序运行的时候打印出每一步的运行顺序和调用逻辑
import sys,os,linecache
def trace(f):
def globaltrace(frame, why, arg):
if why == "call":
return localtrace
return None
def localtrace(frame, why, arg):
if why == "line":
# record the file name and line number of every trace
filename = frame.f_code.co_filename
@MOOOWOOO
MOOOWOOO / Decorator-Timeout.py
Created April 19, 2016 16:50
超时检测装饰器,给任意可能会hang住的函数添加超时功能
import signal,functools
class TimeoutError(Exception):
pass #定义一个Exception,后面超时抛出
def timeout(seconds, error_message = 'Function call timed out'):
def decorated(func): def _handle_timeout(signum, frame):
raise TimeoutError(error_message)
def wrapper(*args, **kwargs):
signal.signal(signal.SIGALRM, _handle_timeout)