我收到command, *args = line.split()
这是我的bce.py
文件
import sys
import os
import pcn
import urp
class BCE(object):
def __init__(self, inPath, outPath):
self.inPath = inPath
self.outPath = outPath
self.eqs = {}
self.operations = {
"r": self.read_pcn,
"!": self.do_not,
"+": self.do_or,
"&": self.do_and,
"p": self.write_pcn,
"q": self.quit
}
self.done = False
def process(self, commandFilePath):
with open(commandFilePath, "r") as f:
for line in f:
command, *args = line.split()
self.operations[command](*args)
if self.done:
return
def read_pcn(self, fNum):
_, self.eqs[fNum] = pcn.parse(os.path.join(self.inPath, fNum + ".pcn"))
def write_pcn(self, fNum):
with open(os.path.join(self.outPath, fNum + ".pcn"), "w") as f:
pcn.write(f, None, self.eqs[fNum])
def do_not(self, resultNum, inNum):
self.eqs[resultNum] = urp.complement(self.eqs[inNum])
def do_or(self, resultNum, leftNum, rightNum):
self.eqs[resultNum] = urp.cubes_or(self.eqs[leftNum], self.eqs[rightNum])
def do_and(self, resultNum, leftNum, rightNum):
self.eqs[resultNum] = urp.cubes_and(self.eqs[leftNum], self.eqs[rightNum])
def quit(self):
self.done = True
Usage = """\
USAGE: {} COMMAND_FILE
"""
if __name__ == "__main__":
if len(sys.argv) > 1:
solutionDir = "BCESolutions"
thisSolDir = os.path.join(solutionDir, sys.argv[1][-5])
try:
os.mkdir(thisSolDir)
except OSError:
# It's okay if it's already there
pass
bce = BCE("BooleanCalculatorEngine", thisSolDir)
bce.process(sys.argv[1])
else:
print(Usage.format(sys.argv[0]))
这是我的pcn.py
文件
from itertools import islice
from itertools import chain
def parse(filePath):
with open(filePath, "rb") as f:
# First line is size of array
try:
lines = iter(f)
numVars = int(next(lines))
cubeCount = int(next(lines))
cubes = [None]*cubeCount
for i in range(cubeCount):
line = next(lines)
cubes[i] = tuple(islice(map(int, line.split()), 1, None))
return (numVars, tuple(cubes))
except Exception as error:
raise AssertionError("Bad pcn file {}".format(filePath)) from error
def write(f, numVars, cubes):
endl = "\n"
f.write(str(max(max(map(abs, cube)) for cube in cubes)))
f.write(endl)
f.write(str(len(cubes)))
f.write(endl)
cubes = tuple(set(tuple(sorted(cube, key=abs)) for cube in cubes))
for cube in cubes:
f.write(' '.join(map(str, chain((len(cube),), cube))))
f.write(endl)
f.write(endl)
答案 0 :(得分:2)
使用*star_target
条目的元组赋值仅适用于Python 3.您不能在Python 2中使用它。请参阅PEP 3132 - Extended Iterable Unpacking。
作为一种解决方法,只需一个目标,然后使用切片:
split_result = line.split()
command, args = split_result[0], split_result[1:]