from tools import *
import hashlib

def md5jiami(mess):
    t=hashlib.md5(mess)
    return t.hexdigest()

def genUuidDeviceid():
    """
    deviced必须随机生成，不然无法统计spu总数。故就算uuid是相同的，deviceid也不能相同
    :return:
    """
    uuid,deviced=codeuuid.uuid4().urn.split(':')[-1],codeuuid.uuid4().urn.split(':')[-1]
    #uuid='87f491d2-527f-4386-9bb1-3d67017cb6d8'
    deviced='guiqiuyue_'+str(deviced)
    return uuid,deviced

def maxdict(**kwargs):
    return max(kwargs,key=lambda x:len(kwargs[x]))

def strTodict(k):
    if isinstance(k,str):
        return json.loads(k.replace("'",'"'))
    raise Exception('[{}]不支持转为dict'.format(k))

def listTodict(k):
    '''
    :param k: [dict]
    :return:
    '''
    temp=defaultdict()
    for i in k:
        for k1,v1 in i.items():
            if temp[k1]:
                raise Exception("列表中的dict的key[{}]有重复数据，转换类型失败".format(k1))
            temp[k1]=v1
    return dict(temp)


def timenow():
    return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())

if __name__ == '__main__':
    t=[{'15':[1]},{'1590': [1]}, {'1592': [0]},{'1592':[1]}]
    print(listTodict(t))