如图,我这个代码的错误都集中在这些filepath的应用上,如 return cls(itemre)说'filepath'unfilled,还有一些其他的问题不知道怎么改,希望能解释一下
from typing import Optional
from os import path
import re
from BoardSetting import BoardSetting
from NetSetting import NetSetting
BusList_org = []
BoundaryPoints_org = []
Components_org = []
PathList_org = []
term_regex = r'''(?mx)
\s*(?:
(?P<roundl>\()|
(?P<roundr>\))|
(?P<num>[+-]?\d+\.\d+(?=[\ \]|\-?\d+(?=[\ \]]))|
(?P<s>[^(^)\s]+)
)'''
def parse_sexp(sexp):
stack = []
out = []
for termtypes in re.finditer(term_regex,sexp):
term,value = [(t,v) for t,v in termtypes.groupdict().items() if v][0]
if term =='roundl':
stack.append(out)
out = []
elif term == 'roundr':
assert stack,"Trouble with nesting of brackets"
tmpout, out = out,stack.pop(-1)
out.append(tmpout)
elif term == 'num':
v = float(value)
if v.is_integer():v = int(v)
out.append(v)
elif term == 's':
out.append(value)
else:
raise NotImplementedError("Error: %r" % (term,value))
assert not stack,"Trouble with nesting of brackets"
return out[0]
class Board:
def __init__(self,item,filePath:str,encoding: Optional[str] = None):
self.filePath = filePath
self.item = item
@classmethod
def from_sexpr(cls, exp: list,filePath:str):
p1=0
p2=0
p3=0
p4=0
for i in range(len(exp)):
p = exp[i]
if p == "Bus":
p1 = i
elif p == "BoundaryPoints":
p2 = i
elif p == "Components":
p3 = i
elif p == "PathList":
p4 = i
for i in range(0, p1):
BusList_org[i] = exp[i]
for i in range(p1 + 1, p2):
BoundaryPoints_org[i] = exp[i]
for i in range(p2 + 1, p3):
Components_org[i] = exp[i]
for i in range(p3 + 1, p4):
PathList_org[i] = exp[i]
for i in range(len(BusList_org)):
BusList_org.append(BusList_org[i])
for i in range(len(BoundaryPoints_org)):
BoundaryPoints_org.append(BoundaryPoints_org[i])
for i in range(len(Components_org)):
Components_org.append(Components_org[i])
for i in range(len(PathList_org)):
PathList_org.append(PathList_org[i])
itemre = [0, 0, 0, 0]
itemre[0] = BusList_org
itemre[1] = BoundaryPoints_org
itemre[2] = Components_org
itemre[3] = PathList_org
return cls(itemre)
@classmethod
def from_file(cls, filepath: str, encoding: Optional[str] = None) :
if not path.isfile(filepath):
raise Exception("Given path is not a file!")
with open(filepath, 'r', encoding=encoding) as infile:
# 把文件字符串分成S列表之后通过from_sexpr来导出各个数的值
item = cls.from_sexpr(parse_sexp(infile.read()))
item.filePath = filepath
last = []
last = cls.read_stack(item[1])
return last
#item是一个列表,每一个元素包含了一个栈,last是最后输出的栈
@classmethod
def read_stack(cls,stack):
read=[]
stack2 = []
while stack:
i=0
read[i] = stack.pop()
i+=1
stack2.append(read[i])
return cls(stack2)
#这是从filepath的s表达式中获取boundarypoint的代码:
filepath="E:\pythonstudy\check\mytext.txt"
a = Board.from_file(filepath)
def read_stack(stack):
read = []
while stack:
i = 0
read[i] = stack.pop()
i += 1
return read
BoundaryPoints = read_stack(a)
for i in range(len(BoundaryPoints)):
print(BoundaryPoints[i])