This commit is contained in:
2023-09-29 17:20:03 +08:00
parent 732dd0453d
commit cd3dc6fcaa
4 changed files with 346 additions and 95 deletions

View File

@ -1,7 +1,7 @@
from typing import Any, Literal, Optional, TextIO
import urllib.request
import urllib.error
import urllib.request
from typing import Any, Callable, Literal, Optional, Set, TextIO, Tuple, Dict, List
import TrimLog
from TrimLog import Console, object_constants
@ -19,10 +19,10 @@ logger = TrimLog.Logger(
try:
myWords = (
urllib.request.urlopen(
'https://gitee.com/TriM-Organization/LinglunStudio/raw/master/resources/myWords.txt'
"https://gitee.com/TriM-Organization/LinglunStudio/raw/master/resources/myWords.txt"
)
.read()
.decode('utf-8')
.decode("utf-8")
.strip("\n")
.split("\n")
)
@ -158,27 +158,32 @@ def ipt(
def format_ipt(
notice: str,
fun,
err_note: str = "",
fun: Callable,
err_note: str = "{}",
strict_mode: bool = False,
*extraArg,
):
) -> Tuple[str, Any]:
"""循环输入,以某种格式
notice: 输入时的提示
fun: 格式函数
err_note: 输入不符格式时的提示
strict_mode: 是否将函数值作为结束循环的判断依据之一
*extraArg: 对于函数的其他参数"""
while True:
result = ipt(notice)
# noinspection PyBroadException
try:
fun_result = fun(result, *extraArg)
break
# noinspection PyBroadException
except BaseException:
prt(err_note)
if strict_mode:
if fun_result := fun(result, *extraArg):
break
else:
fun_result = fun(result, *extraArg)
break
except ValueError as E:
prt(err_note.format(E))
continue
return result, fun_result
def isin(sth: str, range_list: dict):
sth = sth.lower()
for bool_value, res_list in range_list.items():
@ -192,9 +197,9 @@ def bool_str(sth: str):
try:
return bool(float(sth))
except:
if str(sth).lower() in ("true", "", "", 'y', 't'):
if str(sth).lower() in ("true", "", "", "y", "t"):
return True
elif str(sth).lower() in ("false", "", "", 'f', 'n'):
elif str(sth).lower() in ("false", "", "", "f", "n"):
return False
else:
raise ValueError
@ -205,12 +210,40 @@ def float_str(sth: str):
return float(sth)
except ValueError:
try:
return float(sth.replace("", "1").replace("", "2").replace("", "3").replace("", "4").replace("", "5").replace("", "6").replace("", "7").replace("", "8").replace("", "9").replace("", "0").replace("", "1").replace("",'2').replace("", "3").replace("", "4").replace("", "5").replace("", "6").replace("", "7").replace("", "8").replace("", "9").replace("", "0").replace("", "0").replace("", "1").replace("", "2").replace("", "2").replace("","7").replace("",'.'))
return float(
sth.replace("", "1")
.replace("", "2")
.replace("", "3")
.replace("", "4")
.replace("", "5")
.replace("", "6")
.replace("", "7")
.replace("", "8")
.replace("", "9")
.replace("", "0")
.replace("", "1")
.replace("", "2")
.replace("", "3")
.replace("", "4")
.replace("", "5")
.replace("", "6")
.replace("", "7")
.replace("", "8")
.replace("", "9")
.replace("", "0")
.replace("", "0")
.replace("", "1")
.replace("", "2")
.replace("", "2")
.replace("", "7")
.replace("", ".")
)
except:
raise ValueError
def int_str(sth: str):
try:
return int(float_str(sth))
except ValueError:
raise ValueError
raise ValueError