我遇到了代码不断创建的错误。我不明白错误,因为错误1:e2在引用之前被分配。另外我认为我没有通过__ eq __传递任何元组。谢谢!
!
这是我的测试代码
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def compare(self, other):
x1 = self.x
y1 = self.y
x2 = other.x
y2 = other.y
n = 512
if (x1 == x2) and (y1 == y2):
return 0
found = False
while found == False:
if ((x1 // n) != (x2 // n)) or ((y1 // n) != (y2 // n)):
found = True
c1 = ((x1 // n), (y1 // n))
c2 = ((x2 // n), (y2 // n))
else:
if x1 >= n and x2 >= n:
x1 = x1 - n
x2 = x2 - n
if y1 >= n and y2 >= n:
y1 = y1 - n
y2 = y2 - n
n = n / 2
if c1 == (0, 1):
e1 = 1
if c1 == (1, 1):
e1 = 2
if c1 == (0, 0):
e1 = 3
if c1 == (1, 0):
e1 = 4
if c2 == (0, 1):
e2 = 1
if c2 == (1, 1):
e2 = 2
if c2 == (0, 0):
e2 = 3
if c2 == (1, 0):
e2 = 4
if e1 > e2:
return 1
if e2 > e1:
return -1
def dist(self, other):
x = abs(self.x - other.x)
y = abs(self.y - other.y)
if x >= y:
return x
else:
return y
def getx(self):
return self.x
def gety(self):
return self.y
def __lt__(self, other):
return self.compare(other) == -1
def __eq__(self, other):
return self.x == other.getx and self.y == other.gety
def __le__(self, other):
return self < other or self == other
class PointSet:
def __init__(self):
self.list = []
def add(self, point):
count = 0
found = False
if self.list == []:
self.list.append(point)
found = True
while not found:
for x,y in self.getpoints():
if point.compare(Point(x, y)) == -1:
self.list.insert(count, point)
found = True
return count
else:
if count == len(self.list)-1:
self.list.append(point)
found = True
count = count + 1
def NN(self, query):
count = 10000
trace = ()
if self.list == []:
return None
for x,y in self.getpoints():
if query.dist(Point(x, y)) < count:
count = query.dist(Point(x, y))
trace = Point(x, y)
return trace
def ANN(self, query):
count = 0
if self.list == []:
return None
for x,y in self.getpoints():
if query.compare(Point(x, y)) == -1:
(x1, y1) = self.getpoints()[count]
(x2, y2) = self.getpoints()[count+1]
(x3, y3) = self.getpoints()[count-1]
(x4, y4) = self.getpoints()[count-2]
p = min(query.dist(Point(x1, y1)), query.dist(Point(x2, y2)), query.dist(Point(x3, y3)), query.dist(Point(x4, y4)))
if p == query.dist(Point(x1, y1)):
return Point(x1, y1)
if p == query.dist(Point(x2, y2)):
return Point(x2, y2)
if p == query.dist(Point(x3, y3)):
return Point(x3, y3)
if p == query.dist(Point(x4, y4)):
return Point(x4, y4)
else:
count = count + 1
def getpoints(self):
return [(i.x, i.y) for i in self.list]
和我的回溯错误:
import unittest
from Point import Point
from PointSet import PointSet
class TestPointSet(unittest.TestCase):
def setUp(self):
coords = [
(300, 800),
(12,720),
(75,660),
(150,550),
(605 , 810),
(900, 640),
(100, 390),
(300, 400),
(80, 100),
(260, 30),
(400, 25),
(1000, 450),
(940, 400),
(990, 410),
(800, 280)
]
self.pt_list = [Point(x,y) for x,y in coords]
def test_new_point_set(self):
pts = PointSet()
pts.add(Point(0,0))
pts.add(Point(0,1023))
def test_pointset_is_ordered(self):
pointset = PointSet()
for i in range(10):
for j in range(10):
pointset.add(Point(i* 8+1,j * 16 - 1))
for i in range(100-1):
assert(pointset.getpoints()[i] < pointset.getpoints()[i+1])
def test_pointset_is_ordered2(self):
pts = PointSet()
pts.add(self.pt_list[3])
pts.add(self.pt_list[10])
pts.add(self.pt_list[6])
pts.add(self.pt_list[11])
pts.add(self.pt_list[1])
pts.add(self.pt_list[4])
pts.add(self.pt_list[7])
pts.add(self.pt_list[14])
pts.add(self.pt_list[8])
pts.add(self.pt_list[5])
pts.add(self.pt_list[13])
pts.add(self.pt_list[9])
pts.add(self.pt_list[12])
pts.add(self.pt_list[0])
pts.add(self.pt_list[2])
for i,p in enumerate(pts.getpoints()):
self.assertEqual(p, self.pt_list[i])
def test_NN(self):
pointset = PointSet()
for i in range(15):
for j in range(15):
pointset.add(Point(i * 64, j * 64))
for i in range(15):
for j in range(15):
nn = pointset.NN(Point(i * 64 - 31, j * 64 + 31))
assert(nn.getx() == i * 64)
assert(nn.gety() == j * 64)
def test_ANN(self):
ps = PointSet()
for p in self.pt_list:
ps.add(p)
self.assertEqual(ps.ANN(Point(129, 390)), self.pt_list[6])
self.assertEqual(ps.ANN(Point(1000, 512)), self.pt_list[5])
if __name__ == '__main__':
unittest.main()
答案 0 :(得分:0)
只有在满足第一个if语句的条件时,才会在特定情况下定义e1和e2。即使这样,你也没有定义e1和e2。 所以你的代码没有机会正常工作。
您可以更改代码以在函数顶部定义e1和e2:
def compare(self, other):
e1, e2 = 0,0
x1 = self.x
y1 = self.y
x2 = other.x
y2 = other.y
n = 512
if (x1 == x2) and (y1 == y2):
return 0
found = False
while not found:
另请注意,while not found
比while found == false
更清晰,更加pythonic。
您的_eq_
功能不正确,因为您没有调用这些功能。将其更改为:
def __eq__(self, other):
return self.x == other.getx() and self.y == other.gety()