File size: 1,952 Bytes
cec26ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import time, os
location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))

## Các màu hay dùng
BLACK   = '\033[30m';   WHITE  = '\033[97m'
RED     = '\033[91m';   YELLOW = '\033[33m'
GREEN   = '\033[32m';   CYAN   = '\033[36m'
BLUE    = '\033[94m';   GREY   = '\033[37m'
MAGENTA = '\033[95m';   RESET  = '\033[0m'

# Màu đậm           # Màu sáng          # Màu nền đậm       # Màu nền sáng
DK = '\033[30m';    BK = '\033[90m';    GDK = '\033[40m';   GBK = '\033[100m'; # BLACK
DR = '\033[31m';    BR = '\033[91m';    GDR = '\033[41m';   GBR = '\033[101m'; # RED
DG = '\033[32m';    BG = '\033[92m';    GDG = '\033[42m';   GBG = '\033[102m'; # GREEN
DY = '\033[33m';    BY = '\033[93m';    GDY = '\033[43m';   GBY = '\033[103m' # YELLOW
DB = '\033[34m';    BB = '\033[94m';    GDB = '\033[44m';   GBB = '\033[104m'; # BLUE
DM = '\033[35m';    BM = '\033[95m';    GDM = '\033[45m';   GBM = '\033[105m'; # MAGENTA (tím hồng)
DC = '\033[36m';    BC = '\033[96m';    GDC = '\033[46m';   GBC = '\033[106m'; # CYAN
DW = '\033[37m';    BW = '\033[97m';    GDW = '\033[47m';   GBW = '\033[107m'; # WHITE

def pretty_num(x): return round(x*100)/100

TIMER_STARTED_AT = { "default": time.time() }
def reset_timer(timer="default"):
    global TIMER_STARTED_AT
    TIMER_STARTED_AT[timer] = time.time()

def measure_time(message="", timer="default", color=YELLOW):
    total = time.time() - TIMER_STARTED_AT[timer]
    total = round(total * 100) / 100

    message = message.strip()
    if len(message) > 0: 
    	message = " " + message

    print(f"{color}{timer}:{message} {total} seconds{RESET}")

count_words = lambda x: len(x.split())

if __name__ == "__main__":
	reset_timer(timer="my timer")
	s = "chào cả nhà, cả nhà khỏe không ạ?"
	print(f"{RED}{s}{RESET}{CYAN}{count_words(s)} từ")
	measure_time("tổng thời gian chạy", timer="my timer")