Ai
1 Star 0 Fork 0

forward-ovo/Python-Class

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
common.py 2.26 KB
一键复制 编辑 原始数据 按行查看 历史
ysy 提交于 2025-06-08 14:33 +08:00 . 英语笔记管理系统
import re
import sys
import base64
from io import BytesIO
from PIL import Image
def cmdParser(command: str) -> list[str]:
command = re.sub(r'\\\n\s*', '', command)
command = re.sub(r'#.*$', '', command)
pattern = r'''
(?: "(?:\\.|[^"\\])*" | # 双引号字符串
'(?:\\.|[^'\\])*' | # 单引号字符串
[^\s"']+ # 非空白字符
)
'''
matches = re.findall(pattern, command, re.VERBOSE)
def unquote(s:str):
# 处理转义字符
if s.startswith('"') and s.endswith('"'):
s = s[1:-1]
# 双引号内处理转义字符
s = re.sub(r'\\(["\\abfnrtv])', _replace_escape, s)
elif s.startswith("'") and s.endswith("'"):
s = s[1:-1]
# 单引号内只允许转义单引号和反斜杠
s = re.sub(r'\\([\'\\])', _replace_escape, s)
return s
def _replace_escape(match: re.Match) -> str:
esc = match.group(1)
escape_map = {
'a': '\a', # 响铃
'b': '\b', # 退格
'f': '\f', # 换页
'n': '\n', # 换行
'r': '\r', # 回车
't': '\t', # 制表符
'v': '\v', # 垂直制表符
'"': '"', # 双引号
"'": "'", # 单引号
'\\': '\\', # 反斜杠
}
return escape_map.get(esc, esc) # 如果找不到,返回原字符
return [unquote(m) for m in matches if m.strip()]
def fileSelector(remote: bool = False) -> bytes:
if not remote:
file_path = input("请输入图片文件地址:")
try:
with open(file_path, 'rb') as f:
data = f.read()
return data
except FileNotFoundError:
print("[fileSelector] 文件打开失败!请检查文件路径。")
else:
sys.stdout.write("__!fileSelector()_called!__")
data = sys.stdin.readline().strip()
data = base64.b64decode(data)
return data
def showImage(data: bytes, remote: bool = False):
if not remote:
Image.open(BytesIO(data)).show()
else:
sys.stdout.write("__!showImage()_called!__")
data = base64.b64encode(data).decode()
sys.stdout.write(data)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/forward-ovo/python-class.git
git@gitee.com:forward-ovo/python-class.git
forward-ovo
python-class
Python-Class
master

搜索帮助