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 / 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)
@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 / 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 / count-timezone.js
Last active December 2, 2016 01:59
date function list of js.
function calcTime(city, offset) {
let d = new Date();
let utc = d.getTime() + (d.getTimezoneOffset() * 60000);
let nd = new Date(utc + (3600000 * offset));
let gmtTime = new Date(utc);
let day = nd.getDate();
let month = nd.getMonth();
let year = nd.getYear();
let hr = nd.getHours(); //+ offset
let min = nd.getMinutes();
@MOOOWOOO
MOOOWOOO / .zshrc
Last active May 6, 2023 15:02
Init a virtual machine
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH
setopt no_nomatch
export TERM="xterm-256color"
ZSH_TIME='24'
eval "$(thefuck --alias fuck)"
@MOOOWOOO
MOOOWOOO / decode('string-escape')
Created July 14, 2016 03:24
decode('string-escape')
decode('string-escape')
@MOOOWOOO
MOOOWOOO / ubuntu-terminal-maximize
Created July 18, 2016 02:31
ubuntu-terminal-maximize
sudo vim /usr/share/applications/gnome-terminal.desktop
Exec=gnome-terminal --maximize

格式字符串手册

数字格式化

下面的表格展示了使用Python的后起新秀str.format()格式化数字的多种方法,包含浮点数格式化与整数格式化示例。可使用 print("FORMAT".format(NUMBER)); 来运行示例,因此你可以运行: print("{:.2f}".format(3.1415926)); 来得到第一个示例的输出。

数字      	格式    	输出  	    描述
3.1415926 	{:.2f} 	  3.14  	    保留小数点后两位
3.1415926 {:+.2f} +3.14 带符号保留小数点后两位
@MOOOWOOO
MOOOWOOO / ArgumentParserInit.py
Last active April 10, 2017 08:26
new project use argument parser init
#!/usr/bin/env python
# coding: utf-8
import sys
from argparse import ArgumentParser
def main(*args):
pass