import time, datetime, traceback, os, random, requests, websocket, re
import threading




def speedTest(function, *args):
    printTime(function(*args))
    minTime = 9999
    maxTime = 0
    intiTime = time.time()

    i = 0
    while time.time() -1 < intiTime:
        lastTime = time.time()
        function(*args)
        minTime = min(minTime , time.time() - lastTime)
        maxTime = max(maxTime , time.time() - lastTime)
        i = i +1
    printTime('avgTime', i, 'minTime', minTime, 'maxTime', maxTime)
    return i


def trying(function, *args):
    i = 0
    while i < 10:
        try:
            return (function(*args))
        except BaseException as err:
            i = i + 1
            print('err', i, err)
            time.sleep(5)
    return False

def pushTxtTimeAuto(str1, name):
    nowtime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
    str1 = str(str1)
    file = open('./data/' + name + str(time.time())[:8]+ '.txt', 'a')
    file.write(nowtime + '_' + str1 + '\n')
    file.close()

def pushText(str1, name):
    name= str(name)
    str1 = str(str1)
    file = open('./data/' + name + '.txt' , 'w')
    file.write(str1)
    file.close()

def pushTextTime(str1, name):
    name = str(name)
    nowtime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
    str1 = str(str1)
    file = open('./data/' + name + '.txt', 'a')
    file.write(nowtime + '_' + str1 + '\n')
    file.close()

def readText(name):
    return readAny(name + '.txt')


def readJson(name):
    return readAny(name + '.json')


def readAny(name):
    try:
        file = open('./data/' + name, 'r')
        info = file.read()
        file.close()
        info = eval(info)
    except:
        info = eval(requests.get(url + 'data/' + name).text)
    return info


def printTime(*args):
    print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3],
                       *("%.6f" % a if isinstance(a, float) else a
                         for a in args))


def printErr(*args):
    printTime('*'*20 + 'ERR' + '*'*20 )
    printTime(*args)
    printTime('*'*20 + 'ERR' + '*'*20 )


def asyn(function, *args):
    threading.Thread(target=function, args=(*args,)).start()

def test(n):
    i = 0
    while i < n:
        i = i + 1
    return i


printTime('OS:OK')




