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 / CustomField.py
Created June 13, 2016 03:33
button field for Jinja2
#!/usr/bin/env python
# coding: utf-8
'''
定义可以将按钮(button)用于 Flask 表单(form)的域(field)
提供 button式 和 input type='button'式 两种
'''
from werkzeug.utils import escape, text_type
from wtforms import BooleanField, Field, Label
from wtforms.widgets.core import HTMLString, TextInput, Input, html_params
@MOOOWOOO
MOOOWOOO / loading.css
Created June 3, 2016 03:34
LoadingAnimation
* {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
html {
height: 100%;
}
@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)
@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 / 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 / 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 / pip2-upgrade.py
Last active October 7, 2018 15:19
pip-upgrade
#!/usr/bin/python
# -*- coding:utf-8 -*-
import pip
from subprocess import call
for dist in pip.get_installed_distributions():
call("pip2 install --upgrade {}".format(dist.project_name), shell=True)
@MOOOWOOO
MOOOWOOO / AccessDB.py
Created March 18, 2016 03:11
Python连接Access数据库或读取mdb文件内容
# -*- coding:utf-8 -*-
"""
"""
import pypyodbc
__author__ = "Jux.Liu"
# pypyodbc.lowercase = False
db_file = r'D:\att2000.mdb'
@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