-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
62 lines (46 loc) · 1.98 KB
/
utils.py
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import json as _json
import time
from itertools import zip_longest
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
def chinese_font_support() -> None:
"""matplotlib的中文显示支持的自动设置函数"""
plt.rcParams["font.sans-serif"] = ["MicroSoft YaHei"] # 用来正常显示中文标签
# plt.rcParams["axes.unicode_minus"] = False # 用来正常显示负号
def set_figsize(*size):
"""
设置画幅(先宽后高)(似乎需要在画图之前就设置画幅)
size
(w,h)
"""
if len(size) == 1 and isinstance(size[0], tuple):
plt.rcParams["figure.figsize"] = size[0]
elif len(size) == 2:
plt.rcParams["figure.figsize"] = size
def set_color_palette_from_seaborn(color_palette_name="Set1"):
"""常用: tab20c, tab20, Set1"""
sns.set_palette(sns.color_palette("Set1"))
class JsonFile:
@staticmethod
def load(jsonFile="data.json", encoding="utf-8"):
"""读取Json文件"""
with open(jsonFile, "r", encoding=encoding) as f:
return _json.load(f)
@staticmethod
def write(item, jsonFile="data.json", encoding="utf-8", ensure_ascii=False):
"""写入Json文件"""
with open(jsonFile, "w", encoding=encoding) as f:
_json.dump(item, f, ensure_ascii=ensure_ascii)
def format_time(with_date=False):
time_struct = time.localtime()
date_str = f"{time_struct.tm_year}-{time_struct.tm_mon}-{time_struct.tm_mday}"
time_str = f"{time_struct.tm_hour}-{time_struct.tm_min}-{time_struct.tm_sec}"
return f"{date_str}--{time_str}" if with_date else f"{time_str}"
def gene_markdown_table(data: dict):
index_arr, value_arr = zip(*data.items())
text = f"""|{"|".join(map(str, index_arr))}|
|{'|'.join(map(lambda x:":-:", range(len(index_arr))))}|\n"""
for values in zip_longest(*value_arr, fillvalue=""):
text += f"""|{"|".join(map(str, values))}|\n"""
return text