!/usr/bin/env python
-- coding: utf-8 --
########################################################################
Copyright (c) 2018 Baidu.com, Inc. All Rights Reserved
########################################################################
"""
File : 3des.py
"""
import sys
import json
import time
import os
import commands
安装相关依赖
try:
import hashids
except:
try:
install_cmd = 'python -m pip install hashids'
status, output = commands.getstatusoutput(install_cmd)
import hashids
except Exception as e:
print(e)
pass
try:
import murmurhash3
except:
try:
install_cmd = 'python -m pip install murmurhash3'
status, output = commands.getstatusoutput(install_cmd)
import murmurhash3
except Exception as e:
print(e)
pass
reload(sys)
sys.setdefaultencoding('utf8')
dpspath = '/home/work/search/dps'
if 'DPS_WORK_DIR' in os.environ:
dpspath = os.environ['DPS_WORK_DIR']
if dpspath[-4:] != '/dps':
dpspath = dpspath + '/dps'
os.environ['DPS_WORK_DIR'] = dpspath
sys.path.append(dpspath + '/')
sys.path.append('/home/work/search/dps/baselib/dps/')
from function import Function
class Confuse(Function):
"""
bos客户端使用
"""
def init(self):
"""
init
"""
def counfuse_id(self, id):
"""
id 混淆函数
"""
counfuse_id = str(id)
# hashids 混淆id
if is_number(counfuse_id):
hasher = hashids.Hashids(min_length=6, salt="lvlin")
ensecret = hasher.encode(1, 2, 3)
counfuse_id = ensecret
else:
return counfuse_id + "---"
return counfuse_id
def is_number(s):
"""
判断是否是数字
"""
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
if name == 'main':
a = Confuse()
res = a.counfuse_id("cc")
print("res=", res)
网友评论