forked from LlmKira/Openaibot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBase.py
81 lines (65 loc) · 1.85 KB
/
Base.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: utf-8 -*-
# @Time : 9/22/22 11:04 PM
# @FileName: Base.py
# @Software: PyCharm
# @Github :sudoskys
import time
import rtoml
from rich.console import Console
class Dict(dict):
__setattr__ = dict.__setitem__
__getattr__ = dict.__getitem__
class Tool(object):
def __init__(self):
"""
基本工具类
"""
self.console = Console(color_system='256', style=None)
self.now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
def dictToObj(self, dictObj):
if not isinstance(dictObj, dict):
return dictObj
d = Dict()
for k, v in dictObj.items():
d[k] = self.dictToObj(v)
return d
class StrListTool(object):
@staticmethod
def isStrIn(prompt: str, keywords: list, r: float):
isIn = False
full = len(keywords)
score = 0
for i in keywords:
if i in prompt:
score += 1
if score / full > r:
isIn = True
return isIn
@staticmethod
def isStrAllIn(prompt: str, keywords: list):
isIn = True
for i in keywords:
if i not in prompt:
isIn = False
return isIn
class ReadConfig(object):
def __init__(self, config=None):
"""
read some further config!
param paths: the file path
"""
self.config = config
def get(self):
return self.config
def parseFile(self, paths, toObj: bool = True):
data = rtoml.load(open(paths, 'r'))
self.config = data
if toObj:
self.config = Tool().dictToObj(data)
return self.config
def parseDict(self, data):
self.config = Tool().dictToObj(data)
return self.config
@staticmethod
def saveDict(paths, data):
return rtoml.dump(data, open(paths, 'w'))