chio / module /kitool.py
kkk2z1's picture
Create kitool.py
49cba05 verified
#汎用ツール。
import os
import time
from datetime import datetime
import json
import traceback
from colorama import Fore, Back, Style, init
#===ツール===
def log(msg, msg_type):
try:
init()
time_datetime = datetime.now()
if msg_type == 1: # 通常ログ(緑)
print(f"{time_datetime}{Fore.GREEN}[log]: {msg}")
elif msg_type == 2: # 警告(黄色)
print(f"{time_datetime}{Fore.YELLOW}[warn]: {msg}")
elif msg_type == 3: # エラー(赤)
print(f"{time_datetime}{Fore.RED}[error]: {msg}")
elif msg_type == 4: # エラー(エラー部分のトレースバック付き。)
print(f"{time_datetime}{Fore.RED}[error]: {msg}")
print(Fore.RED)
traceback.print_exc()
except Exception as e:
time_datetime = datetime.now()
print(f"{time_datetime}[error]:log()関数にてエラーあり。:{e}")
def count_down(sec,count_down_msg):
try:
START_TIME = sec
while START_TIME > 0:
print(f"{count_down_msg}{START_TIME}秒",)
START_TIME = START_TIME - 1
time.sleep(1)
except Exception as e:
time_datetime = datetime.now()
print(f"{time_datetime}[error]:count_down()関数にてエラーあり:{e}")
def hash_time(hash):
try:
loop = hash
rand_hash = random.randint(0, 75)
now_hash = hash(f"{rand_hash}")
hash_s_time = time.time()
while loop > 0
now_hash = hash(f"{now_hash}")
hash_e_time = time.time()
hash_time = hash_s_time - hash_e_time
return hash_time
except Exception as e:
time_datetime = datetime.now()
print(f"{time_datetime}[error]:hash_time()関数にてエラーあり:{e}")
#===使い方===
#log(msg, msg_type):
# ログ出力。
# log("出力させたいログメッセージ",ログの種類):
# 指定できるログの種類:
# 1=通常のログ
# 2=警告
# 3=エラー
# 4=エラー(トレースバック付き)
#count_down(sec,count_down_msg):
# 指定された秒数カウントダウンをする。
# count_down(カウントする秒数,残り秒数の前に置く文字列):
#hash_time(hash):
# 指定されたから回数だけhashを行い処理にかかった時間を出力する。
# hash_time(hashする回数)