A "Best of the Best Practices" (BOBP) guide to developing in Python.
- "Build tools for others that you want to be built for you." - Kenneth Reitz
- "Simplicity is alway better than functionality." - Pieter Hintjens
# coding: utf-8 | |
import asyncio | |
g = {'num': 1} | |
async def add(g, num=1): | |
print(f'origin {num} from', g['num']) | |
await asyncio.sleep(10) | |
print(f'do add {num} from', g['num']) |
# timer.py | |
import time | |
# Decorator function that prints the time that a function takes to run. | |
def timedFunction(func): | |
# The wrapper function will take whatever arguments the original does. | |
def wrapper(*args, **kwargs): | |
# Call the function we're decorating, recording its start and end time. | |
start = time.time() |
package main | |
import ( | |
"fmt" | |
"net/http" | |
"reflect" | |
"strconv" | |
"github.com/jinzhu/gorm" | |
"github.com/labstack/echo" |
# coding: utf-8 | |
# [<class '__main__.T_1'>, <class '__main__.B'>, <class '__main__.A'>, <class '__main__.C'>, <class 'object'>] | |
# do this by A | |
# [<class '__main__.T_2'>, <class '__main__.B'>, <class '__main__.D'>, <class '__main__.A'>, <class 'object'>] | |
# do this by D | |
# 先深度优先,如果有实现 method 就返回了, 如果是两个继承自同一个父类, 那这个父类之前会遍历它所有的子类 再去查找它 | |
import os | |
import sys | |
import traceback | |
from functools import wraps | |
from multiprocessing import Process, Queue | |
def processify(func): | |
'''Decorator to run a function as a process. | |
Be sure that every argument and the return value |
#!/bin/sh | |
# Just copy and paste the lines below (all at once, it won't work line by line!) | |
# MAKE SURE YOU ARE HAPPY WITH WHAT IT DOES FIRST! THERE IS NO WARRANTY! | |
function abort { | |
echo "$1" | |
exit 1 | |
} | |
set -e |
import time | |
import multiprocessing | |
def timerecord(func): | |
def wrapper(*args, **kwargs): | |
start = time.time() | |
func(*args, **kwargs) | |
end = time.time() | |
print 'COST: {}'.format(end - start) | |
return wrapper |