前言
经常编写一些代码,摘录一下实用的功能代码。
代码
- 时间
#!/usr/bin/python
# coding=utf-8
__author__ = 'testerzhang'
import time
# 转成unix时间
def unix_time(dt):
# 转换成时间数组
timeArray = time.strptime(dt, "%Y-%m-%d %H:%M:%S")
# 转换成时间戳
timestamp = time.mktime(timeArray)
return timestamp
# unix时间转成具体时间
def local_time(timestamp):
# 转换成localtime
time_local = time.localtime(timestamp)
#转换成新的时间格式
dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
return dt
#获取当前日期
def local_date():
return time.strftime('%Y-%m-%d', time.localtime(time.time()))
# 产生日期格式的当前时间戳
def gen_timestamp():
timestamp = datetime.datetime.now()
timestamp = int(time.mktime(timestamp.timetuple()))
print "timestamp=[%s]" % (timestamp)
return timestamp
#把时间戳转化为时间: 1479264792 to 2016-11-16 10:53:12
def TimeStampToTime(timestamp):
timeStruct = time.localtime(timestamp)
return time.strftime('%Y-%m-%d %H:%M:%S', timeStruct)
#获取文件的创建时间,格式化
def get_FileCreateTime_format(filePath):
filePath = unicode(filePath, 'utf8')
t = os.path.getctime(filePath)
return TimeStampToTime(t)
- 搜索文件
#!/usr/bin/python
# coding=utf-8
__author__ = 'testerzhang'
import os
#通过关键字搜索符合的文件
def search(path, keyword):
for filename in os.listdir(path):
fp = os.path.join(path, filename)
if os.path.isfile(fp) and keyword in filename:
print fp
elif os.path.isdir(fp):
search(fp, keyword)
#通过搜索文件内容包含关键字
def search_content(path, keyword):
for filename in os.listdir(path):
fp = os.path.join(path, filename)
if os.path.isfile(fp):
with open(fp) as f:
for line in f:
if keyword in line:
print fp
break
elif os.path.isdir(fp):
search_content(fp, keyword)
- 加密算法:md5加密+DES3加解密
#!/usr/bin/python
# coding=utf-8
__author__ = 'testerzhang'
import urllib
import hashlib
import datetime
import time
from Crypto.Cipher import DES3
import base64
# 加密模式 ECB , 填充模式 PKCS5Padding
BS = DES3.block_size
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
unpad = lambda s : s[0:-ord(s[-1])]
# md5算法,默认返回大写
def md5hex(str):
hl = hashlib.md5()
hl.update(str.encode(encoding='utf-8'))
result = hl.hexdigest().upper()
return result
'''
DES3加密
text 待加密字符串
key 密钥
'''
def encrypt(text, key):
text = pad(text)
cipher = DES3.new(key,DES3.MODE_ECB)
m = cipher.encrypt(text)
m = base64.b64encode(m)
return m.decode('utf-8')
# 比encrypt 多一层md5
def encrypt2(text, key):
text = pad(text)
cipher = DES3.new(key,DES3.MODE_ECB)
m = cipher.encrypt(text)
m2 = base64.b64encode(m)
m3 = md5hex(m2.decode('utf-8'))
return m3
'''
DES3解密
decrypted_text 解密字符串
key 密钥,使用appsecret
'''
def decrypt(decrypted_text, key):
text = base64.b64decode(decrypted_text)
cipher = DES3.new(key, DES3.MODE_ECB)
s = cipher.decrypt(text)
s = unpad(s)
return s.decode('utf-8')
- 装饰器的打印当前函数执行时间
#!/usr/bin/python
# coding=utf-8
__author__ = 'testerzhang'
import time
from functools import wraps
# 计算时间函数
def print_run_time(func):
@wraps(func)
def decorated_function(*args, **kwargs):
local_time = time.time()
result = func(*args, **kwargs)
cost_time = time.time() - local_time
print 'current Function [%s] run time is %.2f s' % (func.__name__, cost_time)
return result
return decorated_function









网友评论