修复一些bug
This commit is contained in:
@@ -350,3 +350,25 @@ class DescriptiveIndicator[T]:
|
|||||||
self.descripion : str = description
|
self.descripion : str = description
|
||||||
self.value : T = value
|
self.value : T = value
|
||||||
|
|
||||||
|
class Switch:
|
||||||
|
def __init__(self, value, isThougth = False) -> None:
|
||||||
|
self.value = value
|
||||||
|
self.isThougth = False
|
||||||
|
self.caseStats = False
|
||||||
|
self.result = None
|
||||||
|
|
||||||
|
def Case(self, caseValue, callback:Callable[[], Any]) -> 'Switch':
|
||||||
|
if self.caseStats and self.isThougth:
|
||||||
|
self.result = callback()
|
||||||
|
elif caseValue == self.value:
|
||||||
|
self.caseStats = True
|
||||||
|
self.result = callback()
|
||||||
|
return self
|
||||||
|
|
||||||
|
def Default(self, callback:Callable[[], Any]) -> Any:
|
||||||
|
if self.caseStats and self.isThougth:
|
||||||
|
self.result = callback()
|
||||||
|
elif self.caseStats == False:
|
||||||
|
self.caseStats = True
|
||||||
|
self.result = callback()
|
||||||
|
return self.result
|
@@ -77,7 +77,10 @@ class ToolFile(BaseModel):
|
|||||||
self,
|
self,
|
||||||
filePath: Union[str, Self],
|
filePath: Union[str, Self],
|
||||||
):
|
):
|
||||||
super().__init__(OriginFullPath=os.path.abspath(os.path.expandvars(str(filePath))))
|
filePath = os.path.expandvars(str(filePath))
|
||||||
|
if filePath[1:].startswith(":/") or filePath[1:].startswith(":\\"):
|
||||||
|
filePath = os.path.abspath(filePath)
|
||||||
|
super().__init__(OriginFullPath=filePath)
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
pass
|
pass
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
@@ -89,9 +92,15 @@ class ToolFile(BaseModel):
|
|||||||
|
|
||||||
def __or__(self, other):
|
def __or__(self, other):
|
||||||
if other is None:
|
if other is None:
|
||||||
return ToolFile(self.GetFullPath() if self.IsDir() else self.GetFullPath()+"\\")
|
return ToolFile(self.GetFullPath() if self.IsDir() else f"{self.GetFullPath()}\\")
|
||||||
else:
|
else:
|
||||||
return ToolFile(os.path.join(self.GetFullPath(), str(other)))
|
# 不使用os.path.join,因为os.path.join存在如下机制
|
||||||
|
# 当参数路径中存在绝对路径风格时,会忽略前面的参数,例如:
|
||||||
|
# os.path.join("E:/dev", "/analyze/") = "E:/analyze/"
|
||||||
|
# 而我们需要的是 "E:/dev/analyze"
|
||||||
|
first = self.GetFullPath().replace('/','\\').strip('\\')
|
||||||
|
second = str(other).replace('/','\\')
|
||||||
|
return ToolFile(f"{first}\\{second}")
|
||||||
def __idiv__(self, other):
|
def __idiv__(self, other):
|
||||||
temp = self.__or__(other)
|
temp = self.__or__(other)
|
||||||
self.OriginFullPath = temp.GetFullPath()
|
self.OriginFullPath = temp.GetFullPath()
|
||||||
@@ -109,20 +118,20 @@ class ToolFile(BaseModel):
|
|||||||
"""
|
"""
|
||||||
if other is None:
|
if other is None:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# 获取比较对象的路径
|
# 获取比较对象的路径
|
||||||
other_path = other.GetFullPath() if isinstance(other, ToolFile) else str(other)
|
other_path = other.GetFullPath() if isinstance(other, ToolFile) else str(other)
|
||||||
self_path = self.OriginFullPath
|
self_path = self.OriginFullPath
|
||||||
|
|
||||||
# 标准化路径,移除末尾的斜线
|
# 如果两个文件都存在,则直接比较路径
|
||||||
if self_path.endswith('/') or self_path.endswith('\\'):
|
if self.Exists() == True and other.Exists() == True:
|
||||||
self_path = self_path[:-1]
|
return self_path.strip('\\/') == other_path.strip('\\/')
|
||||||
if other_path.endswith('/') or other_path.endswith('\\'):
|
# 如果一个文件存在另一个不被判定为存在则一定不同
|
||||||
other_path = other_path[:-1]
|
elif self.Exists() != other.Exists():
|
||||||
|
return False
|
||||||
# 使用系统的路径规范化函数进行比较
|
# 如果两个文件都不存在,则直接比较文件名在视正反斜杠相同的情况下比较路径字符串
|
||||||
return os.path.normpath(self_path) == os.path.normpath(other_path)
|
else:
|
||||||
|
return self_path.replace('/','\\') == other_path.replace('/','\\')
|
||||||
|
|
||||||
def ToPath(self):
|
def ToPath(self):
|
||||||
return Path(self.OriginFullPath)
|
return Path(self.OriginFullPath)
|
||||||
|
@@ -237,15 +237,15 @@ pip install pyinstaller
|
|||||||
pyinstaller --onefile --name convention-tool your_main_script.py
|
pyinstaller --onefile --name convention-tool your_main_script.py
|
||||||
```
|
```
|
||||||
|
|
||||||
## 📄 许可证
|
## 许可证
|
||||||
|
|
||||||
本项目采用 MIT 许可证 - 查看 [LICENSE](LICENSE) 文件了解详情。
|
本项目采用 MIT 许可证 - 查看 [LICENSE](LICENSE) 文件了解详情。
|
||||||
|
|
||||||
## 👨💻 作者
|
## 作者
|
||||||
|
|
||||||
**LiuBai** - [NINEMINEsigma](https://github.com/NINEMINEsigma)
|
**LiuBai** - [NINEMINEsigma](https://github.com/NINEMINEsigma)
|
||||||
|
|
||||||
## 🔗 相关链接
|
## 相关链接
|
||||||
|
|
||||||
- [Convention-Template](https://github.com/NINEMINEsigma/Convention-Template) - 项目模板规范
|
- [Convention-Template](https://github.com/NINEMINEsigma/Convention-Template) - 项目模板规范
|
||||||
- [GitHub Issues](https://github.com/NINEMINEsigma/Convention-Python/issues) - 问题反馈
|
- [GitHub Issues](https://github.com/NINEMINEsigma/Convention-Python/issues) - 问题反馈
|
||||||
|
@@ -2,95 +2,8 @@ import sys
|
|||||||
import os
|
import os
|
||||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||||
|
|
||||||
from Convention.Runtime.Asynchrony import *
|
from Convention.Runtime.File import *
|
||||||
|
|
||||||
class Test(Asynchronous):
|
first = ToolFile("E:/dev/")
|
||||||
a:int
|
second = ToolFile("/analyze/")
|
||||||
b:int
|
print(first|second)
|
||||||
c:int
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
super().__init__(c={"timeout":2},b={"timeout":10})
|
|
||||||
self.a = 1
|
|
||||||
|
|
||||||
|
|
||||||
async def seta(obj:Test, value:int, delay:float = 1) -> None:
|
|
||||||
await asyncio.sleep(delay)
|
|
||||||
obj.a = value
|
|
||||||
|
|
||||||
async def setb(obj:Test, value:int, delay:float = 1) -> None:
|
|
||||||
await asyncio.sleep(delay)
|
|
||||||
obj.b = value
|
|
||||||
|
|
||||||
async def setc(obj:Test, value:int, delay:float = 1) -> None:
|
|
||||||
await asyncio.sleep(delay)
|
|
||||||
obj.c = value
|
|
||||||
|
|
||||||
async def run():
|
|
||||||
print("=== 测试优化后的异步字段系统 ===")
|
|
||||||
test = Test()
|
|
||||||
|
|
||||||
# 测试字段状态检查
|
|
||||||
assert test.is_field_initialized('a')
|
|
||||||
assert not test.is_field_initialized('b')
|
|
||||||
assert not test.is_field_initialized('c')
|
|
||||||
|
|
||||||
print("\n=== 测试1:未设置值的情况(应该超时)===")
|
|
||||||
try:
|
|
||||||
print(f"失败: {test.a,test.b,test.c}")
|
|
||||||
raise RuntimeError("测试1应该超时")
|
|
||||||
except Exception as e:
|
|
||||||
print(f"成功: {e}")
|
|
||||||
|
|
||||||
print("\n=== 测试2:在超时前设置字段b的值 ===")
|
|
||||||
# 创建新的测试实例
|
|
||||||
test2 = Test()
|
|
||||||
assert not test2.is_field_initialized('b')
|
|
||||||
|
|
||||||
# 启动并发任务:设置b的值和获取b的值
|
|
||||||
# 并发执行:设置b值(延迟0.5秒)和获取b值
|
|
||||||
await asyncio.gather(
|
|
||||||
setb(test2, 42, delay=0.5), # 0.5秒后设置b=42
|
|
||||||
return_exceptions=True
|
|
||||||
)
|
|
||||||
|
|
||||||
assert test2.b == 42
|
|
||||||
assert test2.is_field_initialized('b')
|
|
||||||
assert test2.b == 42
|
|
||||||
|
|
||||||
test3 = Test()
|
|
||||||
test3.b = 100
|
|
||||||
assert test3.is_field_initialized('b')
|
|
||||||
assert test3.b == 100
|
|
||||||
|
|
||||||
print("\n=== 测试3:测试字段c(短超时,应该仍然超时)===")
|
|
||||||
try:
|
|
||||||
print(f"失败: {test.c}")
|
|
||||||
except TimeoutError as timeout_e:
|
|
||||||
print(f"成功: {timeout_e}")
|
|
||||||
|
|
||||||
def test_sync_access():
|
|
||||||
"""测试同步访问(在非异步上下文中)"""
|
|
||||||
print("\n=== 测试同步访问 ===")
|
|
||||||
test = Test()
|
|
||||||
|
|
||||||
# 测试已初始化字段的同步访问
|
|
||||||
try:
|
|
||||||
print(f"成功: a = {test.a}")
|
|
||||||
except Exception as e:
|
|
||||||
raise
|
|
||||||
|
|
||||||
# 测试未初始化字段的同步访问(应该有更友好的错误提示)
|
|
||||||
try:
|
|
||||||
print(f"失败: c = {test.c}")
|
|
||||||
raise RuntimeError("字段c此时不应该能够被访问")
|
|
||||||
except Exception as e:
|
|
||||||
print(f"成功: {e}")
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
# 测试同步访问
|
|
||||||
test_sync_access()
|
|
||||||
|
|
||||||
# 测试异步访问
|
|
||||||
print("\n=== 开始异步测试 ===")
|
|
||||||
run_until_complete(run())
|
|
Reference in New Issue
Block a user