Skip to content

Instantly share code, notes, and snippets.

@zyxue zyxue/
Last active Jul 2, 2019

What would you like to do?
import os
import time
import logging
from functools import update_wrapper
level=logging.DEBUG, format='%(asctime)s|%(levelname)s|%(message)s')
def decorator(d):
"Make function d a decorator: d wraps a function fn."
def _d(fn):
return update_wrapper(d(fn), fn)
update_wrapper(_d, d)
return _d
def timeit(f):
"""time a function, used as decorator"""
def new_f(*args, **kwargs):
bt = time.time()
r = f(*args, **kwargs)
et = time.time()"time spent on {f.__name__}: {et - bt:.2f}s")
return r
return new_f
def backup_file(f):
Back up a file, old_file will be renamed to #old_file.n#, where n is a
number incremented each time a backup takes place
if os.path.exists(f):
dirname = os.path.dirname(f)
basename = os.path.basename(f)
count = 1
rn_to = os.path.join(
dirname, '#' + basename + '.{0}#'.format(count))
while os.path.exists(rn_to):
count += 1
rn_to = os.path.join(
dirname, '#' + basename + '.{0}#'.format(count))"Backing up {0} to {1}".format(f, rn_to))
os.rename(f, rn_to)
return rn_to
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.