2016-03-03 43 views
-2

我遇到了一些我的代碼不斷創建的錯誤。我不明白錯誤,因爲對於錯誤1:在引用之前分配了e2。我也不認爲我通過__ eq __傳遞了任何元組。謝謝!賦值之前引用的局部變量'e2'錯誤

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() 

和我回溯錯誤:

Traceback (most recent call last): 
File "C:\Users\\Desktop\\skeleton (5)\TestPointSet.py", line 35, in test_pointset_is_ordered 
pointset.add(Point(i* 8+1,j * 16 - 1)) 
File "C:\Users\n\Desktop\\skeleton (5)\PointSet.py", line 15, in add 
if point.compare(Point(x, y)) == -1: 
File "C:\Users\\Desktop\\skeleton (5)\Point.py", line 44, in compare 
if e1 > e2: 
UnboundLocalError: local variable 'e2' referenced before assignment 


Traceback (most recent call last): 
File "C:\Users\n\Desktop\\skeleton (5)\TestPointSet.py", line 59, in test_pointset_is_ordered2 
self.assertEqual(p, self.pt_list[i]) 
File "C:\Users\\AppData\Local\Programs\Python\Python35-32\lib\unittest\case.py", line 820, in assertEqual 
assertion_func(first, second, msg=msg) 
File "C:\Users\\AppData\Local\Programs\Python\Python35-32\lib\unittest\case.py", line 810, in _baseAssertEqual 
if not first == second: 
File "C:\Users\\Desktop\\skeleton (5)\Point.py", line 67, in __eq__ 
return self.x == other.getx and self.y == other.gety 
AttributeError: 'tuple' object has no attribute 'getx' 
+1

@AMACB這將如何幫助?你的if語句永遠不會被執行,這就是爲什麼你不初始化'e2'變量。 – Selcuk

+1

你只在'if'語句中設置'e2',它們並不總是運行。 – AMACB

+0

爲什麼?不應該定義e1/e2,因爲它只會在創建c1和c2之後轉義while循環。任何x或y不能大於1023,因此c1或c2的唯一可能結果應該是(0,0)(1,0)(1,0)(1,1)。這就像圖形象限。 – Ecoturtle

回答

0

你只在特殊情況下定義E1和E2,當你的第一個條件if語句滿足。即使如此,你也沒有定義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更Python。

您的功能不正確,因爲您不調用這些功能。將其更改爲:

def __eq__(self, other): 
    return self.x == other.getx() and self.y == other.gety() 
相關問題